var isEmptyStrings = "Enter Email, Enter City, Enter State,Enter Zip,";
$('input#emailBox').example('Enter Email');
$('input#cityBox').example('Enter City');
$('input#stateBox').example('Enter State');
$('input#zipBox').example('Enter Zip');
$('.forminput').keypress(function(e){
	if (e.which == 13) {
	    submitForm(document.forms[0]);
	}
});
$('#loc').click(function(e){
	alert("loc");
	//alert(google.loader.ClientLocation.address.city);
	//alert(google.loader.ClientLocation.address.state);
});

function showForm() {
	var cookieURL = getCookie('startsavedest');
	if (cookieURL == null || !(cookieURL.indexOf('http://www.moneymailer.com') === 0)) {
		$('#startsaving').css("display", "none");
		$('#registerform').css("display", "");
	} else {
		location.href = cookieURL;
	}
}

function submitForm(theForm) {
	if (formValid(theForm)) {
		sendValues(theForm);
	}
}

function sendValues(theForm) {
	var dataArray = { email: theForm.email.value,
										zip: theForm.zip.value };
	if (isEmpty(theForm.zip.value)) {
		dataArray = { email: theForm.email.value,
									city: theForm.city.value,
									state: theForm.state.value};
	}
	$.ajax({
		type: 'POST',
		url: '/DesktopModules/SearchCoupons/sendregistration.asp',
		data: dataArray,
		success: function(xml) {
			sFlag = $(xml).find('[nodeName="a:SuccessFlag"]').text();
			destUrl = $(xml).find('[nodeName="a:WebURL"]').text();
			if (sFlag = "true") {
				setCookie('startsavedest', destUrl, 999);
			}
			location.href = destUrl;
		},
		dataType: 'xml'
	});
}

function formValid(theForm) {
	$('div.errormsg').css("visibility", "hidden");
	var emailregex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (emailregex.test(theForm.email.value) == false) {
		$('div#error-email').css("visibility", "visible");
		return false;
	}
	if (isEmpty(theForm.zip.value) && isEmpty(theForm.city.value) && isEmpty(theForm.state.value)) {
		$('div#error-zip').css("visibility", "visible");
		return false;
	}
	if (!isEmpty(theForm.zip.value)) {
		if (parseInt(theForm.zip.value) == NaN || theForm.zip.value.length != 5) {
			$('div#error-zip').css("visibility", "visible");
			return false;
		}
	} else {
		if (isEmpty(theForm.city.value)) {
			$('div#error-city').css("visibility", "visible");
			return false;
		}
		if (isEmpty(theForm.state.value)) {
			$('div#error-state').css("visibility", "visible");
			return false;
		}
	}

	return true;
}
function isEmpty(theValue) {
	return (theValue == '' || isEmptyStrings.indexOf(theValue + ",") != -1);
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}
