var xmlhttp;
var hookid;

//SAMPLE FUNCTION ONLY
/*
function showSelection(displaywhere,str)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		return;
	}
	var url="../actions/test.php";
	url=url+"?val="+str;
	hookid=displaywhere;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
//--END SAMPLE FUNCTION
*/

function sendMail()
{
	if(validateMail())
	{
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		{
			alert("Sorry, your current browser does not support this function.");
			return;
		}
		document.getElementById('btnsend').disabled=true;
		xmlhttp.onreadystatechange=mailReturn;
		var num=encodeURIComponent(document.getElementById("picnum").value);
		var sender=encodeURIComponent(document.getElementById("sender").value);
		var contact=encodeURIComponent(document.getElementById("contact").value);
		var replyto=encodeURIComponent(document.getElementById("replyto").value);
		var subject=encodeURIComponent(document.getElementById("subject").value);
		var message=encodeURIComponent(document.getElementById("message").value);
		var captcha=encodeURIComponent(document.getElementById("captcha").value);
		var parameters="contact="+contact+"&num="+num+"&sender="+sender+"&replyto="+replyto+"&subject="+subject+"&message="+message+"&captcha="+captcha;
		xmlhttp.open("POST", "../actions/sendmail.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(parameters);
	}
}

function mailReturn()
{
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText == "OK")
		{
			alert("Mail sent!");
			location.reload(true);
		}
		else
		{
			alert(xmlhttp.responseText);
			imgsrc = document.getElementById('imgcode').src;
			newnum = Math.floor(Math.random()*21);
			document.getElementById('imgcode').src= imgsrc.substring(0,imgsrc.lastIndexOf('/')+1)+newnum+'.png';
			document.getElementById('picnum').value=newnum;
		}
		document.getElementById('btnsend').disabled = false;
	}
}

function validateMail()
{
	if(document.getElementById('sender').value == '')
	{
		alert('Please enter a name');
		return false;
	}
	if(document.getElementById('replyto').value == '' || (document.getElementById('replyto').value.match("^[A-Za-z0-9][A-Za-z_.\-]*[A-Za-z0-9]@[A-Za-z0-9\-]+(\.[A-Za-z0-9]{2,3}){1,2}$") == null || document.getElementById('replyto').value.match('[.]{2}') != null))
	{
		alert('Please enter a valid email address so we can contact you');
		return false;
	}
	if(document.getElementById('subject').value == '')
	{
		alert('Please enter a message subject');
		return false;
	}
	if(document.getElementById('message').value == '')
	{
		alert('Enter a message');
		return false;
	}
	if(document.getElementById('message').value.length > 1000)
	{
		alert('Your message is too long');
		return false;
	}
	if(document.getElementById('captcha').value=='')
	{
		alert('Enter the validation code');
		return false;
	}
	return true;
}

var trycount;
function submitKey()
{	
	if(trycount<3)
	{
		trycount++;
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		{
			return;
		}
		var url="../actions/download.php";
		url=url+"?action=confirm&dlkey="+document.getElementById('dlkey').value;
		xmlhttp.onreadystatechange=stateChanged;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else
	{
		window.location = "index.php";
	}
}

//general ajax functions
function stateChanged()
{
	if (xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText != '')
		{
			alert(xmlhttp.responseText);
			document.getElementById('dlkey').select();
		}
		else
		{
			document.getElementById('dllink').href="download.php?dlkey="+document.getElementById('dlkey').value;
			document.getElementById('dlform').style.display='none';
			document.getElementById('dllink').style.display = 'inline';
		}
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support AJAX. Parts of this page will not function.");
		return null;
	}
}

//get x,y positions of mouse (relative to the window)
function getX(event)
{
	if(!event.pageX)
	{
		return event.clientX;// - document.documentElement.scrollLeft;
	}
	else
	{
		return event.pageX - (document.body.scrollLeft || document.documentElement.scrollLeft);
	}
}
function getY(event)
{
	if(event.pageY)
	{
		return event.pageY - (document.body.scrollTop || document.documentElement.scrollTop);
	}
	else
	{
		return event.clientY;// - document.documentElement.scrollTop;
	}
}

//get the mouse's X, Y coordinates relative to the document
function getDocX(event)
{
	if(!event.pageX)
	{
		return event.clientX + (document.body.scrollLeft || document.documentElement.scrollLeft);
	}
	else
	{
		return event.pageX;
	}
}
function getDocY(event)
{
	if(!event.pageY)
	{
		return event.clientY + (document.body.scrollTop || document.documentElement.scrollTop);
	}
	else
	{
		return event.pageY;
	}
}

//get object's rendered position on the browser (relative to edges of document)
function getOffsetLeft(obj)
{
	var curleft = 0;
	do
	{
		curleft += obj.offsetLeft;
	}while (obj = obj.offsetParent)
	return curleft;
}

function getOffsetTop(obj)
{
	var curtop = 0;
	do
	{
		curtop += obj.offsetTop;
	}while (obj = obj.offsetParent)
	return curtop;
}

//get the number value from css property value (which is usually in string)
function getNum(strval)
{
	if(strval==""){return 0;}
	var lastchar = strval.substr(strval.length-1,1);
		if (!lastchar.match("[0-9]")) {
			return parseInt(strval.substr(0,strval.length-2));
		}
		else { return parseInt(strval); }
}

	
function getElementsByName_IEFix(namestr)
{
	var arr = new Array();
	var i=0;
	for(var k=0; k<document.all.length; k++)
	{
		if(document.all[k].name==namestr){
			arr[i]=document.all[k];
			i++;}
	}
	return arr;
}

function getField(objname)
{
	var objarr = document.getElementsByName(objname);
	return objarr[0];
}

function formatNumber(num, len)
{
	var str = num+'';
	if(str.length < len)
	{
		while(str.length < len)
		{
			str = '0'+str;
		}
	}
	return str;
}
