var agent = navigator.userAgent.toLowerCase ();
var isOPERA = (agent.indexOf ("opera") != -1);
var isIE = ((agent.indexOf ("msie") != -1) && (agent.indexOf ("safari") == -1) && !isOPERA);


function isCanNotProcess(__form)
{
    return (__form) ? __form.canNotProcess.value == 'true' : true;
}
function processAction(__form)
{
	__form.canNotProcess.value = 'true';
}
function cancelAction(__form) {
	__form.canNotProcess.value = 'false';
}
function preventDefaultEvent(__event)
{
	if(isIE)
		__event.returnValue = false;
	else
		__event.preventDefault();
}

function doSubmitForm(__event, formID)
{
	
	var form = document.forms[formID];

	if(isCanNotProcess(form))
	{
		preventDefaultEvent(__event);
		return;
	}

	var form = document.forms[formID];
	processAction(form);
}


function validatePwd(fieldname) {
	//Initialise variables
	var errorMsg = "";
	var space  = " ";
	fieldvalue  = fieldname.value; 
	fieldlength = fieldvalue.length; 
	
	if (fieldvalue != '') {
		//It must not contain a space
		if (fieldvalue.indexOf(space) > -1) {
			errorMsg += "\nPasswords cannot include a space.\n";
		}
		
		//It must contain at least one upper case character     
		if (!(fieldvalue.match(/[A-Z]/))) {
			errorMsg += "\nStrong passwords must include at least one uppercase letter.\n";
		}
		
		//It must contain at least one lower case character
		if (!(fieldvalue.match(/[a-z]/))) {
			errorMsg += "\nStrong passwords must include one or more lowercase letters.\n";
		}
		
		//It must be at least 7 characters long.
		if (!(fieldlength >= 5)) {
			errorMsg += "\nStrong passwords must be at least 5 characters long.\n";
		}
	}
	
	//If there is aproblem with the form then display an error
	if (errorMsg != "") {
		msg  = "Please enter strong password. See below:\n";
		msg += "Your password must be a minimum of 5 characters\n";
		msg += "contain a combination of upper and lowercase letters\n";
		msg += "Type a password which meets these requirements in both text boxes.\n";
		
		errMsg.innerText = msg;
		return false;
	}
	
	return true;
}

