
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) { 
	elm.addEventListener(evType, fn, useCapture); 
	return true; 
	}
	else if (elm.attachEvent) { 
	var r = elm.attachEvent('on' + evType, fn); 
	EventCache.add(elm, evType, fn);
	return r; 
	}
	else {
	elm['on' + evType] = fn;
	}
}
function getEventSrc(e) {
	if (!e) e = window.event;

	if (e.originalTarget)
	return e.originalTarget;
	else if (e.srcElement)
	return e.srcElement;
}
function addLoadEvent(func) {
var oldonload = window.onload;
	if (typeof window.onload != 'function') {
	window.onload = func;
	} else {
	window.onload = 
		function() {
		oldonload();
		func();
		}
	}
}
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();


addEvent(window,'unload',EventCache.flush, false);

function validateFields() {
	var frmEl = $("#emailForm");
	var sName = $("#senderName");
	var rName = $("#recieverName");
	var sEmail = $("#senderEmail");
	var rEmail = $("#recieverEmail");
	var message = $("#message");
	var $hp = $('#honeypot');
	
	var valid = true;
	
	if($hp.val() != ''){
		valid = false;
	}
	
	/*
	if(!(validateField(rEmail))){
		valid = false;	
	}  
	
	if(!(validateField(rName))){
		valid = false;	
	} 
	
	if(!(validateField(sEmail))){
		valid = false;	
	} 
		
	if(!(validateField(sName))){
		valid = false;	
	}
	*/
	
	if(valid){
		sendPosEmail();
	}
}

function validateField(ele){
	if(ele.val() == ''){
		ele.addClass("error");
		ele.focus();
		return false;
	}
	ele.removeClass("error");
	return true;
	
	//$(".vanadium-invalid span").css('display', 'block');
	
}

function cleanStr(str) {
	var cStr = str;
	cStr = cStr.replace(/&/g,"**am**");
	cStr = cStr.replace(/=/g,"**eq**");
	cStr = cStr.replace(/\+/g,"**pl**");
	return cStr;
}



function sendEmail (type,location) {
	if(type == 'c'){
		var suggestion = $("#charityName");
		var page = "/inc/xmlHttpRequest.php?charity=true&xml=true&location="+location;
		sentTimer = setTimeout("displaySuccess('c')",1000);

	}else{
		var suggestion = $("#businessName");
		var page = "/inc/xmlHttpRequest.php?business=true&xml=true&location="+location;
		sentTimer = setTimeout("displaySuccess('b')",1000);

	}
	
	var str1 = cleanStr(suggestion.val());
	var stuff = "suggestion="+str1;
	loadXMLPosDoc(page,stuff);
	
	//sentTimer = setTimeout("displaySuccess()",1000);

	return false;
}

function displaySuccess (type) {
	alertBox('Thank you','Thank you.  Your suggestion has been sent to Savvy Avenue.');
	
}



function sendPosEmail () {
	$(".btnSend").css("opacity","0.5");
	$(".btnSend").attr("disabled","disabled");
	var frmEl = $("#emailForm");
	var sName = $("#senderName");
	var rName = $("#recieverName");
	var sEmail = $("#senderEmail");
	var rEmail = $("#recieverEmail");
	var message = $("#message");
	var location = $("#location");
	var dealId = $('#dealId');
	var token = $("#token");
	
	var page = "/inc/xmlHttpRequest.php?contact=true&xml=true";
	var sentTimer = setTimeout("hideContactTimer()",2000);
	
	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	var str1 = cleanStr(sName.val());
	var str2 = cleanStr(rName.val());
	var str3 = cleanStr(sEmail.val());
	var str4 = cleanStr(rEmail.val());
	var str5 = cleanStr(message.val());
	var str6 = cleanStr(location.val());
	var str7 = cleanStr(token.val());
	var str8 = cleanStr(dealId.val());
	
	var stuff = "sName="+str1+"&rName="+str2+"&sEmail="+str3+"&rEmail="+str4+"&message="+str5+"&location="+str6+"&token="+str7+"&dealId="+str8;
	
	loadXMLPosDoc(page,stuff)
}

function hideContactTimer () {
	$(".btnSend").css("opacity","1");
	$(".btnSend").removeAttr("disabled");
	var fieldArea = $("#emailForm");
	alertBox(grabPosXML("status"),grabPosXML("confirmation"));
	if(grabPosXML("status") == "Success"){
		$(':input', fieldArea).each(function() {
			var type = this.type;
			var tag = this.tagName.toLowerCase();
		
			if(type == 'text' || tag == 'textarea'){
				this.value = "";	
			}
		});
	}
	
	var frmEl = $("#senderName");
	frmEl.focus();
	
}

