// Chris Hiester, Tin Pan Valley, 10/21/2005, chris@tinpanvalley.com
// these two variables (addr_message, addr_message2) are the values in the rollover "overlib" popups in the index.php page
// that explain why we ask for the recipient's address and explain what our CAPTCHA is all about.



addr_message = "<p class='popup'>Enter your e-mail address if you would like a confirmation message when "  + 
"your Gassy Greeting Card is read.</p><p class='popup'> Please note that if you " +
"choose to get a confirmation e-mail, ,<strong> your e-mail address will still remain " +
" anonymous</strong>.</p> <p class='popup'>Nobody will ever know that you sent this Gassy Greeting Card!</p></span>";
addr_message2 = "To prevent abuse of this system by spammers, we verify that a human is "+
"submitting this form.";

// global variable used by AJAX functions to check for duplicate email addresses
var req;
var emsg = false;
var auth_url = "";

/*after the preview when all of the form fields have been filled out, this function does a final validation and if it passes
it sends the card to send.php */
function send_ggc(rc) {
	sc = "";
	checkEmail(rc, sc);
	checkForDuplicates(rc);
}

// this function displays the preview of the gassy greeting card. 
function preview(rc, sc) {
	checkEmail(rc, sc);
	//alert("emsg is " + emsg)
	if(emsg != true) {
	checkForDuplicates(rc);
	}
	//document.getElementById("recipient").innerHTML = rc;

	
}

/* this is the main AJAX function that pings the server to check for duplicate email addresses in the database */
function checkForDuplicates(rc) {
	var acheckurl = "http://www.trafon.org/gcc/addresstest.php?rc="+rc;
	loadXMLDoc(acheckurl);
}

//this function is going to do an AJAX call to the server to see if the person is registered. 
function authorize_premium_content(filename) {
	document.getElementsByTagName("body").display = "none";
	auth_url = "http://www.trafon.org/gcc/authorize.php?fn="+filename;
	loadXMLDoc(auth_url);
}

// this the validation function to make sure the email address is minimally valid (has a  "@" and a "." and no illegal characters).
function checkEmail(rc, sc) {
 var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(rc))) { 
	   alert( "Please enter a valid e-mail address for the recipient.\n");
	   document.getElementById("pv_holder").style.display = "none";
	   emsg = true;
	   return emsg;
	}
	if(sc != "") {
		if (!(emailFilter.test(sc))) { 
	  	 alert( "Please enter a valid e-mail address for the sender.\n");
	   	document.getElementById("pv_holder").style.display = "none";
	   	emsg = true;
	   	return emsg;
		}
	}
	
	

	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (rc.match(illegalChars)) {
		alert("The recipient's e-mail address contains illegal characters.\n");
		document.getElementById("pv_holder").style.display = "none";
		 emsg = true;
	   return emsg;
	}
	
	if (sc.match(illegalChars)) {
		alert("The sender's e-mail address contains illegal characters.\n");
		document.getElementById("pv_holder").style.display = "none";
		 emsg = true;
	   return emsg;
	}
return emsg = false;
}



/* AJAX stuff */

function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			// we need to determine if the request is coming from index.php or authorize.php
			// we make auth_url empty by default. If it's called from authorize.php, it has a value. 
			// thus we check and if it is still empty, then the request is coming from index.php.
			//alert(auth_url);
			
			if(auth_url != "")   {
				//this checks to see if user is allowed to see premium content. 
				if(req.responseXML.getElementsByTagName("premium")[0].childNodes[0].nodeValue == "TRUE") {
					//alert("authorized");
					return true;
				} else {
				//alert("not authorized");
				location.href="http://www.trafon.org/gcc/content/authorize_form.html";
				}
			} else {
				if(req.responseXML.getElementsByTagName("dup")[0].childNodes[0].nodeValue == "TRUE") {
						document.getElementById("error").innerHTML = "Sorry, this person has already received a Gassy Greeting Card.";
						document.getElementById("pv_holder").style.display = "none";
						return false;
					} else {
						document.getElementById("ps").style.visibility = "hidden";
						document.getElementById("pv_holder").style.display = "block";
						document.getElementById("error").innerHTML = "";
					}
			}

		
			
			
				

        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    } else {
if(typeof(req.responseXML.getElementsByTagName("dup")[0]) != "undefined") {
	if(typeof(req.responseXML.getElementsByTagName("dup")[0]) != "object") {
document.getElementById("error").innerHTML = "One moment please ...";
	}
}

}
	
}

// end of AJAX script

/* after the preview is displayed, we hide the "Edit Address" button to prevent a "false bottom", having the user
think that the Edit Address button is the button used to submit the form. However, if the user puts their cursor in the 
recipient address field, thne the Edit Address button appears to allow them to change the address (in the event of a typo, etc.). */
function showbutton() {
	document.getElementById("ps").style.visibility = "visible";
	if(document.getElementById("ps").style.visibility != "visible") {
	document.getElementById("ps").value = "Edit Address";
	}
}



