sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function getById(elementname)
{
	if(document.all)
	{
		element=document.all[elementname];	
	}
	else
	{
		element=document.getElementById(elementname);	
	}
	return element;
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function isInt(x)
{
	var y=parseInt(x); 
	if (isNaN(y))
		return false; 
	return x==y && x.toString()==y.toString();
}

function changeClass(theYear,theMonth)
{
	window.location.href="chooseday.php?cl="+getById('class').options[getById('class').selectedIndex].value+"&year="+theYear+"&month="+theMonth;
}

function selectDay(theDay)
{
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				//alert(xmlHttp.responseText);
				var content=xmlHttp.responseText.split("|");

				getById('date').value=content[1];
				getById('displayDate').innerHTML=content[2];
				newMonth(getById('m').value,getById('y').value)
			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 
	
	var currMonth=getById('m').value;
	var currYear=getById('y').value;

	xmlHttp.open('GET', "includes/ajax.getStamp.php?m="+currMonth+"&y="+currYear+"&d="+theDay, true); 
	xmlHttp.send(null);
}

function SwapOn(whichBox)
{
	getById('textLeft'+whichBox).style.backgroundImage="url(images/textLeft1Over.gif)";
	getById('textRight'+whichBox).style.backgroundImage="url(images/textRight"+whichBox+"Over.gif)";
	getById('textDiv'+whichBox).style.color="#ff9f56";
}
function SwapOut(whichBox)
{
	getById('textLeft'+whichBox).style.backgroundImage="url(images/textLeft1.gif)";
	getById('textRight'+whichBox).style.backgroundImage="url(images/textRight"+whichBox+".gif)";
	getById('textDiv'+whichBox).style.color="#FFFFFF";
}

function newMonth(newMonth,newYear)
{
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				//alert(xmlHttp.responseText);
				var content=xmlHttp.responseText.split("|");
				getById('calendarDiv').innerHTML=content[0];

				getById('m').value=content[1];
				getById('y').value=content[2];
			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 

	xmlHttp.open('GET', "includes/ajax.getMonth.php?m="+newMonth+"&y="+newYear+"&chosenStamp="+getById('date').value, true); 
	xmlHttp.send(null);
}

function slideRight(rightTab,leftTab)
{
	getById('weekDay8').innerHTML="";
	getById('weekDay9').innerHTML="";
	getById('weekDay10').innerHTML="";
	getById('weekDay11').innerHTML="";
	getById('weekDay12').innerHTML="";
	getById('weekDay13').innerHTML="";
	getById('weekDay14').innerHTML="";
	
	var firstTab=parseInt(leftTab+1);
	if(getById("tab"+firstTab).innerHTML!=getById('today').value)
	{
		getById("tab"+leftTab).style.display="block";
		getById("tab"+leftTab).style.width="1px";
		getById("tab"+leftTab).style.visibility="hidden";
		getById("tab"+rightTab).style.display="none";
		var leftWidth=0;
		resizeRight(rightTab,leftTab,leftWidth);
	}
	else
	{
		resetTabs();	
	}
}
function resizeRight(rightTab,leftTab,leftWidth)
{
	leftWidth=leftWidth+10;
	getById("tab"+leftTab).style.width=leftWidth+"px";
	if(leftWidth<85)
	{
		setTimeout("resizeRight("+rightTab+","+leftTab+","+leftWidth+")",0);	
	}
	else
	{
		getById("tab"+leftTab).style.visibility="visible";
		getById("tab"+leftTab).style.width="85px";
		getById("tempSelected").value=parseInt(getById("tempSelected").value)+1;
		
		if(parseInt(getById("tempSelected").value)<(parseInt(getById("selectedTab").value)+3))
		{
			rightTab=rightTab-1;
			leftTab=leftTab-1;
			slideRight(rightTab,leftTab);
		}
		else
		{
			//reset tabs	
			resetTabs();
		}
	}
}

function slideLeft(rightTab,leftTab)
{
	getById('weekDay8').innerHTML="";
	getById('weekDay9').innerHTML="";
	getById('weekDay10').innerHTML="";
	getById('weekDay11').innerHTML="";
	getById('weekDay12').innerHTML="";
	getById('weekDay13').innerHTML="";
	getById('weekDay14').innerHTML="";
	var leftWidth=85;
	getById("tab"+leftTab).style.visibility="hidden";
	resizeLeft(rightTab,leftTab,leftWidth);
}
function resizeLeft(rightTab,leftTab,leftWidth)
{
	leftWidth=leftWidth-10;
	if(leftWidth>0)
	{
		getById("tab"+leftTab).style.width=leftWidth+"px";
		setTimeout("resizeLeft("+rightTab+","+leftTab+","+leftWidth+")",0);	
	}
	else
	{
		getById("tab"+leftTab).style.width="0px";
		getById("tempSelected").value=parseInt(getById("tempSelected").value)-1;
		getById("tab"+leftTab).style.visibility="visible";
		if(parseInt(getById("tempSelected").value)>(parseInt(getById("selectedTab").value)-3))
		{
			getById("tab"+rightTab).style.display="block";
			getById("tab"+rightTab).style.width="85px";
			getById("tab"+leftTab).style.display="none";
			rightTab=rightTab+1;
			leftTab=leftTab+1;
			slideLeft(rightTab,leftTab);
		}
		else
		{
				//reset tabs
			getById("tab"+rightTab).style.display="block";
			getById("tab"+rightTab).style.width="85px";
			getById("tab"+leftTab).style.display="none";
			resetTabs();
		}
	}
}

function resetTabs()
{
	var firstDisplay=0;
	var found=false;
	while(!found && firstDisplay<=21)
	{
		firstDisplay++;
		//alert(firstDisplay+": "+getById("tab"+firstDisplay).style.display);
		if(getById("tab"+firstDisplay).style.display=="block")
		{
			found=true;	
		}
	}
	var firstDate=getById("tab"+firstDisplay).innerHTML;
	
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				//alert(xmlHttp.responseText);
				getById('tabDiv').innerHTML=xmlHttp.responseText;
				if(getById('tempSelected').value<8)
					getById('tempSelected').value=8;
				if(getById('tempSelected').value>14)
					getById('tempSelected').value=14;
				resetWeekdays(firstDate);
				selectTab(getById('tempSelected').value);

			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 
	
	xmlHttp.open('GET', "includes/ajax.resetTabs.php?firstDate="+firstDate, true);
	xmlHttp.send(null);
}
function resetWeekdays(firstDate)
{
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				var array=xmlHttp.responseText.split(",");
				var currDay=8;
				var currEl=0;
				while(currDay<=14)
				{
					getById("weekDay"+currDay).innerHTML=array[currEl];
					currDay++;
					currEl++;
				}
				
			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 

	xmlHttp.open('GET', "includes/ajax.resetWeekdays.php?firstDate="+firstDate, true);
	xmlHttp.send(null);
}
function selectTab(tabNum)
{
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				//alert(xmlHttp.responseText);
				getById('contentBox').innerHTML=xmlHttp.responseText;
				setReservationDetails(0,0,0,"Not Selected","Not Selected","Not Selected","Not Selected");
				setTabDate(getById('tab'+tabNum).innerHTML,getById('year').value);
			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 
	
	getById('tab'+getById('selectedTab').value).className="tab";
	getById('selectedTab').value=tabNum;
	getById('tempSelected').value=tabNum;
	if(getById('selectedTab').value==8)
		getById('tab'+getById('selectedTab').value).className="tab leftselected";
	else if(getById('selectedTab').value==14)
		getById('tab'+getById('selectedTab').value).className="tab rightselected";
	else
		getById('tab'+getById('selectedTab').value).className="tab tabselected";
	examinerText="";
	if(parseInt(getById('hiddenExaminer').value)>0)
	{
		examinerText="&examiner="+getById('hiddenExaminer').value;	
	}
	registryText="";
	if(parseInt(getById('hiddenRegistry').value)>0)
	{
		registryText="&registry="+getById('hiddenRegistry').value;	
	}
	xmlHttp.open('GET', "includes/ajax.getTimes.php?day="+getById('tab'+tabNum).innerHTML+"&class="+getById('class').value+"&city="+getById('city').value+"&year="+getById('year').value+examinerText+registryText, true); 
	xmlHttp.send(null);
}
function setTabDate(day,year)
{
	var xmlHttp;
	//alert(inputVal);
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	} 
	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				//alert(xmlHttp.responseText);	
				getById('dateSelection').innerHTML=xmlHttp.responseText;
				getById('hiddenDateSelection').value=xmlHttp.responseText;
			}//end if status==200
			else 
			{ 
				
			}
		}//end if readystate==4
	}; //end state change function 

	xmlHttp.open('GET', "includes/ajax.setTabDate.php?day="+day+"&year="+year, true); 
	xmlHttp.send(null);
}
function setReservationDetails(intervalStart,companyId,registryId,intervalText,companyName,registryName,price)
{
	getById("hiddenTimeSelection").value=intervalStart;
	getById("hiddenCompanySelection").value=companyId;
	getById("hiddenRegistrySelection").value=registryId;
	getById("hiddenPriceSelection").value=price;
	
	getById("startSelection").innerHTML=intervalText;
	//getById("companySelection").innerHTML=companyName;
	getById("registrySelection").innerHTML=registryName;
	if(price=="Not Selected")
	{
		getById("priceSelection").innerHTML=price;
		getById("bookNowImage").style.display="none";
		getById("secondarySubmit").style.display="none";
	}
	else
	{
		getById("priceSelection").innerHTML="$"+parseFloat(price).toFixed(2);
		getById("bookNowImage").style.display="block";
		getById("secondarySubmit").style.display="block";
	}
}
function showReminder()
{
	getById('reminder_div').style.display='block';	
	if(getById('R_Area_Code').value.length==0 && getById('R_Prefix').value.length==0 && getById('R_Suffix').value.length==0)
	{
		getById('R_Area_Code').value=getById('Area_Code').value;
		getById('R_Prefix').value=getById('Prefix').value;
		getById('R_Suffix').value=getById('Suffix').value;
	}
	getById('R_Area_Code').focus();
}

