// JavaScript Document
function CancelRes(){
	var http = getHTTPObject();
	if(validate_email(document.frmCancel.cancel_email, "Please enter a valid email.")){
		targ = "processCancelation.asp?Process="+document.frmCancel.Process.value+
			"&email="+document.frmCancel.cancel_email.value+"&ms="+new Date().getTime();
		
		//alert(targ);	
		http.open('GET', targ, false);
		http.send(null);
		document.getElementById("divCancel").innerHTML = '<strong><font color="#FF0000">Please check your email to confirm cancellation.</font></strong>';
		return true;
	}
	else{
		document.frmCancel.cancel_email.focus();	
		return false;
	}
}

function AdjustPricing(){
	if(document.frmResRequest.aaaMember.checked){
		document.getElementById("divCosts").innerHTML = document.getElementById("divAAAPrices").innerHTML;
	}
	else{
		document.getElementById("divCosts").innerHTML = document.getElementById("divNonAAAPrices").innerHTML;
	}
	document.getElementById("divTotalCost").innerHTML = "";
}

function CheckDates(){
	var currDate = new Date().getTime();
	var arriveDate = new Date(document.frmResRequest.arrival.value).getTime();
	if (document.frmResRequest.ariAP.value == 'PM') {arriveDate = arriveDate + (12*1000*60*60)}

	arriveDate = arriveDate + (document.frmResRequest.ariHour.value*1000*60*60)
	if(arriveDate <= currDate) {
	alert("You cannot arrive before the current date and time.");
	  return false
	}
	
	var departDate = new Date(document.frmResRequest.depart.value).getTime();
	departDate = departDate + (document.frmResRequest.depHour.value*1000*60*60)
	if (document.frmResRequest.depAP.value == 'PM') {departDate = departDate + (12*1000*60*60)}
	if(arriveDate && departDate){
		if(arriveDate > departDate){
			alert("You cannot arrive after you depart.\n Please select a valid date.");
			document.frmResRequest.depart.value = "";
			document.frmResRequest.depart.focus();
			return
		}
	}	
}

function SetType(thisType){
	document.frmResRequest.typeDesc.value = thisType;
}


function DateDiff(byType, fromDate, toDate){
	//alert(fromDate+" ~ "+toDate);
	if((fromDate == "Check In...") || (toDate == "Check Out...")){
		//alert(fromDate+" ~ "+ toDate);
		return;
	}
	var totalMiliseconds = toDate.getTime() - fromDate.getTime();
	var totalSeconds = totalMiliseconds / 1000;
	var totalMinutes = (totalSeconds / 60);
	var totalHours = totalMinutes / 60;
	//alert("PF:"+parseFloat(totalHours)+" PI:"+parseInt(totalHours))
	if(parseFloat(totalHours) != parseInt(totalHours)){
		totalHours = parseInt(totalHours) + 1;
	}
	var totalDays = totalHours / 24;	
	
	if(parseFloat(totalDays) != parseInt(totalDays)){
		totalDays = parseInt(totalDays) + 1;
	}
	switch(byType){// This needs to be completed to accomdate more switches
		case "h":
			return totalHours;
			break;
	}
}