function ajaxContact() {
	
	/*
	var frmEl = $('#emailForm');
	
	 frmEl.bind("submit", function(e){
    	 validateFields();
	 return false;
	 });
	 */
	 
	//addEvent(frmEl, 'submit', validateFields, false);
	//frmEl.onsubmit = function() { return false; }
}
addEvent(window, 'load',ajaxContact, false);

var pos; // variable for posting information
function loadXMLPosDoc(url,posData) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        pos = new XMLHttpRequest();
        pos.onreadystatechange = processPosChange;
        pos.open("POST", url, false);
		pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        pos.send(posData);
	// branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        pos = new ActiveXObject("Microsoft.XMLHTTP");
        if (pos) {
            pos.onreadystatechange = processPosChange;
            pos.open("POST", url, false);
			pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            pos.send(posData);
        }
    }
}

function grabPosXML (tagName) {
	return pos.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
}

function processPosChange() {
    // page loaded "complete"
    if (pos.readyState == 4) {
        // page is "OK"
        if (pos.status == 200) {
			if ( grabPosXML("posStatus") == 'NOTOK' ) { 
				alert('There were problems Sending Email. Please check back in a couple minutes');
			}
		}
	}
}

function validateSubscribe() {
	
	var cleveland = $("#cleveland");
	var nashville = $("#nashville");
	if((!(cleveland.attr('checked')))&&(!(nashville.attr('checked')))){
		alertBox('Error','Please choose at least one city.');
		return false;
	}
	var email = $("#tempSubscribeEmail");
	if(email.val() == ''){
		alertBox('Error','Please enter a valid email address.');
		return false;	
	}
		
	if((cleveland.attr('checked'))&&(nashville.attr('checked'))){
		subscribe(email.val(),0);
	}else if(nashville.attr('checked')){
		subscribe(email.val(),3);
	}else if(cleveland.attr('checked')){
		subscribe(email.val(),4);
	}
	
}

function validateSubscribe2(a,b) {
	if(a == ''){
		alertBox('Error','Please enter a valid email address to subscribe.');
	}else{
		$("#subscribeBtnTop").css("opacity","0.5");
		$("#subscribeBtnTop").attr("disabled","disabled");
		subscribe(a,b);
	}
}

function subscribe(email,location){
	$("#subscribeBtn").css("opacity","0.5");
	$("#subscribeBtn").attr("disabled","disabled");
	
	var page = "/inc/xmlHttpRequest.php?subscribe=true&xml=true&location="+location;

	var stuff = "email="+email;
	var sentTimer = setTimeout("displaySuccess2()",2000);

	loadXMLPosDoc(page,stuff);
}

/*
function subscribeTop(email,location){
	
	if(email == ''){
		alertBox('Error','Please enter a valid email address to subscribe.');
	}else{
		$("#subscribeBtnTop").css("opacity","0.5");
		$("#subscribeBtnTop").attr("disabled","disabled");
			var page = "/inc/xmlHttpRequest.php?subscribe=true&xml=true&location="+location;

			var stuff = "email=" + email + "&location=" + location;
			var sentTimer = setTimeout("subscribeTopSuccess()",2000);

			loadXMLPosDoc(page,stuff);
	}
}
*/


function subscribeTopSuccess(){
	if(grabPosXML("email") == 0){
		alertBox('Error','Please enter a valid email address.');
	}else{
		alertBox('Thank you!',grabPosXML("email")+' has been signed up to receive email updates!');
	}

	$("#subscribeBtnTop").css("opacity","1");
	$("#subscribeBtnTop").removeAttr("disabled");
}

function validateEmailFields(){
	var email = $("#signInEmail");
	var pw = $("#signInPassword");
	var valid = true;
	
	if(!(validateField(pw))){
		valid = false;	
	}
	
	if(!(validateField(email))){
		valid = false;
	}
	
	return valid;
}

function validateRegisterFields(){
	var email = $("#createAccountEmail");
	var pw = $("#createAccountPassword");
	var cpw = $("#createAccountConfirmPassword");
	
	var valid = true;
	
	if(!(validateField(cpw))){
		valid = false;	
	}
	
	if(!(validateField(pw))){
		valid = false;	
	}
	
	if(!(cpw.val() == pw.val())){
		valid = false;	
		cpw.addClass("error");
		pw.addClass("error");
	}
	
	if(!(validateField(email))){
		valid = false;
	}
	
	return valid;
}

function validateForgotPWFields(){
	var email = $("#forgotPasswordEmail");
	var cemail = $("#confirmForgotPasswordEmail");
	var valid = true;
	
	if(!(validateField(cemail))){
		valid = false;	
	}
	
	if(!(validateField(email))){
		valid = false;
	}
	
	if(!(cemail.val() == email.val())){
		valid = false;	
		cemail.addClass("error");
		email.addClass("error");
	}
	
	return valid;
}