function hideReminder()
{
	getById('reminder_div').style.display='none';	
}
function hidecc()
{
	getById('cc_div').style.display='none';	
}
function showcc()
{
	getById('cc_div').style.display='block';	
	if(getById('Exam_CC_Email').value.length==0)
	{
		getById('Exam_CC_Email').value=getById('Exam_CC_Email').value;
	}
	getById('Exam_CC_Email').focus();
}
function checkPhone(which)
{
	if(which==1)
	{
		if(getById('Area_Code').value.length==3)
		{
			getById('Prefix').focus();	
		}
	}
	else if(which==2)
	{
		if(getById('Prefix').value.length==3)
		{
			getById('Suffix').focus();	
		}
	}
}
function backStep()
{
	var theclass=getById('hiddenClassSelection').value;
	var city=getById('hiddenCitySelection').value;
	var m=getById('hiddenMonthSelection').value;
	var y=getById('hiddenYearSelection').value;
	var date=getById('hiddenStampSelection').value;
	
	window.location.href="choosetime.php?class="+theclass+"&city="+city+"&m="+m+"&y="+y+"&date="+date;	
}

function check_cc_div()
{
	if(getById('payment_method_visa').checked || getById('payment_method_mastercard').checked)
	{
		getById('credit_card_div').style.display="block";	
		getById('bookNowImage').style.display="inline";
		getById('paypal_div').style.display="none";
	}
	else
	{
		getById('credit_card_div').style.display="none";	
		getById('bookNowImage').style.display="none";
		getById('paypal_div').style.display="block";
	}
}
function SetExpressCheckout(paypal_api,paypal_user,paypal_pass,paypal_sig)
{
	if(getById('regterms').checked && getById('registry_initial').value.length>0 && getById('eligible').checked && getById('eligible_initial').value.length>0)
	{
		var xmlHttp;
		//alert(inputVal);
		if (window.XMLHttpRequest)     // Object of the current windows
		{ 
			xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
		} 
		else if (window.ActiveXObject)   // ActiveX version
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
		}
		
		var amount=parseFloat(getById('AMT').value).toFixed(2);
		var params = "";
		params+="USER="+paypal_user;
		params+="&PWD="+paypal_pass;
		params+="&SIGNATURE="+paypal_sig;
		params+="&VERSION=60.0";
		params+="&PAYMENTACTION=Sale";
		
		params+="&L_NAME0=Road Test Reservation";
		params+="&L_DESC0="+getById('Online_Title').value;
		params+="&L_AMT0="+getById('Online_Price').value;
		params+="&L_QTY0=1";
		
		params+="&L_NAME1=Road Test Permit";
		params+="&L_DESC1=Tax Exempt";
		params+="&L_AMT1="+getById('PERMIT').value;
		params+="&L_QTY1=1";
		
		params+="&L_NAME2=Service Fee";
		params+="&L_DESC2=AlbertaDriverExaminer.ca";
		params+="&L_AMT2="+getById('service_fee').value;
		params+="&L_QTY2=1";
		
		params+="&ITEMAMT="+getById('subTotal').value;
		params+="&TAXAMT="+getById('gstAmount').value;
		params+="&AMT="+amount;
		params+="&CURRENCYCODE=CAD";
		params+="&PAGESTYLE=AlbertaDriverExaminer";
		params+="&RETURNURL="+getById('RETURNURL').value;
		params+="&CANCELURL="+getById('CANCELURL').value;
		params+="&METHOD=SetExpressCheckout";
		
		xmlHttp.onreadystatechange  = function()
		{ 
			if(xmlHttp.readyState  == 4)
			{
				if(xmlHttp.status  == 200) 
				{
					if(xmlHttp.responseText==0)
					{
						alert("There is a problem communicating with the PayPal servers. Please try again later. ERR Code: 002");
						getById('ExpressCheckoutButton').style.visibility="visible";
						getById('loadingIMG').style.display='none';
					}
					else
					{
						var temp='';
						var resultArray=new Array();
						var results=xmlHttp.responseText.split("&");
						for(var i=0; i<results.length;i++)
						{
							temp=results[i].split("=");
							resultArray[temp[0]]=temp[1];
							//alert(getById('PAYPALSITE').value+"/webscr?cmd=_express-checkout&token="+resultArray['TOKEN']+"&AMT="+amount+"&CURRENCYCODE=CAD&RETURNURL="+getById('RETURNURL').value+"&CANCELURL="+getById('CANCELURL').value+"&useraction=commit");
							window.location.href=getById('PAYPALSITE').value+"/webscr?cmd=_express-checkout&token="+resultArray['TOKEN']+"&AMT="+amount+"&CURRENCYCODE=CAD&RETURNURL="+getById('RETURNURL').value+"&CANCELURL="+getById('CANCELURL').value+"&useraction=commit";
						}
					}
					
				}//end if status==200
				else 
				{ 
					alert("There is a problem communicating with the PayPal servers. Please try again later. ERR Code: 001");
					getById('ExpressCheckoutButton').style.visibility="visible";
					getById('loadingIMG').style.display='none';	
				}
			}//end if readystate==4
		}; //end state change function 
		xmlHttp.open("GET", "https://www.albertadriverexaminer.ca/includes/ajax.SetExpressCheckout.php?"+params, true);
		xmlHttp.send(params);
		getById('ExpressCheckoutButton').style.visibility="hidden";
		getById('loadingIMG').style.display='block';
	}
	else
	{
		alert("Please agree to the registry terms and policies, and affirm that you are eligible for your road test first");	
	}
}
function DoDirectPayment(paypal_api,paypal_user,paypal_pass,paypal_sig,ipaddress)
{
	getById('bookNowImage').style.display="none";
	getById('loadingIMG').style.display="inline";
	var xmlHttp;
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	}
	
	var amount=parseFloat(getById('AMT').value).toFixed(2);
	var cardType='';
	if(getById('payment_method_visa').checked)
		cardType='Visa';
	else if(getById('payment_method_mastercard').checked)
		cardType='MasterCard';
	var params = "";
	params+="USER="+paypal_user;
	params+="&PWD="+paypal_pass;
	params+="&SIGNATURE="+paypal_sig;
	params+="&VERSION=60.0";
	params+="&PAYMENTACTION=Sale";
	params+="&IPADDRESS="+ipaddress;
	params+="&CREDITCARDTYPE="+cardType;
	params+="&ACCT="+getById('ACCT').value;
	params+="&EXPDATE="+getById('expiry_month').value+"20"+getById('expiry_year').value;
	params+="&CVV2="+getById('ccv').value;
	params+="&AMT="+amount;
	params+="&METHOD=DoDirectPayment";
	params+="&FIRSTNAME="+getById('card_name_first').value;
	params+="&LASTNAME="+getById('card_name_last').value;
	params+="&STREET="+getById('billing_address').value;
	params+="&CITY="+getById('billing_city').value;
	params+="&STATE="+getById('billing_province').options[getById('billing_province').selectedIndex].value;
	params+="&ZIP="+getById('billing_postal_prefix').value+"-"+getById('billing_postal_suffix').value;
	params+="&COUNTRYCODE=CA";
	params+="&CURRENCYCODE=CAD";

	xmlHttp.onreadystatechange  = function()
	{ 
		if(xmlHttp.readyState  == 4)
		{
			if(xmlHttp.status  == 200) 
			{
				if(xmlHttp.responseText==0)
				{
					window.location.href='https://www.albertadriverexaminer.ca/booknow.php?method=creditcard';
				}
				else
				{	
					alert(xmlHttp.responseText);
					getById('bookNowImage').style.display="inline";
					getById('loadingIMG').style.display='none';
				}
			}//end if status==200
			else 
			{ 
				alert("There is a problem communicating with the PayPal servers. Please try again later. ERR Code: 005");
				getById('bookNowImage').style.display="inline";
				getById('loadingIMG').style.display='none';	
			}
		}//end if readystate==4
	}; //end state change function 
	xmlHttp.open("GET", "https://www.albertadriverexaminer.ca/includes/ajax.ValidatePayment.php?"+params, true);
	xmlHttp.send(params);
}
