// JavaScript Document
function setCheckedValue(radioObj, newValue) {

	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function do_track(id)
{
   
   if(document.getElementById("mytrack").value =="" || document.getElementById("mytrack").value =="ticket id here") return false;
}


function checkseats(obj,num,adults,children)
{
  //alert("test");
   if(( adults  + children) > num)
   {
       obj.checked = false;
	   return false;
   }
  return true;

}

function clearPhone(value)
{
    var found=value.indexOf("X")
    if (found != -1)
	{
	    document.getElementById("contact_phone").value ="";
	}
}



function validate1()
{
   var obj = document.getElementById("RadioGroup1_1"); 
   var d1 = new Date(document.reservation1.year.value,document.reservation1.month.value-1,document.reservation1.day.value)
   var d2 = new Date(document.reservation1.year2.value , document.reservation1.month2.value-1, document.reservation1.day2.value)
   var today= new Date()
   
   
   var radioLength = obj.length;
   var checkvalue;
	if(radioLength == undefined)
		if(obj.checked)
			checkvalue= obj.value;
		else
			checkvalue= "";
	for(var i = 0; i < radioLength; i++) {
		if(obj[i].checked) {
			checkvalue= obj[i].value;
		}
	}


   if( d1 > d2 && checkvalue ==2)
   {
      document.reservation1.day.focus
      alert("Your return date cannot be earlier than your departure date!")
	  
	  return false
   }
   
   
  // if(d1 < today || d2 < today)
//   {
//      
//      document.reservation1.day.focus
//      alert("Invalid Dates")
//	  return false
//   }
   
   
   
   if(parseInt(document.reservation1.adults.value) == 0 && parseInt(document.reservation1.children.value)==0 && parseInt(document.reservation1.baby.value)==0 )	
   {
     alert("You must select the number and types of passengers for this flight!")
     document.reservation1.adults.focus();
	 return false
   }
   
    if((parseInt(document.reservation1.children.value) > 0 || parseInt(document.reservation1.baby.value)) && (parseInt(document.reservation1.adults.value)==0 ))
   {
     alert("Children and/or Babies must be accompanied by an Adult")
     document.reservation1.adults.focus();
	 return false
   }
   
   var num;
   num = parseInt(document.reservation1.adults.value) + parseInt(document.reservation1.children.value) + parseInt(document.reservation1.baby.value);
																												  
   
   if(num > 9)
   {
    
	  alert("The total number of passengers cannot exceed 9!")
     document.reservation1.adults.focus();
	 return false 
   }
   
   if(document.reservation1.origin.value == document.reservation1.destination.value)
   {
     alert("The origin and destination cannot be identical!")
     document.reservation1.origin.focus();
	 return false 
   
   }
   
   return true
}

function validate2(key)
{
	
  if(!document.reservation2.RadioGroup_DFlight)
		return false;
  var i = 1
  
  for(; i < document.reservation2.RadioGroup_DFlight.length; i++) {
		
		if(document.reservation2.RadioGroup_DFlight[i].checked == true) {
			break;
		}
  }
  
  if(i >= document.reservation2.RadioGroup_DFlight.length)
  {
    alert('Please select a departure flight')
	return false
  }
  
  if(key == 2)
  {
   
	     i=1
		 for(; i < document.reservation2.RadioGroup_RFlight.length; i++) {
		
		if(document.reservation2.RadioGroup_RFlight[i].checked == true) {
			break;
		}
        }
  
        if(i >= document.reservation2.RadioGroup_RFlight.length)
         {
	
	 alert('Please select a return flight')
	              return false
          }
  
  }
   return true;
}


function validate3()
{

  var response = confirm("Are you sure you want to book this ticket?");
  if(response == false) return false;
  
  for(i=0; i<document.reservation3.elements.length; i++)
    {
          
		  switch(document.reservation3.elements[i].type)
		  {
		  
		    case 'select-one':
			{
			    
				if(document.reservation3.elements[i].value=="X" && document.getElementById(document.reservation3.elements[i].name).disabled == false)
                {
                  //var name = document.reservation3.elements[i].name;
				  //var newname = name.replace(/_/g,' ');
				  //alert("Please enter "+newname);
			      //alert('Missing Information. Please Fill');
				  document.reservation3.elements[i].focus();
				  // alert(document.reservation3.elements[i].name);
			      return false;
               }
			}
			break;
			  case 'text':
			 {
			   
			  document.reservation3.elements[i].style.backgroundColor='white';
			   
			   if(document.reservation3.elements[i].name=="contact_email"   )
			   {
			   
			        if(checkEmail(document.reservation3.elements[i].value).length > 0)
				  {
				      alert(checkEmail(document.reservation3.elements[i].value));
					   document.reservation3.elements[i].focus();
					  return false;
				  }
			   }
				 
				 
				  if(document.reservation3.elements[i].name=="contact_phone"   )
			   {
			   
			       if(checkPhoneNumber(document.reservation3.elements[i].value).length > 0)
				  {
				      alert(checkPhoneNumber(document.reservation3.elements[i].value));
					   document.reservation3.elements[i].focus();
					  return false;
				  }
			   }
			   
			   
			   
			   
			   
			   if((document.reservation3.elements[i].name!="connection") && (document.reservation3.elements[i].value=="" && document.getElementById(document.reservation3.elements[i].name).disabled == false))
               {
                  
				   
				  //var name = document.reservation3.elements[i].name;
				  //var newname = name.replace(/_/g,' ');
				  //alert("Please enter "+newname);
				  document.reservation3.elements[i].style.backgroundColor='yellow';
			      document.reservation3.elements[i].focus();
			      //alert(document.reservation3.elements[i].name);
				  return false;
               }
			   
			 }
			 break;
			 
		  }
		  
	}
	
  return true;
}


function checkEmail(address) {
email = address
AtPos = email.indexOf("@")
StopPos = email.lastIndexOf(".")
Message = ""

if (AtPos == -1 || StopPos == -1) {
Message = "Not a valid email address"
}

if (StopPos < AtPos) {
Message = "Not a valid email address"
}

if (StopPos - AtPos == 1) {
Message = "Not a valid email address"
} 

return Message
}


function checkPhoneNumber(phone)
{
Message=""
    if(phone.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
   {
      Message = "The phone/fax number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx."
   }

	return Message
}

function checkDate(dstring)
{
    
	Message=""
	var mydate = new Date(dstring)
	var today = new Date()
	if (mydate < today)
	Message="Invalid Date Selection./nYour selected date must be later than today's date"
	return Message
}


function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.value = "";
		return false;
	}
}

