var arrRegistrationInfo;
var arrStoreInfo;
var objEmailRegExp = /^(?:[a-zA-Z0-9_'^&amp;\/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;\/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$/i;

function showVideo(videoID) {
	var showVideo = document.getElementById(videoID);
	showVideo.onclick();
}

function clearSelect(elem) {
	elem.length = 0;
}


function supportedByMGD(Counties) {
		for (var i=0; i<Counties.length; i++) {
			// If the County ID or the State ID is not an integer value it means that it's not 
			// supported by MGD.
			if((Counties[i]['county_id'] == 0) || (Counties[i]['state_id'] == 0)) {
				var removed = Counties.splice(i, 1);
			}
		}
		
		if (Counties.length > 0) {
			return (true);
		}
		
		var zipNotSupported = document.getElementById('zipNotSupported');
		if(zipNotSupported){
			zipNotSupported.onclick();
		}
		return (false);
} // end of supportedByMGD

function submit_step1() {
	var zip = $("zip");

	if(zip.value == ""){
		var zipMissing = document.getElementById('zipMissing');
		if (zipMissing) {
			zipMissing.onclick();
		}
		return false;
	}else{
		ajax_request(submit_step1_cb, '../index.cfm?fuseaction=usr.getLocationInfoFromZip', Form.Element.serialize(zip) );
	}
}

function resetSplashBtn() {
	return false;
}
function submit_step1_cb(data) {
	var zip = $("zip");
					
	// Ths will import a variable called Counties
	eval(data.responseText);
	arrRegistrationInfo = Counties;

	if (Counties.length == 0) {
		zip.focus();
		zip.select();
		var zipNotFound = document.getElementById('zipNotFound');
		if(zipNotFound){
			zipNotFound.onclick();
		}
		return;
	} else if (Counties.length > 1) {
		if (supportedByMGD(Counties)) {
			if(hasCookieDomain && cookieDomain){
				document.cookie = 'MGD_GUEST_ZIP=' + encodeURIComponent(zip.value) + '; domain=' + cookieDomain;
			}else{
				document.cookie = 'MGD_GUEST_ZIP=' + encodeURIComponent(zip.value);
			}
			createCountyList();
		}
	} else {	
		if (supportedByMGD(Counties)) {
			if(hasCookieDomain && cookieDomain){
				document.cookie = 'MGD_GUEST_ZIP=' + encodeURIComponent(zip.value) + '; domain=' + cookieDomain;
			}else{
				document.cookie = 'MGD_GUEST_ZIP=' + encodeURIComponent(zip.value);
			}
			var county_id = Counties[0]['county_id'];
			finalSubmit(county_id);
		}else{
			var zipNotFound = document.getElementById('zipNotFound');
			if(zipNotFound){
				zipNotFound.onclick();
			}
		}
	}		
	
	zip.readOnly = true;
	return false;
}


function createCountyList() {
	var county_ids = "";
	var county_names = "";
	var found = "";
	var delim = "";
	for(var i=1; i<=arrRegistrationInfo.length; i++) {
		var cid = arrRegistrationInfo[(i-1)].county_id;
		if (found.indexOf("," + cid) < 0) {
			found = found + "," + cid;
			var county_id = arrRegistrationInfo[(i - 1)].county_id;
			var county_name = arrRegistrationInfo[(i - 1)].county_name;
			county_ids += delim + county_id;
			county_names += delim + county_name;
			delim = ",";
		}
	}
	var zipCountySelection = document.getElementById('zipCountySelection');
	if(zipCountySelection){
		zipCountySelection.href += '&county_ids='+encodeURIComponent(county_ids)+"&county_names="+encodeURIComponent(county_names);
		zipCountySelection.onclick();
		return false;
	}
}

function finalSubmit( county_id ){
	var search_terms = $('search_terms');
	var text = Form.Element.getValue(search_terms);
	var zip = $('zip');

	if(text && text !== "" && text !== "Type product name or category here"){
		 window.location = "index.cfm?fuseaction=endeca.dspResults&N=0&Ne=5&Dx=mode+matchallpartial&Ntx=mode+matchallpartial&Ntk=Search&D="+encodeURIComponent(text)+"&Ntt="+encodeURIComponent(text)+"&countyId="+encodeURIComponent(county_id)+"&zip="+encodeURIComponent(zip.value);
	}else{
		window.location = "index.cfm?fuseaction=usr.dspSetGuestStores&countyId="+encodeURIComponent(county_id)+"&zip="+encodeURIComponent(zip.value);
	}
}

function checkCounty() {
	var county_select = $('countyID');

	if (county_select.selectedIndex == 0) {
		alert("You must select a county before continuing.");
		return false;
	}

	var county_id = Form.Element.getValue(county_select);
	finalSubmit(county_id);
}

function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		submit_step1();
        return false;
	} 
	else
	return true;
}

	function dealsOnClick () {
	var search = document.getElementById("search_terms");
	search.value = "";
	search.className = "";
}
	function zipOnClick () {
	var zip = document.getElementById("zip");
	zip.value = "";
}
function zipOnBlur() {
	var zip = document.getElementById("zip");
	if(zip.value == ""){
		zip.value = "Enter Your 5-Digit Zip Code to Save!";
		zip.className = "field search fl_l";
	}
}


function submit_user_check() {
	var email = $("collectEmail");
	if(email.value == ""){
		document.getElementById('emailRequired').style.display = 'block';
		return false;
	}else{
		ajax_request(submit_user_check_cb, '../index.cfm?fuseaction=usr.getUserExists&strEmail='+encodeURIComponent(email.value) );
	}
}
function submit_user_check_cb(data){
	// Ths will import a variable called userExists
	eval(data.responseText);
	$('checkEmail').style.display = 'none';
	if(userExists){
		$('memberLogin').style.display = 'block';
		$('strUserName').value = $("collectEmail").value 
	}else{
		$('guestLogin').style.display = 'block';
		$('strGuestEmail').value = $("collectEmail").value 
	}
}