function CurrencyFormatted(amount){
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function validate_form(thisform) {
    var found = 0
    document.frmResRequest.depHour.style.backgroundColor='#fff'
    document.frmResRequest.ariHour.style.backgroundColor='#fff'
    vExit = CheckDates()
	if (vExit == '0') {return false}
	if (!validateArea(thisform)) return false;
	with (thisform) {	
		var inputs = thisform.getElementsByTagName("input");
		var textareas = thisform.getElementsByTagName("textarea");
		var selects = thisform.getElementsByTagName("select");
		var checkboxes = thisform.getElementsByTagName("checkbox");
		for (var i=0; i < inputs.length; i++) {
			inputs[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < textareas.length; i++) {
			textareas[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < selects.length; i++) {
			selects[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < inputs.length; i++) {
//			alert(71+' '+inputs[i].name+' > '+inputs[i].value.length+' , '+inputs[i].className.substr(0,3).toUpperCase());
			if(inputs[i].type == "checkbox" && !inputs[i].checked && inputs[i].className.toUpperCase() == "REQ"){
				alert("Please read the disclaimer before proceeding.");
				inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				inputs[i].focus();
				return false;
			}
			if (inputs[i].type == 'radio') {
				if(inputs[i].checked) {
				 found = 1
				}
			}
			if (inputs[i].className.substr(0,3).toUpperCase() == "REQ" && (inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].value.length==0) {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + inputs[i].name.replace("*", "") + " is required. Please complete all required fields in form.");
				inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				inputs[i].focus();
				return false;
			} else {
			  if (inputs[i].name == "guest_email") {
			    vExit = isEmail (inputs[i], 'Please Enter a valid Email.');
				if (vExit == '0') {return false}
			  }
//			  alert(inputs[i].name)
			  if (inputs[i].name == "arrival") {
     			if (isDate(inputs[i].value)==false){
		    		inputs[i].focus()
					inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
			    	return false
     			}
			  }	
			  if (inputs[i].name == "depart") {
     			if (isDate(inputs[i].value)==false){
		    		inputs[i].focus()
					inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
			    	return false
     			}
			  }	
			}
		}
		if (found == 0) {
			alert("\t\tREQUIRED FIELD\n\n is required. Please select a SERVICE.");
			//	alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + "" + " is required. Please complete all required fields in form.");
			return false
		}
		for (var i=0; i < textareas.length; i++) {
			if(textareas[i].className.toUpperCase() == "REQ" && textareas[i].value.length < 1) {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + textareas[i].name.replace("*", "") + " is required. Please complete all required fields in form.");
				textareas[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				textareas[i].focus();
				return false;
			}
		}
		for (var i=0; i < selects.length; i++) {
			if (selects[i].className.toUpperCase() == "REQ" && selects[i].value.length==0) {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + selects[i].name.replace("*", "") + " is required. Please select an item from drop down");
				selects[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				selects[i].focus();
				return false;
			}
		}
		/*if(isdefined(document, 'contact_email')) {
			if (validate_required(contact_email,"Email must be filled out!")==false) {
				email.focus();
				return false;
			}
			else if(validate_email(contact_email,"Not a valid e-mail address!")==false) {
				email.focus();
				return false;
			}
		}*/
	}
	for(i = 0;i < thisform.elements.length; i++){
		if(thisform.elements[i].type == "select-one"){
			if(thisform.elements[i].value.length == 0 || 
			thisform.elements[i].value == "H"){
				alert("Please select an Hour.");
				thisform.elements[i].focus();
				thisform.elements[i].style.backgroundColor = 'red'
				return false;
				break;
			}
		}
	}
}

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="")	{
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

// use this function like this...
//<textarea name="comment" rows="6" cols="30" onkeypress="return validateChars(event, document.frm);"></textarea>
function validateChars(e, thisForm) {
	var keynum;
	var keychar;
	var charCheck;
	if(window.event) { // IE 
		keynum = e.keyCode;
	}
	else if(e.which) {// Netscape/Firefox/Opera
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	charCheck = /[[@]/;
	if (charCheck.test(keychar))
		alert("Square braces( [ ) and at symbols( @ ) are not allowed.");
	if (!charCheck.test(keychar))
		validateArea(thisForm);
	return !charCheck.test(keychar);
}

function validateArea(thisForm) {
	textAreas = thisForm.getElementsByTagName("textarea");
	returnValue = true;
	for (i = 0; i < textAreas.length; i++) {
		if (textAreas[i].value.indexOf("[") != -1 || textAreas[i].value.indexOf("@") != -1) {
			do {
				textAreas[i].value = textAreas[i].value.replace("[", "");
			} while (textAreas[i].value.indexOf("[") != -1);
			do {
				textAreas[i].value = textAreas[i].value.replace("@", "");
			} while (textAreas[i].value.indexOf("@") != -1);
			returnValue = false;
		}
	}
	if (!returnValue)
		alert("Square braces( [ ) and at symbols( @ ) are not allowed. They have been removed.");
	return returnValue;
}

function isdefined(object, variable) {
	return (typeof(eval(object)[variable]) != 'undefined');
}

function isEmail (s,message){
	// s is a form element
	// message is a string
	var i,ii;
	var j;
	var k,kk;
    var jj;
    var len;
    // Check blank
    // Check valid email
    // Must have a "@" and a "." to be valid.
    // Must have at least 1 character before "@"
    // Must have at least 1 character after "@" and before "."
    // Must have at least 2 characters after "."
    if (s.value.length >0){
		i=s.value.indexOf("@");
		ii=s.value.indexOf("@",i+1);
		j=s.value.indexOf(".",i);
		k=s.value.indexOf(",");
		kk=s.value.indexOf(" ");
		jj=s.value.lastIndexOf(".")+1;
		len=s.value.length;
		if ((i>0) && (j>(1+1)) && (k==-1) && (ii==-1) && (kk==-1) &&
			(len-jj >=2) && (len-jj<=3)) {
			        return true;
			    	alert("passed");
			}
		else {
	 		 	alert(message)
	 		 	s.focus();			
				s.style.background = "#FFBBBB";//sets the background color of the required field yellow
				return 0;
		}
     }
	else  {
	 		 	s.focus();	
				s.style.background = "#FFBBBB";//sets the background color of the required field yellow
	     	 	alert(message)				
				return 0;
	}
}



//**********************************************************
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

function CalcNew(){
	var totalDays;
	var totalHours;
	var outputStr;
	var thisPage = String(window.location);
	
	if(document.frmResRequest.arrival.value.length > 0){
		var arriveDate = document.frmResRequest.arrival.value;
	}
	for(var i=0; i<document.frmResRequest.ariHour.length; i++){
		if(document.frmResRequest.ariHour[i].selected){
			var arriveHour = document.frmResRequest.ariHour[i].value;
			if(parseInt(arriveHour) < 10) arriveHour = "0"+arriveHour;
		}
	}
	
	for(var i=0; i<document.frmResRequest.ariAP.length; i++){
		if(document.frmResRequest.ariAP[i].selected){
			var arriveAMPM = document.frmResRequest.ariAP[i].value;
		}
	}
	
	if(arriveDate && arriveHour && arriveAMPM){
		var arriveDate = arriveDate+" "+arriveHour+":00 "+arriveAMPM;
		arriveDate = new Date(arriveDate);
	}
	if(document.frmResRequest.depart.value.length > 0){
		var departDate = document.frmResRequest.depart.value;
	}
	
	for(var i=0; i<document.frmResRequest.depHour.length; i++){
		if(document.frmResRequest.depHour[i].selected){
			var departHour = document.frmResRequest.depHour[i].value;
			if(parseInt(departHour) < 10) departHour = "0"+departHour;
		}
	}
	
	for(var i=0; i<document.frmResRequest.depAP.length; i++){
		if(document.frmResRequest.depAP[i].selected){
			var departAMPM = document.frmResRequest.depAP[i].value;
		}	
	}
	
	if(departDate && departHour && departAMPM){
		var departDate = departDate+" "+departHour+":00 "+departAMPM;
		departDate = new Date(departDate);
	}
	var busFee = document.frmResRequest.bus_cost.value;
	
	for(var i=0; i < document.frmResRequest.Service_type.length; i++){
		if(document.frmResRequest.Service_type[i].checked){
			var costType = document.frmResRequest.Service_type[i].value;
		}
	}
	if(arriveDate && departDate){				
		totalHours = DateDiff("h", arriveDate, departDate);
		// totalHours = totalHours-1;
		if(parseFloat(totalHours) != parseInt(totalHours)){
			totalHours = parseInt(totalHours) + 1;
		}
	}
if(costType != undefined && arriveDate != undefined && departDate != undefined)	 {
var http = getHTTPObject(); // We create the HTTP Object
	targ = "calc_new.asp?st="+arriveDate.getFullYear()+'/'+(arriveDate.getMonth()+1)+'/'+arriveDate.getDate()+' '+arriveDate.getHours()+':'+arriveDate.getMinutes()+'0:'+arriveDate.getSeconds()+"0&ed="+departDate.getFullYear()+'/'+(departDate.getMonth()+1)+'/'+departDate.getDate()+' '+departDate.getHours()+':'+departDate.getMinutes()+'0:'+departDate.getSeconds()+"0&id="+costType+"&ms="+new Date().getTime();
	http.open('GET', targ, false);
	http.send(null);
	strHTML = http.responseText;
	aRay = strHTML.split('+')
//	alert(aRay[0].split(' - ')[0]);
	vRay = aRay[1].split('=')
	totalDays = vRay[1]
	totalDayCost = vRay[0]
	var testStr = "_koa.asp";
		
/*	totalDays = parseFloat(totalHours) / 24;
	if(parseFloat(totalDays) != parseInt(totalDays)) totalDays = parseInt(totalDays) + 1;
	if(thisPage.indexOf(testStr) > 0){
		totalDays = totalDays - 1;
	}	
	var totalDayCost = totalDays * parseFloat(costType);*/
	var grandTotal = parseFloat(totalDayCost) + parseFloat(busFee);
	if(!isNaN(totalDayCost) && !isNaN(grandTotal)){
	
		if(thisPage.indexOf(testStr) > 0){
			subTotal = parseFloat(totalDayCost) + parseFloat(costType) + parseFloat(busFee);
			outputStr = "<table align=left width=200  cellpadding=0 cellspacing=0 border=0><tr><td colspan=3>"+aRay[0]+"</td></tr><tr><td></td><td>"+
				"</td></tr><tr><td align='left'>Airport Access Fee:</td><td>$"+CurrencyFormatted(busFee)+				
				" </td></tr><tr><td>Sub Total:</td><td>"+CurrencyFormatted(subTotal)+
				"</td></tr><tr><td col='2'><font  color='#FF0000'><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You Save $"+
				CurrencyFormatted(costType)+				
				"!!</td></tr><tr><td align='left'>Grand Total:</td><td>$"+(CurrencyFormatted(grandTotal))+
				"</td></tr></tr></table>";
		}
		else{
			outputStr = "<table align=left width=200 cellpadding=0 cellspacing=0 border=0><tr><td colspan=3>"+aRay[0]+"</td></tr><tr></tr><tr><td align='left'>Total Hours:</td><td>"+totalHours+
				"</td></tr><tr><td align='left'>Number of days:</td><td><!--totalDays"+totalDays+"-->"+totalDays+
				"</td></tr><tr><td align='left'>Total:</td><td>$"+CurrencyFormatted(totalDayCost)+
				"</td></tr><tr><td align='left'>Airport Access Fee:</td><td>$"+CurrencyFormatted(busFee);
			outputStr = outputStr+"</td></tr><tr><td align='left'>Grand Total:</td><td>$"+(CurrencyFormatted(grandTotal))+
				"</td></tr></tr></table>";
		}//thisPage.indexOf(testStr)
		document.frmResRequest.totalCost.value = CurrencyFormatted(grandTotal);
		document.getElementById("divTotalCost").innerHTML = outputStr;
		//document.getElementById("divTotalCost").style.backgroundColor = '#ff0000';
	}
   else {
     document.getElementById("divTotalCost").innerHTML = '';
     document.getElementById("divTotalCost").innerHTML = aRay[0];
   }//if(!isNaN(totalDayCost) && !isNaN(grandTotal)){
   
  }	
 }//EOF
