String.prototype.trim=function() {
     return this.rtrim().ltrim()
}
String.prototype.ltrim=function() {
     return this.replace(/^\s*/,'')
}
String.prototype.rtrim=function() {
     return this.replace(/\s*$/,'')
}

function check_email(email) {
	var emregex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	if (email.trim() == "") return 'blank';
	else if (!email.trim().match(emregex)) return 'invalid';
	else return 'valid';
}

//AJAX stuff
function makeRequest(url,val) {
	var http_request = false;
	var email = val.trim();
	var res = "";
	
	//This section will validate the email address entered
	document.getElementById("resp").innerHTML = "Loading...";
	res = check_email(email);
	if (res == "blank") {
		document.getElementById("resp").innerHTML = "Please enter your e-mail before clicking the button";
		return false;
	}
	else if (res == "invalid")	{
		document.getElementById("resp").innerHTML = "Please enter a valid email of the format \"x@y.ext\". Thanks!";
		return false;
	}
	//end email validation
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			  http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	
	urlemail = email.replace("@","*|*");
	url = url+"?email="+email;
	http_request.onreadystatechange = function() { alertContents(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);
}

function alertContents(http_request) {
    if (http_request.readyState == 4) {
    	if (http_request.status == 200) {
            var htmlres = http_request.responseText;
			if (htmlres.substr(0,6) == "Thanks") document.getElementById("eminp").value = "";
			document.getElementById("resp").innerHTML = htmlres;
        } else if (http_request.status == 404) {
        	alert ("Requested URL is not found.");
      	} else if (http_request.status == 403) {
        	alert("Access denied.");
		} else {
            alert('There was a problem with the request.  '+http_request.status);
        }
    }
}

function changelink(lnk,x,y) {
	var xx = "";
	var a;
	for(i=1; i <= y; i++) {
		xx = lnk+i;
		a = document.getElementById(xx).innerHTML;
		//alert(a);
		if (i==x) {
			document.getElementById(xx).color="#CD711C";
			if (a.indexOf("&gt;") == -1 && a.indexOf(">") == -1)
				document.getElementById(xx).innerHTML = a+'&nbsp;&nbsp;&nbsp;&nbsp;&gt;';
		}
		else {
			document.getElementById(xx).color="#FFFFFF";
			if (a.indexOf("&gt;") > 0)
				document.getElementById(xx).innerHTML = a.replace("&nbsp;&nbsp;&nbsp;&nbsp;&gt;","");
			if (a.indexOf(">") > 0) {
				document.getElementById(xx).innerHTML = a.replace(">","");
				document.getElementById(xx).innerHTML = document.getElementById(xx).innerHTML.replace(/^\s+/,"");
				document.getElementById(xx).innerHTML = document.getElementById(xx).innerHTML.replace(/\xA0/g,"");
			}
		}
	}
	return true;
}