var cacheObj = new Object();

/*---\\\ EMAIL CHECK FUNCTIONS ///---*/
	function email_request(eml){
		//structKeyExists() is a UDF at the bottom of this page ;)
		if(structKeyExists(cacheObj,eml) )		//populate from cache if already retrieved
			details_response(cacheObj[eml]);
		else{									//otherwise make an http() call
			var httpParam = new Object();
				httpParam.email = eml;
			http( "POST" , "/openHouseCFC.cfc?method=getEmail" , email_response , httpParam );
		}
	}	
	
	function email_response(obj){
		cacheObj[obj.eml] = obj; 
		
		//POPULATE RESPONSE FIELD
		var form = document.userForm;
		var emailArea = document.getElementById('emailFound');
		if(obj.emailfound == 1) {
			emailArea.innerHTML = "* Email address already exists. Please <a href='signIn.cfm'>log in</a>.";
			document.getElementById('submit').disabled = true;
		} else {
			emailArea.innerHTML = "";
			document.getElementById('submit').disabled = false;
		}
	}
/*---\\\ EMAIL CHECK FUNCTIONS ///---*/

/*---\\\ SET FEATURED AD FUNCTIONS ///---*/
	function set_featured(fid,fstat) {
		(fstat == true)? fstat = 1: fstat = 0;
		var httpParam = new Object();
			httpParam.fid = fid;
			httpParam.fstat = fstat;
			http( "POST" , "/openHouseCFC.cfc?method=setFeatured" , feature_response , httpParam );
	}
	
	function feature_response (obj) {
			if(!obj) alert("Error");
	}

/*---\\\ SET FEATURED AD FUNCTIONS ///---*/

/*---\\\ FLAG INNAPROPRIATE FUNCTIONS ///---*/
	function flag_inappropriate(adID) {
		var httpParam = new Object();
			httpParam.adID = adID;
			http( "POST" , "/openHouseCFC.cfc?method=flagInappropriate" , flag_response , httpParam );
	}
	
	function flag_response (obj) {
			if(!obj) alert("Error");
			else alert('Thank you. We will review this ad and take necessary action.');
	}

/*---\\\ FLAG INNAPROPRIATE AD FUNCTIONS ///---*/

/*---\\\ CLEAR INNAPROPRIATE FLAG FUNCTIONS ///---*/
	function clear_flag(adID) {
		var httpParam = new Object();
			httpParam.adID = adID;
			http( "POST" , "/openHouseCFC.cfc?method=clearFlag" , clearflag_response , httpParam );
	}
	
	function clearflag_response (obj) {
			if(!obj) alert("Error");
			else alert("Flag has been cleared");
	}

/*---\\\ CLEAR INNAPROPRIATE FLAG FUNCTIONS ///---*/

/*---\\\ DISABLE AD FUNCTIONS ///---*/
	function disable_ad(adID) {
		var httpParam = new Object();
			httpParam.adID = adID;
			http( "POST" , "/openHouseCFC.cfc?method=disableAd" , disablead_response , httpParam );
	}
	
	function disablead_response (obj) {
			if(!obj) alert("Error");
			else alert("Ad has been disabled");
	}

/*---\\\ DISABLE AD FUNCTIONS ///---*/

/*---\\\ CHECK PROMO CODE FUNCTIONS ///---*/
	function check_promocode(promocode,acctID) {
		var httpParam = new Object();
			httpParam.strCode = promocode;
			httpParam.intAcctID = acctID;
			http( "POST" , "/openHouseCFC.cfc?method=checkCode" , checkcode_response , httpParam );
	}
	
	function checkcode_response (obj) {
		var browser = navigator.userAgent;
		var subtotal = document.getElementById('subtotal').value; // run total value from form, before any discounts
			subtotal = subtotal.replace(/[^\d.]/g, '');
		var discAmt = subtotal;
		var autodisc = Number(document.getElementById('autodisc').value); // if there is an automatic (quantity) discount amouint, it is here
		var total = document.getElementById('total'); // run total - automatic (quantity) deduction
		document.getElementById('canSend').value = 0;
			if(browser.match("MSIE")) showType = "inline";
			else showType = "table-row";
			if(obj.result == 1) { // discount code valid
				document.getElementById('canSend').value = 1;
				document.getElementById('response').innerHTML = "Valid";
				document.getElementById('response').style.color = "green";
				document.getElementById('response').style.fontweight = "bold";
				if(obj.promotype.match("%")) // discount percentage
					discAmt = subtotal*(obj.amt*.01) + autodisc;
				else if(obj.promotype.match("$")) // discount value
					discAmt = obj.amt + autodisc;
				document.getElementById('discount').innerHTML = "-$" + discAmt.toFixed(2);
				if(discAmt > subtotal) discAmt = subtotal;
				total.innerHTML = "$" + (subtotal - discAmt).toFixed(2);
			}
			else if (obj.result != 1) { // not valid code, or code already used
				document.getElementById('response').innerHTML = "Invalid";
				document.getElementById('response').style.color = "red";
				document.getElementById('response').style.fontweight = "bold";
				document.getElementById('discount').innerHTML = "-$" + autodisc.toFixed(2);
				document.getElementById('total').innerHTML = "$" + (subtotal-autodisc).toFixed(2);
				//document.getElementById('VISA').checked = false;
				//document.getElementById('ccNumber').value = "";
				//document.getElementById('month').selectedIndex = 0;
				//document.getElementById('year').selectedIndex = 0;
				//document.getElementById('cc1').style.display = showType;
				//document.getElementById('cc2').style.display = showType;				
				//document.getElementById('cc3').style.display = showType;
			}
			if(obj.result == 2) { // code already used
					alert(obj.message + "\n" + obj.detail);
			}
			if((discAmt==subtotal) && (obj.result==1)){ // valid code
				document.getElementById('VISA').checked = true;
				document.getElementById('ccNumber').value = "4111111111111111";
				document.getElementById('month').selectedIndex = 1;
				document.getElementById('year').selectedIndex = 3;
				document.getElementById('cc1').style.display = 'none';
				document.getElementById('cc2').style.display = 'none';				
				document.getElementById('cc3').style.display = 'none';
			} else { // not valid or used code
				//document.getElementById('VISA').checked = false;
				//document.getElementById('ccNumber').value = "";
				//document.getElementById('month').selectedIndex = 0;
				//document.getElementById('year').selectedIndex = 0;
				//document.getElementById('cc1').style.display = showType;
				//document.getElementById('cc2').style.display = showType;				
				//document.getElementById('cc3').style.display = showType;
			}
	}

/*---\\\ CHECK PROMO CODE FUNCTIONS ///---*/


/*---\\\ EMULATE CF FUNCTIONS ///---*/
	function structKeyExists(obj,key){
		for(x in obj){
			if(x == key)
			return true;
		}
	return false;	
	}
	
	function isDefined(argsVar){
		return (eval('typeof('+argsVar+') != "undefined"'))
	}
/*---\\\ EMULATE CF FUNCTIONS ///---*/
