var defaultCityValue 	= 'City, State, or Zip Code';
var defaultCityValue2 	= 'Enter City, State, or Zip Code';
var defaultCityValue3   = 'Enter City, State, or Zip Code';
var defaultZipValue 	= '';
var defaultZipValue2 	= 'Zip Code';


var invalidZipText		= 'Please enter a 5 digit zip or valid postal code.';
var invalidCityZipText	= 'Please enter a City, State or ZIP / Postal Code.';
var invalidHousingType	= 'Please select a Housing Type.';

var requiredEmailText	= 'Email is required.';
var invalidEmailText	= 'Valid email is required.';   


if(
	location.href == 'http://www.seniorsforliving.com/' ||
	location.href == 'http://www.seniorsforliving.com/' ||
	location.href.substr(0, 41) == 'http://www.seniorsforliving.com/index.php' ||
	location.href.substr(0, 41) == 'http://www.seniorsforliving.com/index.php' ||
	location.href.substr(0, 50) == 'http://www.seniorsforliving.com/search-results.php' ||
	location.href == 'http://10.10.0.142/'
)
{
	$(document).ready(function() {
		$('#go_pop').click(function() {
			$("#zip").val($("#hczip").val());
			$("#city").val('');
			$("#state").val('');
			document.form1.submit();
		});
		$('#close_pop').click($.unblockUI);
	});
	$("#location_submit").click(function(){
		var zip = $("#zip").val();
		var state = $("#state").val();
		alert( zip + "\n" + state );
		if( ( zip == defaultZipValue || zip == "" ) && ( state == '' ) )
		{
			alert(invalidCityZipText);
		}
	});
}

//	Mailing List
//	Validates email format for the mailing list form
function validateReport()
{
	var mailingListForm = document.form2;
	var email			= mailingListForm.email.value;
	var regex			= /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	// Blank Email
	if( email == '' )
	{
		alert(requiredEmailText);
		return false;
	}
	// Check Email Format
	else if( !regex.test( email ) )
	{
		alert(invalidEmailText);
		return false;
	}
	// Default
	else
	{
		return true;
	}
} // END: validateReport()

//	Amenity Drop Down
//	Validates the drop down value for amenity selection
function validateAmenity( selectedAmenity )
{
	if( selectedAmenity != '' )
	{
		if( document.form1.zip.value != '' && document.form1.zip.value != defaultCityValue3 && document.form1.zip.value != defaultCityValue2 && document.form1.zip.value != defaultCityValue )
		{
			document.form1.submit();
		}
		else
		{
			var e = 1;
		}
	}
} // END: validateAmenity()

//	Home Page Search Form
//	Validates the search form on the homepage
function frontpage_submit()
{ 
	var theForm		= document.form1;
	var regex		= /^[0-9]+$/;
	var city		= ( document.getElementById('suggest') == undefined ) ? document.form1.city.value : document.getElementById('suggest').value;
	var zip			= '';
	var amenityIndex= theForm.elements['amenity[]'].selectedIndex;
	var	isValidZip	= validZipPostal(zip);
} // END: frontpage_submit()

//	
//	
function validateSearch()
{
	var theForm		= document.form1;
	var regex		= /^[0-9]+$/;
	var isHomeCare	= false;
	var amenities	= ( theForm.elements['amenity[]'] != undefined ) ? theForm.elements['amenity[]'] : '';
	var city		= ( theForm.elements['city'] != undefined ) ? theForm.elements['city'].value : '';
	var state		= ( theForm.elements['state'] != undefined ) ? theForm.elements['state'].value : '';
	var zip			= theForm.elements['zip'].value;
	var isValidZip 	= validZipPostal(zip);

	// Loop through amenities and see if "Home Care" is selected
	if( amenities != '' )
	{
		for( var i = 0; i < amenities.length; i++ )
		{
			var node = amenities[i];
			if( node.checked && node.value == '84' )
			{
				isHomeCare = true;
			}
		}
		if( amenities.value == '84' )
		{
			isHomeCare = true;
		}
	}
	
	// If Home Care and valid zip
	if( isHomeCare && isValidZip )
	{
		return true;
	}
	else if( isHomeCare && !isValidZip )
	{
		setTimeout( function(){ alert(invalidZipText); }, 500 );
		return false;
	}
	// If Home Care 
	else if( isHomeCare )
	{
		return true;
	}
	// If city and state are blank
	else if( ( city == '' || city == defaultCityValue || city == defaultCityValue2 || city == defaultCityValue3 ) && state == '' )
	{
		// If zip is blank
		if( zip == '' || zip == defaultZipValue || zip == defaultZipValue2 )
		{
			setTimeout( function(){ alert(invalidCityZipText); }, 500 );
			return false;
		}
		// If zip is invalid
		else if( !isValidZip )
		{
			setTimeout( function(){ alert(invalidZipText); }, 500 );
			return false;
		}
	}
	// Default
	else
	{
		return true;
	}
} // END: validateSearch()

