/* 	OK, the new sweeps form has a radio button instead
	of a checkbox for "I haven't set a date yet." That
	means it needs its own copy of the geotargeting
	validation, adjusted to work with the checkbox.
*/

function validateDate()
{		
		var xmlHttpReq = false;
		var self = this;
		var strUrl = '/xCheckDate.php';

		// Mozilla/Safari
		if (window.XMLHttpRequest) 
		{
			self.xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) 
		{
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}

		self.xmlHttpReq.open('POST', strUrl, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() 
		{
			if (self.xmlHttpReq.readyState == 4) 
			{
				updatepage(self.xmlHttpReq.responseText);
			}
		}
		self.xmlHttpReq.send(getquerystring());
}

function getCookieValue (cookieName) 
{
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) 
  {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

function getquerystring() 
{
    var form     = document.forms['one'];
    var city = form.Location_City.value;
    var state = form.Location_State.value;
    var month = form.weddingmonth.value;
    var day = form.weddingdate.value;
    var year = form.weddingyear.value;
    var page = window.location.pathname;
	var session = getCookieValue('PHPSESSID');

	var now = new Date();
	now = now.getTime();

	if (document.one.NoDateCheckbox[1].checked)
	{
		
	    qstr = 'session=' + session + '&page=' + page + '&month=' + escape(month) + '&day=' + escape(day) + '&year=' + escape(year) + '&city=' + escape(city) + '&state=' + escape(state) + '&NoDateCheckbox=on&rand=' + now;  // NOTE: no '?' before querystring	
	}
	else
	{
	    qstr = 'session=' + session + '&page=' + page + '&month=' + escape(month) + '&day=' + escape(day) + '&year=' + escape(year) + '&city=' + escape(city) + '&state=' + escape(state) + '&rand=' + now;  // NOTE: no '?' before querystring
	}
	
	return qstr;
}

function extractResponse(element, xml)
{
	var searchResponse = '<' + element + '>';
	var searchResponse2 = '</' + element + '>';
	return xml.substring( xml.indexOf(searchResponse) + searchResponse.length, xml.indexOf(searchResponse2) );
}


function updatepage(str)
{
	// rip out response
	var searchResponse = '<response>';
	var searchResponse2 = '</response>';
	var response = str.substring( str.indexOf(searchResponse) + searchResponse.length, str.indexOf(searchResponse2) );
    document.getElementById("result").innerHTML = response;
	if ( response == 'true' )
	{
		// successful response submit the form to eloqua and redirect to thankyou page
		document.forms['one'].Market.value = extractResponse('market', str);
		
			// Set cookies for city/state
		var date = new Date();
		date.setTime(date.getTime()+(60*60*24*30*24));
		var expires = "; expires="+date.toGMTString();
		
		var form     = document.forms['one'];
    	var city = form.Location_City.value;
    	var state = form.Location_State.value;
		var market = extractResponse('market', str);
		document.cookie = 'city'+"="+city+expires+"; path=/";
		document.cookie = 'state'+"="+state+expires+"; path=/";
		document.cookie = 'market'+"="+market+expires+"; path=/";
		
		document.forms['one'].Rating.value = '';				    		    		
		document.forms['one'].Market.value = extractResponse('market', str);	
		
		
		document.forms['one'].submit();
		return;
	}

	var status = extractResponse('status', str);
	if ( status == 'declineddate' )
	{
		// failed date submit the form to eloqua and redirect to no service closed date page
		document.forms['one'].Rating.value = 'Sold Out Date';				    		    		
		document.forms['one'].Market.value = extractResponse('market', str);		
		document.forms['one'].submit();
		return;
	}

	// out of market decline
	var searchStatus = '<status>';
	var searchStatus2 = '</status>';
	var status = str.substring( str.indexOf(searchStatus) + searchStatus.length, str.indexOf(searchStatus2) );
	if ( status == 'declinedmarket' )
	{
		document.forms['one'].Market.value = 'OOM-na';
		document.forms['one'].Rating.value = 'Declined';				    		    
		document.forms['one'].submit();	    	
		return;
	}
	
	// rip out error message
	var message = extractResponse('message', str);
	var errors = message.split(';');

	var num=0;
	var errorMessage = '';
	while (num < errors.length)
 	{
		errorMessage += errors[num] + '<br>';
		num+=1;
	}
    document.getElementById("error").innerHTML = errorMessage;
    
	// rip out nearest match
	var match = extractResponse('nearestMatch', str);
	var matches = match.split(';');

	if ( matches.length > 0 )
	{
		var num=0;
		var nearestMatch = '';		
		while (num < matches.length)
	 	{
	 		if ( matches[num].length > 0 )
	 		{
				nearestMatch += matches[num] + '? ';
	 		}
			num+=1;	 		
		}
		
		if ( nearestMatch.length > 0 )
		{
			// Display nearest match
		    document.getElementById("nearestMatch").innerHTML = nearestMatch = 'Did you mean ' + nearestMatch;
		}
		else
		{
			// Submit the page to eloqua and redirect to service declined page 
		    document.getElementById("nearestMatch").innerHTML = 'Unable to locate city, please check spelling.';
		}
	}    
 }
 
 
 // End geotargeting
 
 function setMailCookie() {
		var cookieName = "thankYouPageEmail";
		var cookieValue = document.one.email.value;
		var today = new Date();
		var expire = new Date();
		var nDays=365;
		expire.setTime(today.getTime() + 3600000*24*nDays);
		document.cookie = cookieName+"="+escape(cookieValue)
		+ ";expires="+expire.toGMTString();
}
 
 function validateForm(one)
{
if(""==document.forms.one.First_Name.value)
	{
		alert("Please enter your first name.");
		 
		return false;
	}
	
	if(""==document.forms.one.Last_Name.value)
	{
		alert("Please enter your last name.");
		
		return false;
	}
	
if(""==document.forms.one.address1.value)
	{
		alert("Please enter your street address.");
		
		return false;
	}

	if(""==document.forms.one.City.value)
	{
		alert("Please enter your city.");
		
		return false;
	}
	

	if(document.forms.one.State.selectedIndex == 0)
		{
		alert("Please select your state.");
		
		return false;
	}
	
	if(""==document.forms.one.Zip.value)
	{
		alert("Please enter your ZIP code.");
		
		return false;
	}
	
if(""==document.forms.one.email.value)
	{
		alert("Please enter your e-mail address.");
		
		return false;
	}

if(""==document.forms.one.Phone1_1.value)
	{
		alert("Please enter a valid phone number where we can contact you.");
		
		return false;
	}

	if(""==document.forms.one.Area_Code1.value)
	{
		alert("Please enter a valid area code where we can contact you.");
		
		return false;
	}
	
	if(""==document.forms.one.Phone1_2.value)
	{
		alert("Please enter a valid phone number where we can contact you.");
		
		return false;
	}
	
	if (	(eval("document.forms.one.NoCeremonyCheckbox[0].checked") == false) && (eval("document.forms.one.NoCeremonyCheckbox[1].checked") == false)	) 
	{
		alert("Please tell us if you have chosen a location for your ceremony.");		
		return false;
	}
	if (	(eval("document.forms.one.NoCeremonyCheckbox[0].checked") == true) ) 
	{
		if(""==document.forms.one.Ceremomy_venue.value)
		{
			alert("Please enter a ceremony venue.");			
			return false;
		}
	}
	
	
	if (	(eval("document.forms.one.NoReceptionCheckbox[0].checked") == false) && (eval("document.forms.one.NoReceptionCheckbox[1].checked") == false)	) 
	{
		alert("Please tell us if you have chosen a location for your reception.");
		return false;
	}
	if (	(eval("document.forms.one.NoReceptionCheckbox[0].checked") == true) ) 
	{
		if(""==document.forms.one.recvenue.value)
		{
			alert("Please enter a reception venue.");			
			return false;
		}
	}

	if(""==document.forms.one.Location_City.value)
	{
		alert("Please enter the city your wedding will be in.");
		
		return false;
	}
	


	
	if(""==document.forms.one.Location_City.value)
	{
		alert("Please enter the city your wedding will be in.");
		
		return false;
	}
	else
	{
		// remove ", CA" state typos
		if ( document.forms.one.Location_City.value.indexOf(",") != -1 )
		{
			pos = document.forms.one.Location_City.value.indexOf(",");
			document.forms.one.Location_City.value = document.forms.one.Location_City.value.substr(0, pos);
			alert(document.forms.one.Location_City.value);
		}	
	}



if("State"==document.forms.one.Location_State.value)
	{
		alert("Please enter the state your wedding will be in.");
		
		return false;
	}
	
	if(""==document.forms.one.Location_City.value)
	{
		alert("Please enter the city your wedding will be in.");
		
		return false;
	}
	else
	{
		// replace any wierd city names like st louis to saint louis.
		document.forms.one.Location_City.value = fixWeirdCity(document.forms.one.Location_City.value);
	}


if(document.forms.one.C_Interested_in_Services1.selectedIndex == 0)
	{
		alert("Please tell us what services you are interested in.");
		
		return false;
	}
	

	

	var realname=document.forms.one.First_Name.value + " " + document.forms.one.Last_Name.value;
	
	document.forms.one.realname.value=realname;
	
	var address=document.forms.one.address1.value + " " + document.forms.one.address2.value;
	
	document.forms.one.address.value=address;
	
	var Phone_Number=document.forms.one.Area_Code1.value + "-" + document.forms.one.Phone1_1.value + "-" + document.forms.one.Phone1_2.value;
	
	document.forms.one.Phone_Number.value=Phone_Number;
	
	var Other_Phone=document.forms.one.Area_Code2.value + "-" + document.forms.one.Phone2_1.value + "-" + document.forms.one.Phone2_2.value;
	
	document.forms.one.Other_Phone.value=Other_Phone; 

	if(!document.one.NoDateCheckbox[1].checked) {
		if(""==document.forms.one.weddingmonth.value)
		{
			alert("Please enter your wedding month.");
			return false;
		}
		if(""==document.forms.one.weddingdate.value)
		{
			alert("Please enter your wedding date.");
			return false;
		}
			if(""==document.forms.one.weddingyear.value)
		{
			alert("Please enter your wedding year.");	
			return false;
		}

		if( checkDate(document.forms.one.weddingmonth.value, document.forms.one.weddingdate.value, document.forms.one.weddingyear.value) )
		{
			if( isInPast(document.forms.one.weddingmonth.value, document.forms.one.weddingdate.value, document.forms.one.weddingyear.value) )
			{
				alert("Your wedding date is in the past.");
				return false;			
			}
		} 
		else
		{
			alert("Please enter a valid date.");	
			return false;
		}
	}

	var Wedding_Date=document.forms.one.weddingmonth.value + "/" + document.forms.one.weddingdate.value + "/" + document.forms.one.weddingyear.value;

	document.forms.one.Wedding_Date.value=Wedding_Date;
	
	var Account_Name=realname + ": " + Wedding_Date;
	
	document.forms.one.Account_Name.value=Account_Name;
	
	var Location=document.forms.one.Location_City.value + document.forms.one.Location_State.value;
	
	document.forms.one.Location.value=Location;

	setMailCookie();
	
	validateDate();
}

function NoDateCheck(){

if (document.one.NoDateCheckbox[1].checked) {
		document.one.weddingmonth.value = "";
		document.one.weddingmonth.style.backgroundColor="#d9e2f3";
		document.one.weddingmonth.style.color="#d9e2f3";
		document.one.weddingmonth.disabled=true;
		document.one.weddingdate.value = "";
		document.one.weddingdate.style.backgroundColor="#d9e2f3";
		document.one.weddingdate.style.color="#d9e2f3";
		document.one.weddingdate.disabled=true;
		document.one.weddingyear.value = "";
		document.one.weddingyear.style.backgroundColor="#d9e2f3";
		document.one.weddingyear.style.color="#d9e2f3";
		document.one.weddingyear.disabled=true;
		document.one.weddingNoDate.value="NoWeddingDateYet";		
		}
	else {
		document.one.weddingmonth.value = "";
		document.one.weddingmonth.style.backgroundColor="#FFFFFF";
		document.one.weddingmonth.style.color="#000000";
		document.one.weddingmonth.disabled=false;
		document.one.weddingdate.value = "";
		document.one.weddingdate.style.backgroundColor="#FFFFFF";
		document.one.weddingdate.style.color="#000000";
		document.one.weddingdate.disabled=false;
		document.one.weddingyear.value = "";
		document.one.weddingyear.style.backgroundColor="#FFFFFF";
		document.one.weddingyear.style.color="#000000";
		document.one.weddingyear.disabled=false;
		document.one.weddingNoDate.value = "";	
		}
}


function NoCeremonyCheck(){
	if (document.one.NoCeremonyCheckbox.checked) {
		document.one.Ceremomy_venue.value = "No_Ceremony_Venue_Yet";
		document.one.Ceremomy_venue.style.backgroundColor="#d9e2f3";
		document.one.Ceremomy_venue.disabled=true;
	}
	else {
		document.one.Ceremomy_venue.value = "";
		document.one.Ceremomy_venue.style.backgroundColor="#FFFFFF";
		document.one.Ceremomy_venue.disabled=false;
	}
}

function NoReceptionCheck(){
	if (document.one.NoReceptionCheckbox.checked) {
		document.one.Reception_venue.value = "No_Reception_Venue_Yet";
		document.one.Reception_venue.style.backgroundColor="#d9e2f3";
		document.one.Reception_venue.disabled=true;
	}
	else {
		document.one.Reception_venue.value = "";
		document.one.Reception_venue.style.backgroundColor="#FFFFFF";
		document.one.Reception_venue.disabled=false;
	}
}

var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function checkDate(month, day, year)
{
	day = parseInt(day);
	month = parseInt(month);
	year = parseInt(year);
	
	if (!day || !month || !year)
		return false;

	if (year/4 == parseInt(year/4))
		monthLength[1] = 29;

	if (day > monthLength[month-1])
		return false;

	monthLength[1] = 28;

	var now = new Date();
	now = now.getTime(); //NN3

	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();

	var futureDate = (now < checkDate);
	var pastDate = (now > checkDate);

	return true;
}

function isInPast(month, day, year)
{
	day = parseInt(day);
	month = parseInt(month);
	year = parseInt(year);

	var now = new Date();
	now = now.getTime();

 	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	
	var checkDate = dateToCheck.getTime();

	var futureDate = (now < dateToCheck);
	var pastDate = (now > dateToCheck);

	return pastDate;
}

function isWithinDays(month, day, year)
{
	day = parseInt(day);
	month = parseInt(month);
	year = parseInt(year);

	var today = new Date();
	today.setDate(today.getDate() + 30);

	var futureDate = today.getTime();
	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();
	
	return checkDate < futureDate;
}