monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
now = new Date;
newDate = new Date;
newDate.setDate(now.getDate());
newDate.setMonth(now.getMonth());
newDate.setYear(now.getFullYear());


var myDate = new Date;

	

function populateDays(monthChosen)
{
temp_day=0;
temp_day =document.quoteRequest.optDays.selectedIndex;
//alert(document.getElementById('optDays').selectedIndex) ;

	monthStr=document.quoteRequest.optMonths.value ;
	if(monthStr!="")
	{
		var theMonth=parseInt(monthStr);
	}
	document.quoteRequest.optDays.options.length = 0;
	if(isLeapYear(document.quoteRequest.optYears.value))
	{
   		monthDays[1]=29;
	} 
	else
	{
		monthDays[1]=28;
	}
	for(i=0;i<monthDays[theMonth-1];i++)
	{
		document.quoteRequest.optDays.options[i] = new Option(i+1);
	}
document.quoteRequest.optDays.selectedIndex =temp_day;

}
function isLeapYear (year) 
{
	return (year % 4 == 0) && 
		   ((year % 100 != 0) || (year % 400 == 0));
}
function setForm()
{
	document.quoteRequest.optMonths.value = now.getMonth()+1 ;
	document.quoteRequest.optYears.value = now.getFullYear();
	populateDays(document.quoteRequest);
	document.quoteRequest.optDays.selectedIndex = now.getDate();
}

var CheckList = new Array() 
function Check(errorMessage, FormValue, Type) 
{
	this.errorMessage = errorMessage;
	this.FormValue = FormValue;
	this.Type = Type;
} 


function checkQuoteForm(form)
{
	var errorName, errorMail, errorDate, c_of_o_error,other_tel_error;
	var url = this.location.href;
	index1 = url.lastIndexOf("/");
	index2 = url.lastIndexOf("?");
	stLength = url.length;

	var hotelUrlString = /Hotel/;
	pageName = url.substring(index1 + 1,index2);

	//if(pageName=="mymapofhotel.php"|pageName=="mymapofhoteltest.php"|pageName == "special_offers.html")
	if(hotelUrlString.test(url)|pageName == "special_offers.html")
	{
		myDate.setDate(eval("document."+form+"."+"optDays.selectedIndex")+1);
		myDate.setMonth(eval("document."+form+"."+"optMonths.value")-1);
		myDate.setYear(eval("document."+form+"."+"optYears.value"));	

	}

	var email2 = eval("document."+form+".Email2.value");
	num=CheckList.length;
	var invalidChar=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var invalidNumber=/^(\+? {0,}\d{2} {0,})?(\({1}\d{1,}\){1} {0,})?([0-9 {0,}])+$/;
	
	for (i=0; i<=CheckList.length-1; i++) 
	{
		var fieldvalue = CheckList[ i ].FormValue;
		var fieldname = CheckList[ i ].errorMessage;
		var type = CheckList[ i ].Type;
		if(type!="dateOpted")
		{
			stringValue = eval("document."+form+"."+fieldvalue+".value");
		}
		if (type=="email")
		{
			if(!invalidChar.test(stringValue))
			{
				errorMail="Please note: These fields must contain valid email addresses";
			}				
			else if(stringValue!=email2)
			{
				
				errorMail="Please note: Both email addresses must match.";
			}
			else
			{
				errorMail="";
			}
		}
		if (type=="userfName")
		{			
			if(stringValue.length<2)
			{
				errorName="Please note: Both these fields must contain valid names";			
			}				
			else
			{
				errorName="";
			}
		} 
		if (type=="usersName" & errorName=="")
		{				
			if(stringValue.length<2)
			{
				errorName="Please note: Both these fields must contain valid names";			
			}				
			else
			{
				errorName="";
			}
		} 
		if (type=="dateOpted")
		{

			if(myDate.getTime()<=newDate.getTime())
			{			
				errorDate="Please note: Arrival date must be at least 1 day from enquiry date!";
			}
			else
			{

				errorDate="";
			}
		}


		if (type=="tel")
		{
			if(stringValue.length>5)
			{
		
				stringV =stringValue;//.replace(/[^\d]+/g,"");
				if(!invalidNumber.test(stringV))
				{
					telError="Please Note: A valid telephone number is required";
				}
				else
				{
					telError="";
				}
			}
			else
			{
				telError="Please Note: A valid telephone number is required";

			}
		}
		if(type=="other_tel")
		{
			if(stringValue.length>5)
			{
				stringV =stringValue//.replace(/[^\d]+/g,"");

				if(!invalidNumber.test(stringV))
				{
					other_tel_error="Please Note: A valid telephone number is required";
				}
				else
				{
					other_tel_error="";
				}
			}
			else if(!stringValue.length<5 & stringValue.length >0)
			{
				other_tel_error = "Invalid telephone number.  Please either clear the  field or enter a valid number"
			}
			else
			{
				other_tel_error =""
			}
		}	
		if(type == "countryoforigin")
		{
			if(stringValue=='Which country are you enquiring from?')
			{
				c_of_o_error = "Please choose the correct country you are enquiring from as this will enable us to call you at a resonable time.";
			}	
			else
			{
				c_of_o_error="";
			}
		}
	


	}
	if (errorMail!="" | errorName!="" | errorDate!="" |telError!=""|c_of_o_error!=""|other_tel_error!="")
	{
			nameErr.innerHTML = errorName;
			emailErr.innerHTML = errorMail;
			dateBox.innerHTML = errorDate;
			telErr.innerHTML = telError;
			c_o_error.innerHTML = c_of_o_error;
			other_t_error.innerHTML = other_tel_error;
		return false;
	}
	else
	{
		return true;
	}
}