function validateIntuitiveSearch()
{
	if( document.form1.zip.value == '' || document.form1.zip.value == defaultCityValue3 || document.form1.zip.value == defaultCityValue2 || document.form1.zip.value == defaultCityValue )
	{
		alert(invalidCityZipText);
		return false;
	}
	else if( document.form1.zip.value.length < 7 && !validZipPostal(document.form1.zip.value) )
	{
		alert(invalidZipText);
		return false;
	}
	else if( document.form1.amenity.value == '' )
	{
		alert(invalidHousingType);
		return false;
	}
	return true;
}

//	Validate Partner Form
//	Validates required fields for the partner form
function validatePartner()
{
	var theForm 	= document.form1;
	var extrafield1	= theForm.extrafield1.value;
	var name		= theForm.name.value;
	var phone		= theForm.phone.value;
	var email		= theForm.email.value;
	
	str = "";

	if( extrafield1 == '' )
	{
		str += "-Missing Company Name\n";
	}
	if( name == '' )
	{
		str += "-Missing Name\n";
	}
	if( phone == '' )
	{
		str += "-Missing Phone\n";
	}
	if( email == '' )
	{
		str += "-Missing Email\n";
	}

	if( str != '' )
	{
		alert("The following fields are missing or incorrect:\n"+str);
		return false;
	}
	else
	{
		return true;
	}
} // END: validatePartner()

function Hide()
{
	x = document.getElementById('otherFeatures');
	y = document.getElementById('divShow');
	z = document.getElementById('divHide');
	x.style.display = 'none';
	y.style.display = 'none';
	z.style.display = 'block';
}

function Show()
{
	x = document.getElementById('otherFeatures');
	y = document.getElementById('divShow');
	z = document.getElementById('divHide');
	x.style.display = 'block';
	y.style.display = 'block';
	z.style.display = 'none';
}

function HideClient(client_id)
{
	window.location.reload( true );
}

function IsNumeric(str)
{
	isNum = true;
	i = 0;
	while(isNum && i < str.length)
	{
		digit = str.substr(i, 1);
		if(digit < '0' || digit > '9')
		{
			isNum = false;
		}
		i++;
	}

	if(str.length == '')
	{
		isNum = false;
	}
	return isNum;
}

function isDigit(str)
{
	if (str.match(/[0-9]/)) return true;
	return false;
}
function isLetter(str)
{
	if (str.match(/[a-zA-Z]/)) return true;
	return false;
}

function disableEnterKey(e)
{
	var key;

	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = e.which;     //firefox

	if(key == 13)
	{
		return false;
	}
	else
		return true;
}

function validZipPostal(str) // matches 5 and 9 digit US codes, and 3 and 6 digit CA codes
{
	if (str.search(/^((\d{5}-\d{4})|(\d{5})|([A-Za-z]\d[A-Za-z][^0-9a-zA-Z]?\d[A-Za-z]\d)|([A-Za-z]\d[A-Za-z]))$/) == 0)
	{
		var response = $.ajax({
		   async:	false,
		   type: 	"POST",
		   url: 	"/js/validzip.php",
		   data: 	"zip="+str
		 }).responseText;
		 //if( response == 'valid' )
		 //{
			return true;
		// }
		 //else
		 //{
			return false;
		 //}
	}
	else
		return false;
}

function zipClick( homepage )
{
	var homepage = ( homepage == undefined ) ? 0 : 1;
	var zip = document.form1.zip;
	if(zip.value == defaultZipValue2)
	{
		zip.value = '';
	}
	else if( zip.value == '' )
	{
		zip.value = defaultZipValue2;
	}
	
	if( document.getElementById('suggest') == undefined && document.form1.city != undefined )
		document.form1.city.value = '';
	if( document.getElementById('suggest') != undefined )
	{
		document.getElementById('suggest').value = ( homepage ) ? defaultCityValue : defaultCityValue2;
	}
	if( document.getElementById('state') != undefined )
	{
		document.getElementById('state').value = '';
	}
	if( document.getElementById('city') != undefined )
	{
		document.getElementById('city').value = '';
	}
}

function cityClick( homepage )
{
	var homepage = ( homepage == undefined ) ? 0 : homepage;
	
	if( document.getElementById('suggest') == undefined )
	{
		if(document.form1.city.value == 'City' ){
			document.form1.city.value = '';
		}
	}
	if( document.getElementById('suggest') != undefined )
	{
		if( document.getElementById('suggest').value == defaultCityValue || document.getElementById('suggest').value == defaultCityValue2 || document.getElementById('suggest').value == defaultCityValue3 )
		{
			document.getElementById('suggest').value = '';
		}
		else if( document.getElementById('suggest').value == '' )
		{
			document.getElementById('suggest').value = ( homepage === 1 ) ? defaultCityValue : ( (homepage === 2)? defaultCityValue3:defaultCityValue2 );
		}
	}
}

