// JavaScript Document
var XMLHttp;
function sendRequest(url, results){
	XMLHttp = getXMLHttp();
	XMLHttp.open("POST",url);
	XMLHttp.onreadystatechange=handlerFunk;
	XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	XMLHttp.send(results);
}
function handlerFunk(){
	if(XMLHttp.readyState == 4){
		var xml = XMLHttp.responseXML;
		var text = XMLHttp.responseText;
	}
}
function getXMLHttp() {
  var XMLHttp = null;
  if (window.XMLHttpRequest) {
    try {
      XMLHttp = new XMLHttpRequest();
    } catch (e) { }
  } else if (window.ActiveXObject) {
    try {
      XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) { }
    }
  }
  return XMLHttp;
}
var checkform;
function pledgeForm(){
	if(checkForm('pledge')){
		//location.href = "thanks.html";
		document.pledge.submit();	
	}
}
function emailForm(){
	if(checkForm('tella')){
		checkform.results += "whatTypeofCall=tellafriend&";
		document.getElementById('tellafriend').style.display = "none";
		document.getElementById('thankyou').style.display = "block";
		checkform.reset();
		if (location.href.match("event") != null || location.href.match("mobile") != null) {
			sendRequest("../php/clear2go.php", checkform.results);
		}
		else {
			sendRequest("php/clear2go.php", checkform.results);
		}
	}
}
function checkForm(formname){
	checkform = new submitForm(formname);
	checkform.validate();
	if(checkform.continueSend){
		return true;
	}else{
		return false;
	}
}
function checkValid(){	
	checkform.validate();
}
function submitForm(id){
	this.name = id;
	this.continueSend = true;
	this.element = document.getElementsByName(this.name)[0];
	this.results = "";
	this.emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	elementArray : 
		this.elementArray = new Array();
		var inputs = this.element.getElementsByTagName('input');
		var selects = this.element.getElementsByTagName('select');
		var textareas = this.element.getElementsByTagName('textarea');
		var i;
		var j = 0;
		for(i = 0; i < inputs.length; i++){
			if(inputs[i].className.match('required') != null){
				this.elementArray[j] = inputs[i];
				inputs[i].onblur = checkValid;
				j++;
			}
		}
		for(i = 0; i < selects.length; i++){
			if(selects[i].className.match('required') != null){
				this.elementArray[j] = selects[i];
				selects[i].onchange = checkValid;
				j++;
			}
		}
		for(i = 0; i < textareas.length; i++){
			if(textareas[i].className.match('required') != null){
				this.elementArray[j] = textareas[i];
				textareas[i].onblur = checkValid;
				j++;
			}
		};
	this.validate = function(){
		this.update();
		this.continueSend = true;
		for(var i = 0; i < this.elementArray.length; i++){
			if (this.elementArray[i].value == '-1' || this.elementArray[i].value == this.elementArray[i].defaultValue ||
			(this.elementArray[i].name.match("email") && !this.emailFilter.test(this.elementArray[i].value))) {
				this.continueSend = false;
				if (this.elementArray[i].name.match("email")) {
					this.writeError("Invalid Email", this.elementArray[i]);
				}
				else {
					this.writeError("This field is required", this.elementArray[i]);
				}
			}else if(this.elementArray[i].value == ''){
				this.continueSend = false;
				this.writeError("This field is required", this.elementArray[i]);
				this.elementArray[i].value = this.elementArray[i].defaultValue;	
			}else{
				if(this.elementArray[i].type == "password" && this.elementArray[i-1].type == "password"){
					if(this.elementArray[i].value != this.elementArray[i-1].value){
						this.writeError("Passwords do not match", this.elementArray[i]);	
						this.continueSend = false;
					}
					this.results += this.elementArray[i].name + '=' + this.elementArray[i].value + '&';
				}else{
					this.results += this.elementArray[i].name + '=' + this.elementArray[i].value + '&';	
				}
			}
		}
	};
	this.writeError = function(txt,element){
		var w = document.createElement("div");
		w.setAttribute("id", "securityRed");
		w.style.fontSize = 11 + "px";
		w.style.textAlign = "right";
		w.style.clear = "left";
		w.style.padding = "2px 0 0 0";
		w.style.color = "red";

		if(element.parentNode.lastChild.id != "securityRed"){
			element.parentNode.appendChild(w).innerHTML = txt;		
		}
	}
	this.update = function(){
		for(var i =0; i < this.elementArray.length; i++){	
			if(this.elementArray[i].parentNode.lastChild.id == "securityRed"){
				this.elementArray[i].parentNode.removeChild(this.elementArray[i].parentNode.lastChild);
				this.elementArray[i].parentNode.style.color = "black";
				this.elementArray[i].parentNode.style.backgroundColor = "transparent";
			}
		}	
	}
	this.reset = function(){
		for(var i =0; i < this.elementArray.length; i++){	
			this.elementArray[i].value = this.elementArray[i].defaultValue;
		}		
	}
	return true;
}
function moreInfoWindow(){
	window.scrollTo(0,0);
	document.getElementById('largeCont').style.display = "block";
	document.getElementById('overlayComment').style.display = "block";
	document.getElementById('about').style.display = "block";	
	var selectArray = document.getElementsByTagName('select');
	for(var i = 0; i < selectArray.length; i++){
		selectArray[i].style.display = "none";	
	}
}
function closeLight(){
	document.getElementById('tellafriend').style.display = "none";
	document.getElementById('about').style.display = "none";	
	document.getElementById('largeCont').style.display = "none";
	document.getElementById('overlayComment').style.display = "none";	
	document.getElementById('thankyou').display = "none";
	var selectArray = document.getElementsByTagName('select');
	for(var i = 0; i < selectArray.length; i++){
		selectArray[i].style.display = "inline";	
	}
}
function openTellafriend(){
	window.scrollTo(0,0);
	document.getElementById('largeCont').style.display = "block";
	document.getElementById('overlayComment').style.display = "block";
	document.getElementById('tellafriend').style.display = "block";	
	var selectArray = document.getElementsByTagName('select');
	for(var i = 0; i < selectArray.length; i++){
		selectArray[i].style.display = "none";	
	}
}
function swapEmail(){
	document.getElementById('tellafriend').style.display = "block";
		document.getElementById('thankyou').style.display = "none";
}
function checkShare(){
	if(location.search.length > 1){
		if(location.search == "?share"){
			openTellafriend();
		}	
	}
}














