// JavaScript Document
function setRates(){
	document.theForm.room.value =135;
	getSum();
}
function setRated(){
	document.theForm.room.value =190;
	getSum();
}
function getEnabled(){
		document.theForm.accomindate.disabled=false;
		document.theForm.accomoutdate.disabled=false;
}

function getdisable_promo(){
	document.theForm.promocode.value = '';
	document.theForm.promofee.value= '0';
	document.theForm.promofee.disabled=true;
	document.theForm.promocode.disabled=true;
	}

function geten_promo(){
	document.theForm.promofee.disabled=false;
	document.theForm.promocode.disabled=false;
	//document.theForm.promofee.value=1500;
	//document.theForm.regfee.value=1500;
	getSum();
	}
	
function set_promo(){
	document.theForm.regfee.value = document.theForm.promofee.value;
	}	

function getDisabled(){
		document.theForm.accomindate.disabled=true;
		document.theForm.accomoutdate.disabled=true;
		document.theForm.accommodation.value =0;
		document.theForm.accomindate.value="";
		document.theForm.accomoutdate.value="";
		getAll();
	}


function isValidDate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[1];
year = matchArray[4];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}


function getSum(){
str = '' 
tag = document.getElementsByTagName("INPUT") ;
var tot =0;
	for(x=0;x<tag.length;x++)
		{ 
			  if(tag[x].getAttribute('type') == "radio")
			  { 
					 if((tag[x].getAttribute('name').indexOf('f') != -1) && (tag[x].checked == true))
					 { 
									tot += eval(tag[x].getAttribute('value'));
									switch (tag[x].getAttribute('name'))
									{
										case "faccom":
											tot += eval(theForm.accommodation.value );
											break;
										case "fvisa":
											document.theForm.visafee.value = eval(tag[x].getAttribute('value'));
											break;
										case "fees":
											 if(tag[x].getAttribute('value')=="1600"){
												if(eval(document.theForm.promofee.value)==0)
												{
													document.theForm.promofee.value=1600;
													document.theForm.regfee.value =1600;
												}
												else
												{
													document.theForm.regfee.value = document.theForm.promofee.value;
												}
											}
											 else{
													document.theForm.regfee.value = eval(tag[x].getAttribute('value'));
													getdisable_promo(); 
												 }
										break;		 
									}
						getAll();	//document.theForm.regfee.value = tot;
					 } 
			  } 
		} 
}   

//=======================================================================================
function getAll(){
//	getSum();
	var i= eval(document.theForm.room.value);
	document.theForm.totalfees.value =  
	eval(document.theForm.visafee.value) + 
	eval(document.theForm.regfee.value) +  
	eval(parseInt(document.theForm.accommodation.value)*parseInt(document.theForm.room.value));
	
	document.theForm.accommfee.value = 
	eval(parseInt(document.theForm.accommodation.value)*parseInt(document.theForm.room.value));	
}

function set_regfee(){
	document.theForm.regfee.value = eval(document.theForm.promofee.value);
	getAll();
	}

function dateDiff() {

date1 = new Date();
date2 = new Date();
diff  = new Date();

var days =0;

//if(document.theForm.faccom.checked == true){
	if (isValidDate(document.theForm.accomindate.value)) { 

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

		var matchArray = (document.theForm.accomindate.value).match(datePat); // is the format ok?
		month = matchArray[3]; // parse date into variables
		day = matchArray[1];
		year = matchArray[4];
		var dt1 = (month + "/" + day + "/" + year);	
		document.theForm.sdt.value = dt1 ;	
		date1temp = new Date(document.theForm.sdt.value + " " + "12:00 AM");
		date1.setTime(date1temp.getTime());
		}
		else return false; // otherwise exits
	
	if (isValidDate(document.theForm.accomoutdate.value)) { 
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
		var matchArray = (document.theForm.accomoutdate.value).match(datePat); // is the format ok?
		month = matchArray[3]; // parse date into variables
		day = matchArray[1];
		year = matchArray[4];
		var dt2 = (month + "/" + day + "/" + year);		
		document.theForm.edt.value = dt2 ;
		date2temp = new Date(document.theForm.edt.value + " " + "12:00 AM");
		date2.setTime(date2temp.getTime());
	}
	else return false; // otherwise exits
	
	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
	timediff = diff.getTime();
	
	days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
//}	
	document.theForm.accommodation.value = days;
	getAll();
//return false; // form should never submit, returns false
}

function formCheck(theForm){
	var alertText = "Please check following form field(s):";
	var e = 0;
if (theForm.fname.value==""){alertText = alertText + "\nPlease enter First Name.";var e = 1;}
if (theForm.lname.value==""){alertText = alertText + "\nPlease enter Last Name.";var e = 1;}
if (theForm.country.value==""){alertText = alertText + "\nPlease select Country.";var e = 1;}
if (! theForm.useremail.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi))	
{alertText = alertText +"\nEmail must be valid email address.";var e = 1;}
if (theForm.userphone.value==""){alertText = alertText + "\nPlease enter Phone Number.";var e = 1;}
if (e!=0) {alert(alertText);return (false)}
         else 
		 {
			document.theForm.accommodation.disabled=false;
			document.theForm.totalfees.disabled=false;
			document.theForm.visafee.disabled=false;
			document.theForm.regfee.disabled=false;
			document.theForm.accommfee.disabled=false;
			document.theForm.promofee.disabled=false;
			document.theForm.promocode.disabled=false;
		 	return (true);
		 }
	}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
//Format Phone Number 9999999999 = (999)999-9999
//Call the function like so onKeyPress="javascript:formatPhone(this);
function formatPhone(objFormField){

	intFieldLength = objFormField.value.length;
      if(intFieldLength == 3){
            objFormField.value = "+" + objFormField.value + " ";
            return false;
            }
    if(intFieldLength >= 6 && intFieldLength <= 7){
        objFormField.value = objFormField.value + " ";
        return false;
        }
	if(intFieldLength >= 10 && intFieldLength <= 11){
        objFormField.value = objFormField.value + " ";
        return false;
        }
		return(VAM_KeyPress(objFormField, event));
}