function frmreservation_check()
{
	if (document.frmreservation.CardNum.value == '') 
	{
		errMsg.innerText = "Please Enter Your Credit Card Number.";
		document.frmreservation.CardNum.focus();
		return false;
	}
	if (!document.frmreservation.CardNum.value.match(/^[\d|\s|\-]+$/g)) {
		errMsg.innerText = "Please Enter Valid Credit Card Number.";
		document.frmreservation.CardNum.focus();
		return false;
	}
	if (!document.frmreservation.CCV.value.match(/^[\d]+$/g)) {
		errMsg.innerText = "Please Enter Valid Card Identification Number.";
		document.frmreservation.CCV.focus();
		return false;
	}
	if (document.frmreservation.ExpirationDate.value  =='') 
	{
		errMsg.innerText = "Please Enter Your Credit Card Expiration Date.";
		document.frmreservation.ExpirationDate.focus();
		return false;
	}

	// test value for right format

	var exp_date;
	var mm;
	var yy;
	var now_date = new Date();
	var re_date = /^(\d+)\/(\d+)$/;

	if (!re_date.exec(document.frmreservation.ExpirationDate.value)) 
	{
		errMsg.innerText = "Please Use MM/YY Format for Your Credit Card Expiration Date.";
		document.frmreservation.ExpirationDate.focus();
		return false;
	}
	mm = RegExp.$1;
	yy = RegExp.$2;
	if (yy<100)
	{ 
		exp_date = new Date(parseInt(yy,10)+2000, parseInt(mm,10)-1, 28);
	} else {
		exp_date = new Date(parseInt(yy,10), parseInt(mm,10)-1, 28);
	}
	if ((exp_date < now_date) || (parseInt(mm,10) > 12))
	{
		errMsg.innerText = "Please Enter Valid Credit Card Expiration Date.";
		document.frmreservation.ExpirationDate.focus();
		return false; 
	}

	if (document.frmreservation.FirstName.value=='')
	{
		errMsg.innerText = "Please enter Your First Name.";
		document.frmreservation.FirstName.focus();
		return false;
	}

	if (document.frmreservation.LastName.value=='')
	{
		errMsg.innerText = "Please Enter Your Last Name.";
		document.frmreservation.LastName.focus();
		return false;
	}

	if (document.frmreservation.Address.value =='')
	{
		errMsg.innerText = "Please Enter Your Billing Address.";
		document.frmreservation.Address.focus();
		return false;
	}

	if (document.frmreservation.City.value=='') 
	{
		errMsg.innerText = "Please Enter Your City.";
		document.frmreservation.City.focus();
		return false;
	}

	if (document.frmreservation.State.value=='') 
	{
		errMsg.innerText = "Please Select Your State.";
		document.frmreservation.State.focus();
		return false;
	}

	if (!is_zip_usa_canada(document.frmreservation.ZipCode.value)) 
	{
		errMsg.innerText = "Please Enter valid Zip Code.";
		document.frmreservation.ZipCode.focus();
		return false;
	}
	if (document.frmreservation.PersonParking.value=='') 
	{
		errMsg.innerText = "Please Enter your Parking Person.";
		document.frmreservation.PersonParking.focus();
		return false;
	}
	
	if (document.frmreservation.Email != null){ // user not registered
		if (document.frmreservation.Email.value=='')
		{
			errMsg.innerText = "Please Enter Your E-mail address.";
			document.frmreservation.Email.focus();
			return false;
		}

		var re = new RegExp("^([0-9a-zA-Z]+[-._+&]*)+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", "i");
		if (!re.test(document.frmreservation.Email.value)){
			errMsg.innerText = "Invalid E-mail address.";
			return false;
		}

		if (document.frmreservation.ConfirmEmail.value=='')
		{
			errMsg.innerText = "Please Confirm Your E-mail address.";
			document.frmreservation.ConfirmEmail.focus();
			return false;
		}
	
		if (document.frmreservation.ConfirmEmail.value != document.frmreservation.Email.value)
		{
			errMsg.innerText = "Email do not match confirm email.";
			document.frmreservation.ConfirmEmail.focus();
			return false;
		}

		if (document.frmreservation.Password.value=='')
		{
			errMsg.innerText = "Please Enter Your Password.";
			document.frmreservation.Password.focus();
			return false;
		}
		
		if (!validatePwd(document.frmreservation.Password)){
			document.frmreservation.Password.focus();
			return false;
		}

		if (document.frmreservation.ConfirmPassword.value=='')
		{
			errMsg.innerText = "Please Confirm Your Password.";
			document.frmreservation.ConfirmPassword.focus();
			return false;
		}
	
		if (document.frmreservation.ConfirmPassword.value != document.frmreservation.Password.value)
		{
			errMsg.innerText = "Password does not match confirm password.";
			document.frmreservation.ConfirmPassword.focus();
			return false;
		}
	}

	if (document.frmreservation.CheckTerms.checked ==false ) 
	{
		errMsg.innerText = "Please agree to our Terms of Services before booking your reservation.";
		document.frmreservation.CheckTerms.focus();
		return false;
	}
	
	return true;
}
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   errMsg.innerText = "Invalid E-mail Address";
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   errMsg.innerText = "Invalid E-mail Address";
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    errMsg.innerText = "Invalid E-mail Address";
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    errMsg.innerText = "Invalid E-mail Address";
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    errMsg.innerText = "Invalid E-mail Address";
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    errMsg.innerText = "Invalid E-mail Address";
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    errMsg.innerText = "Invalid E-mail Address";
		    return false
		 }

 		 return true					
}

function validateaccess(__form)
{
	
	if ((document.frmlogin.Username.value=='') || (document.frmlogin.Username.value==null))
	{
		errMsg.innerText = "Please enter your Email Address.";
		document.frmlogin.Username.focus();
		return false;
	}
	if (echeck(document.frmlogin.Username.value)==false){
		document.frmlogin.Username.value=""
		document.frmlogin.Username.focus()
		return false
	}
	
	if (document.frmlogin.Password.value=='')
	{
		errMsg.innerText = "Please enter your password.";
		document.frmlogin.Password.focus();
		return false;
	}
	
	return true;
}

function validateReservation(__form, flNonFirstTransaction)
{
	
	if(!frmreservation_check()) {
		//tb_show('', '#TB_inline?height=200&width=300&inlineId=checkoutErrorMessage&modal=true');
		cancelAction(__form);
		return false; 
	}
	
	if(flNonFirstTransaction) {
		flConfirm = confirm("Our system indicates that you just completed a parking reservation.\nIf you would like to place a second reservation, please click OK.\nOtherwise, click Cancel.");
		if(flConfirm == false) {
			cancelAction(__form);
			return false;
		}
	}
	
	//tb_show('', '#?keepThis=true&TB_iframe=true&height=200&width=300&modal=true');
	
	/*var ifr = document.getElementById("TB_iframeContent");
	if (ifr) {
		__form.target = ifr.name;
	}
	*/
	return true;
}

