String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");}
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

// date validation functions
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 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 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 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 isDate(dtStr,showAlert)
{
	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)
		{
		if(showAlert)
			alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12)
		{
		if(showAlert)
			alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{
		if(showAlert)
			alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
		{
		if(showAlert)
			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)
		{
		if(showAlert)
			alert("Please enter a valid date");
		return false;
	}
return true;
}

function isValidDate(dateStr) 
	{
	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[1]; // parse date into variables
	day = matchArray[3];
	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;
}

// end of date validation functions
function IsDateWithinAYear(ticks)
{
// ticks variable will accepts date and later converts to ticks 
	try
	{
		var todayDate=new Date();
		//ticks=Date.parse(ticks);
		ticks=ticks-todayDate;
		ticks=parseInt(ticks);
		ticks=ticks/1000;
		ticks=ticks/60;
		ticks=ticks/60;
		ticks=ticks/24;
		if (ticks>365)
		{		
			return false;
		}
		else
		{		
			return true;
		}
	}
	catch (err)
	{
		return false;
	}
}
	function CheckFormat()
	{				
		var warningMessage="";
		var canSubmit=new Boolean();
		var todayDate=new Date();
		var formField1;
		var destinationCity;
		var departCity;
		var fieldPattern;
		var adult;
		var totalNoOfPassengers;
		var senior;
		var infantinLap;
		var infantOnSeat;
		var child;
		canSubmit=false;
		
		// Field #1
		departCity=document.getElementById('_txtDepartCity').value;
		departCity=departCity.trim();
		document.getElementById('_txtDepartCity').value=departCity;
		fieldPattern=/^[a-zA-Z\s]*$/;				
		if (departCity.length>0 && fieldPattern.test(departCity))
		{					
			if (departCity.length==3)
			{
				canSubmit=true;
			}
			else
			{
				canSubmit=false;
				warningMessage="Departure City Code length must be of three characters\n";
			}	
		}
		else
		{
			warningMessage="Please Enter Valid Input in Departure City.\n";
			canSubmit=false;
		}
		// Field #2
		destinationCity=document.getElementById('_txtDestinationCity').value;
		destinationCity=destinationCity.trim();
		document.getElementById('_txtDestinationCity').value=destinationCity;
		fieldPattern=/^[a-zA-Z\s]*$/;
		if (destinationCity.length>0)
		{				
			if (departCity.toLowerCase()!=destinationCity.toLowerCase())
			{					
				if (fieldPattern.test(destinationCity))
				{
					if (destinationCity.length==3)
					{
						canSubmit=(canSubmit==false)?false:true;
					}
					else
					{
						canSubmit=false;
						warningMessage+="Destination City Code length must be of three characters\n";
					}							
				}
				else
				{
					warningMessage+="Please Enter Valid Input in Destination City.\n";
					canSubmit=false;
				}
			}
			else
			{
				if (departCity!="" && destinationCity!="")
				{
						warningMessage+="Depart City and Destination City Cannot be Same.\n";
				}
				canSubmit=false;
			}
		}
		else
		{
				warningMessage+="Please Enter Valid Input in Destination City.\n";
				canSubmit=false;
		}
		// Field #3
		formField=document.getElementById('_txtReturnFromDifferentCity').value; // return from different
		formField=formField.trim();
		document.getElementById('_txtReturnFromDifferentCity').value=formField;
		fieldPattern=/^[a-zA-Z\s]*$/;				
		if ( formField.length>0)
		{
			if ( fieldPattern.test(formField))
			{
				// check for depart == returning from case here
				if (departCity.toLowerCase()==formField.toLowerCase())
				{
					canSubmit=false;
					warningMessage+="Depart City Cannot Be Same If You Are Returning from Different City.\n";
				}
				// check for destination == returning from case here
				else if (destinationCity.toLowerCase()==formField.toLowerCase())
				{
					canSubmit=false;
					warningMessage+="Destination Cannot Be Same If You Are Returning from Different City.\n";
				}
				else if (formField.length!=3)
				{
					canSubmit=false;
					warningMessage+="Return From Different City Code Length must be of three characters.\n";
				}
			}
			else
			{
				warningMessage+="Please Enter Valid Input in Return From Different City.\n";
				canSubmit=false;
			}				
		}		

		// Field #4
		formField=document.getElementById('_txtDepartDate').value;
		formField=formField.trim();
		try
		{
			if (isDate(formField))
			{
				formField=Date.parse(formField);						
				if (todayDate<formField)
				{	
					if (IsDateWithinAYear(formField))
					{
						canSubmit=(canSubmit==false)?false:true;
					}
					else
					{
						warningMessage+="Departure Date must be within a year from the current date.\n";
						canSubmit=false;
					}												
				}
				else
				{
					warningMessage+="Departure Date must be Greater than Today.\n";
					canSubmit=false;
				}
			}
			else
			{
					warningMessage+="Please Enter a valid Departure Date in mm/dd/yyyy format.\n";
					canSubmit=false;
			}
		}
		catch(err)
		{
			warningMessage+="Please Enter Valid Input in Departure Date.\n";
			canSubmit=false;
		}
		// Field #5
		formField1=document.getElementById('_txtReturnDate').value; // for validating return date
		formField1=formField1.trim();		
		if (formField1.length>0)
		{
			try
			{
				if (isDate(formField1))
				{
					formField1=Date.parse(formField1);
					if (todayDate<formField1)
					{							
						if (formField<formField1)
						{
							if (IsDateWithinAYear(formField1))
							{
								canSubmit=(canSubmit==false)?false:true;	
							}
							else
							{
								canSubmit=false;
								warningMessage+="Return Date must be within a year from the current date.\n";	
							}
											
						}
						else
						{
							canSubmit=false;
							warningMessage+="Return Date must be Greater than Depart Date.\n";								
						}
					}
					else
					{
						canSubmit=false;
						warningMessage+="Return Date must be Greater than Today.\n";							
					}
				}
				else
				{
						canSubmit=false;
						warningMessage+="Please enter a valid Return Date in mm/dd/yyyy format.\n";							
				}
				
			}
			catch(err)
			{
				canSubmit=false;
				warningMessage+="Please Enter Valid Input in Return Date.\n";					
			}
		}
		
		// Field Adults+Seniors+Children+InfantLap+InfantSeat
		fieldPattern=/^[\d]*$/;

		adult=document.getElementById('_txtAdults').value; // adult
		document.getElementById('_txtAdults').value=adult.trim();
		adult=(adult=="")?"0":adult.trim();
		
		senior=document.getElementById('_txtSeniors').value
		document.getElementById('_txtSeniors').value=senior.trim();
		senior=(senior=="")?"0":senior.trim();

		if ( fieldPattern.test( adult ) &&  fieldPattern.test( senior ) )
		{
			adult=parseInt(adult);
			senior=parseInt(senior);
			if (adult+senior==0)
			{
				canSubmit=false;
				warningMessage+="An Adult or a Senior Passenger must be there in Group Travel.\n";
			}
			else 
			{
				fieldPattern=/^[\d]*$/;
				formField =document.getElementById('_txtChilds').value; 
				formField=formField.trim();
				document.getElementById('_txtChilds').value=formField;
				formField=(formField=="")?"0":formField;

				if (fieldPattern.test( formField )==false )
				{
					canSubmit=false;
				}
				child=parseInt(formField);
				if (child<0)
				{
					canSubmit=false;
					warningMessage+="Please enter valid input in child .\n";
				}

				formField = document.getElementById('_txtInfantInLap').value;
				formField=formField.trim();
				document.getElementById('_txtInfantInLap').value=formField;
				formField=(formField=="")?"0":formField;

				if (fieldPattern.test( formField )==false )
				{
					canSubmit=false;
				}
				infantinLap=parseInt(formField);

				if (infantinLap<0)
				{
					canSubmit=false;
					warningMessage+="Please enter valid input in InfantOnLap .\n";
				}
				formField = document.getElementById('_txtInfantOnSeat' ).value;
				formField=formField.trim();
				document.getElementById('_txtInfantOnSeat').value=formField;
				formField=(formField=="")?"0":formField;

				if (fieldPattern.test( formField )==false )
				{
					canSubmit=false;
				}
				infantOnSeat=parseInt(formField);
				if (infantOnSeat<0)
				{
					canSubmit=false;
					warningMessage+="Please enter valid input in InfantOnSeat .\n";
				}
			}					
		}
		else
		{
				canSubmit=false;
				warningMessage+="Please enter valid input in Adult or Senior.\n";
		}

		totalNoOfPassengers=adult+senior+infantinLap+infantOnSeat+child;			

		if (canSubmit==true &&  (totalNoOfPassengers<10 || totalNoOfPassengers>100))
		{
			canSubmit=false;
			warningMessage+="Total Number of Passengers must be greater than or equal to 10 and less than or equal to 100 for Group Travel request.\n";
		}
		
		// Field #9				
		formField=document.getElementById('_txtFirstName').value;
		formField=formField.trim();
		document.getElementById('_txtFirstName').value=formField;
		fieldPattern=/^[a-zA-Z\s]*$/;
		if (fieldPattern.test(formField) && formField.length>0)
		{
			canSubmit=(canSubmit==false)?false:true;
		}
		else
		{
			canSubmit=false;
			warningMessage+="Please enter a valid Input in First Name.\n";
		}

		// Field #10				
		formField=document.getElementById('_txtLastName').value;
		formField=formField.trim();
		document.getElementById('_txtLastName').value=formField;
		fieldPattern=/^[a-zA-Z\s]*$/;
		if (fieldPattern.test(formField) && formField.length>0)
		{
			canSubmit=(canSubmit==false)?false:true;
		}
		else
		{
			canSubmit=false;
			warningMessage+="Please enter a valid Input in Last Name.\n";
		}
		
		// Field #11
		formField=document.getElementById('_txtPhone').value;
		formField=formField.trim();
		document.getElementById('_txtPhone').value=formField;
		fieldPattern=/^[0-9]*$/;
		if (fieldPattern.test(formField) && formField.length>=10 && formField.length<15)
		{
			canSubmit=(canSubmit==false)?false:true;
		}
		else
		{
			canSubmit=false;
			warningMessage+="Please Enter a Valid Input in Phone.\n";
		}
		
		// Field #12
		formField=document.getElementById('_txtEmail').value;
		formField=formField.trim();
		document.getElementById('_txtEmail').value=formField;
		fieldPattern=/^[\w-\.]+@([\w-]+\.)+[\w-]{2,3}$/;
		if (fieldPattern.test(formField) && formField.length>0)
		{
			canSubmit=(canSubmit==false)?false:true;
		}
		else
		{
			canSubmit=false;
			warningMessage+="Please Enter Valid Input in Email.\n";
		}
		if(warningMessage.length>0)
		{
		   alert(warningMessage);
		}
		return canSubmit;
	}
function ConstructElement()
{
var parentElement=document.getElementById('formMail');
var newNode=document.createElement('input');
newNode.setAttribute("type","hidden");
newNode.setAttribute("value","true");
newNode.setAttribute("id","jsalert");
newNode.setAttribute("name","jsalert");
parentElement.appendChild(newNode);
}
function getAirportCode(theTagID)
{
	var strUrl = "/DesktopModules/Fareportal/Modules/Flight/AirportCode.aspx?frm=Form&textbox=" + theTagID;
	window.open(strUrl,"AirportPicker","toolbar=no,status=no,resizable=no,scrollbars=yes,width=560,height=900,top=100,left=100");
}