function cityClick1( homepage )
{
	var homepage = ( homepage == undefined ) ? 0 : homepage;
	if( document.getElementById('suggest') == undefined )
	{
		if(document.form1.city.value == 'City' ){
			document.form1.city.value = '';
		}
	}

	if( document.getElementById('suggest') != undefined )
	{
		if( document.getElementById('suggest').value == defaultCityValue || document.getElementById('suggest').value == defaultCityValue )
		{
			document.getElementById('suggest').value = '';
		}
		else if( document.getElementById('suggest').value == '' )
		{
			document.getElementById('suggest').value = ( homepage ) ? defaultCityValue :  ( (homepage === 2)? defaultCityValue3:defaultCityValue2 );
		}
	}
}

function cityKeyPress()
{
	document.form1.city.value = '';
	document.form1.state.value = '';
}
function zipKeyPress()
{
	document.getElementById('suggest').value = '';
}

function ShowCities()
{
	document.getElementById('divShow').style.display = 'none';
	document.getElementById('moreLinks').style.display = 'inline';
}

function landingpage_submit(validate_all)
{ 
	var theForm		= document.form1;
	
	//var regex		= /^[0-9]+$/;
	var city		= ( document.getElementById('suggest') == undefined ) ? document.form1.city.value : document.getElementById('suggest').value;
	var zip			= document.getElementById('zip').value;
	var firstname	= $('#firstname').val();
	var lastname	= $('#lastname').val();
	var email		= $('#email').val();
	var phone		= $('#phone').val();
	
	$('#suggest').removeClass('mederror')
	$('#zip').removeClass('mederror')
	$('#firstname').removeClass('mederror')
	$('#lastname').removeClass('mederror')
	$('#email').removeClass('mederror')
	$('#phone').removeClass('mederror')
	
	var error = false;
	var errorstring = '';
	
	// City and Zip validation
	if ( ( zip == defaultZipValue || zip == '' ) && ( city == defaultCityValue || city == defaultCityValue2 || city == defaultCityValue3 || city == '' ) )
	{
		$('#suggest').addClass('mederror')
		$('#zip').addClass('mederror')
		errorstring += invalidCityZipText + "\n";
		error = true;
	}
	else if( ( city == defaultCityValue || city == defaultCityValue2 || city == defaultCityValue3 || city == '' ) && !validZipPostal(zip) )
	{
		$('#zip').addClass('mederror')
		errorstring += invalidZipText + "\n";
		error = true;
	}
	
	if( validate_all )
	{
		// Name validation
		if( firstname == '' )
		{
			$('#firstname').addClass('mederror')
			errorstring += "Please enter first name." + "\n";
			error = true;
		}
		if( lastname == '' )
		{
			$('#lastname').addClass('mederror')
			errorstring += "Please enter last name." + "\n";
			error = true;
		}
		
		// Email validation
		emailReg = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; // valid
		if( email == "" ){
			$('#email').addClass('mederror')
			errorstring += 'Missing email' + "\n";
			error = true;
		}
		else if( !emailReg.test(email ) )
		{
			$('#email').addClass('mederror')
			errorstring += "Invalid Email" + "\n";
			error = true;
		}
		
		// Phone validation
		phoneReg = /[^0-9]+/gi;
		phone = phone.replace(phoneReg,"");
		Phone1 = phone.substr(0,3);
		Phone2 = phone.substr(3,3);
		Phone3 = phone.substr(6,4);
		Phone4 = phone.substr(10);
		if(phone != '')
		{
			phone = '(' + Phone1 + ')' + Phone2 + '-' + Phone3;
		}
		if(Phone4 != '')
		{
			phone += ' x' + Phone4;
		}
		
		if(phone.length < 13)
		{
			$('#phone').addClass('mederror')
			errorstring += "Phone number must be 10 digits" + "\n";
			error = true;
		}
		else if ( Phone2 == "555" )
		{
			$('#phone').addClass('mederror')
			errorstring += "Phone number is not valid" + "\n";
			error = true;
		}
	}
	
	if( error ) 
	{ 
		errorstring = "There were errors with your submission: \n\n" + errorstring
		alert( errorstring );
		return false;
	}
	return true;
} // END: landingpage_submit()

function hideSummary( show_id, hide_id, e )
{
	var node;

	// hide summary
	node = document.getElementById( hide_id );
	node.style.display = "none";

	// show full description
	node = document.getElementById( show_id );
	node.style.display = "block";

	// prevent hyperlink action
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
}

function showSummary( show_id, hide_id, e )
{
	var node;

	// hide summary
	node = document.getElementById( hide_id );
	node.style.display = "none";

	// show full description
	node = document.getElementById( show_id );
	node.style.display = "block";

	// prevent hyperlink action
	e.preventDefault ? e.preventDefault() : e.returnValue = false;
}

