function GAddContactsToAddressBook(theContacts,theUserId,theResponseElementId) {
	
	//alert("[GAddContactsToAddressBook]: Adding "+theContacts.length+" Contacts");
	
	var str = "";
	var tmpstr = "";
	for(var i=0;i<theContacts.length;i++) {

		tmpstr = "";

		for(var j=0;j<theContacts[i].length;j++) {
			tmpstr += ";;;"+theContacts[i][j];	
		}
		str += "^^^"+tmpstr.substr(3);
	}
	str = str.substr(3);
	
	var gAddContactsRequest = new Ajax.Request("/ajax.addcontacts.php",
								{
									method:"post",
									parameters: {userid:theUserId,contacts:str},
									
									onSuccess: function(transport){
										var response = transport.responseText || false;
										
										if(response == false) alert("[GAddContactsToAddressBook]: Response not valid?");
										else {
											$(theResponseElementId).innerHTML = "<p class=\"notice\">"+transport.responseText+"</p>";
										}
									},
									onException: function(theInstance,theException) { alert("DEBUG: Exception!\n"+theException);
								 },
									onFailure: function(){ alert("DEBUG: Failed to connect to script:\n"+"/ajax.addcontacts.php") }
								});
	
}



function GToggleAllCheckboxesFor(theElementId,theToggleSwitch) {
	
	var inputElements = $(theElementId).getElementsByTagName("INPUT");
	
	var newState = theToggleSwitch.checked;
	
	for(var i=0;i<inputElements.length;i++) {
		
		if(inputElements[i].type != "checkbox" || inputElements[i]==theToggleSwitch) continue;
		
		inputElements[i].checked = newState;
		
	}

}






function GDisableFormTable(theTableId) {
	
	var inputElements = $(theTableId).getElementsByTagName("INPUT");
	for(var i=0;i<inputElements.length;i++) {
		inputElements[i].enabled = false;
	}
	var selectElements = $(theTableId).getElementsByTagName("SELECT");
	for(var i=0;i<selectElements.length;i++) {
		selectElements[i].enabled = false;
	}
	var textareaElements = $(theTableId).getElementsByTagName("TEXTAREA");
	for(var i=0;i<textareaElements.length;i++) {
		textareaElements[i].enabled = false;
	}
	
	$(theTableId).addClassName("disabled");
	
}


function GEnableFormTable(theTableId) {

	var inputElements = $(theTableId).getElementsByTagName("INPUT");
	for(var i=0;i<inputElements.length;i++) {
		inputElements[i].enabled = true;
	}
	var selectElements = $(theTableId).getElementsByTagName("SELECT");
	for(var i=0;i<selectElements.length;i++) {
		selectElements[i].enabled = true;
	}
	var textareaElements = $(theTableId).getElementsByTagName("TEXTAREA");
	for(var i=0;i<textareaElements.length;i++) {
		textareaElements[i].enabled = true;
	}
	
	$(theTableId).removeClassName("disabled");
	
}




function GValidateDonation(theFormId) {
	
	var form_elem = $(theFormId);
	
	if(form_elem['donation'].value=="") {
		window.alert("Please select a donation.");
		return false;
	}


	if(!$('dDonationOtherText') || $('dDonationOtherText').enabled == false || $('dDonationOther').checked == false) {
	
		GValidateWithRequired('eventdonate',['first_name','last_name','email']);
	
	} else if(parseInt($('dDonationOtherText').getValue()) > parseInt($('dMaxValue').value)) {
		
		
		
		window.alert("Your donation cannot exceed $"+$('dMaxValue').value+".");
		
		return false;
	
	} else if($('dDonationOtherText').getValue().trim()=="" || parseInt($('dDonationOtherText').getValue()) < parseInt($('dDonationMin').value)) {
		
		window.alert("Your donation must be at least $"+$('dDonationMin').value+".");
		
		return false;
	
	} else {
	
		GValidateWithRequired('eventdonate',['first_name','last_name','email']);
	
	}
	
	
}


function GValidateWithRequired(theFormId,requiredNames_arr) {

	var form_elem = $(theFormId);
	
	var theInput;
	var formIsValid = true;
	var tmp;
	var numfilter = /^([0-9_\.\,\-])+$/;
	
	// clear previous errors
	var allInputs = form_elem.getElements();
	for(var i=0;i<allInputs.length;i++) {
		// clear errors
		if($(allInputs[i]).hasClassName("error")) $(allInputs[i]).removeClassName("error");
	}
	
	
	// check passwords
	for(var i=0;i<allInputs.length;i++) {
	
		if($(allInputs[i]).enabled == false) continue;
	
		// check password fields
		if($(allInputs[i]).readAttribute("type") == "password") {
			
			if($($(allInputs[i]).readAttribute("id")+"Confirm")) {
				
				tmp = $($(allInputs[i]).readAttribute("id")+"Confirm");
				
				if($(allInputs[i]).getValue().length < 6 || $(tmp).getValue().length < 6 || $(tmp).getValue() != $(allInputs[i]).getValue()) {
					
					$(allInputs[i]).addClassName("error");
					$(tmp).addClassName("error");

					formIsValid = false;
					continue;
				}
			}
		}
	}
	
	
	var allTextInputs = form_elem.getInputs('text');
	
	// check all text inputs for numeric and email
	for(var i=0;i<allTextInputs.length;i++) {
		
		theInput = allTextInputs[i];
		
		if(theInput.enabled==false) continue;

		// Done checking for likely required elements
		if(!$(theInput).present()) continue;
		
		
		if($(theInput).value == $(theInput).getAttribute("example")) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}
		
		if($(theInput).hasClassName("numeric") && $(theInput).name.indexOf("date") < 0 && !numfilter.test($(theInput).getValue())) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}
		
		if($(theInput).name.indexOf("date") > -1 && !GValidateDate($(theInput).getValue())) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}
		
		if($(theInput).name.indexOf("email") > -1 && !GValidateEmail($(theInput).getValue())) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}
		
		if($(theInput).name.indexOf("phone") > -1 && !GValidatePhone($(theInput).getValue())) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}
		
		if($(theInput).name.indexOf("zip") > -1 &&  !GValidateZIP($(theInput).getValue())) {
			$(theInput).addClassName("error");
			formIsValid = false;
			continue;
		}

	}
	
	
	
	if(requiredNames_arr != null) {
	
	
		for(var i=0;i<requiredNames_arr.length;i++) {
		
			theInput = form_elem[requiredNames_arr[i]];
			
			if(!$(theInput) || $(theInput).enabled == false) continue;
			
			if(!$(theInput).getValue() || $(theInput).getValue().trim() == "") {
				$(theInput).addClassName("error");
				formIsValid = false;
				continue;
			}
			
		}

	} 
	
	
	
	if(formIsValid) form_elem.submit();
	else window.alert("There are errors in your submission.\nPlease review highlighted fields.");
	
}

function GValidateEmail(theEmailStr) {
	var emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailfilter.test(theEmailStr);
}

function GValidateDate(theDateStr) {
	var datefilter = /^([0-9]{1,2})(.)([0-9]{1,2})(.)([0-9]{4})$/;
	if(!datefilter.test(theDateStr)) return false;
	
	var theDateArr = theDateStr.match(datefilter);

	if(parseInt(theDateArr[1]) < 1 || parseInt(theDateArr[1]) > 12) return false;
	if(parseInt(theDateArr[3]) < 1 || parseInt(theDateArr[3]) > 31) return false;
	if(parseInt(theDateArr[5]) < 2009 || parseInt(theDateArr[5]) > 2100) return false;

	return true;
	
}


function GValidatePhone(thePhoneStr) {
	var phonefilter = /^(\(?)([0-9]{3})(\)?)(.?)([0-9]{3})(.?)([0-9]{4})$/;
	return phonefilter.test(thePhoneStr);
}

function GValidateZIP(theZIPStr) {
	var zipfilter = /^([0-9]{5})(\-[0-9]{4})?$/;
	return zipfilter.test(theZIPStr);
}






function GPopup(theUrl) {
		
	var win_width = 400;
	var win_height = 400; 	

	window.open(theUrl,"popup","width="+win_width+",height="+win_height+",menubar=no,resizeable=yes,scrollbars=yes" );
}


function GResizeArtFlashHeight(theNewHeight) {
	$('artslideshowcontainer').style.height = theNewHeight+"px";
}



function GStripeTables() {
	var tables = document.getElementsByTagName("TABLE");
	var tbody;
	var rows;
	var flag = false;
	
	for(var i=0;i<tables.length;i++) {
	
		if(tables[i].className.indexOf("striped") < 0) continue;
	
		flag = false;
	
		tbody = tables[i].getElementsByTagName("TBODY");
	
		rows = tbody[0].getElementsByTagName("TR");

		for(var j=0;j<rows.length;j++) {
			if(flag) rows[j].className += " alt";
			flag = !flag;
		}

	}
}


function GSetupListeners() {
	var inputs = document.getElementsByTagName("INPUT");

	for(var i=0;i<inputs.length;i++) {
		
		if(inputs[i].type == "text" && inputs[i].getAttribute("example")) {

			$(inputs[i]).observe('focus',GL_Text_Focus);
			$(inputs[i]).observe('blur',GL_Text_Blur);
			
			if($(inputs[i]).value.trim()=="" && inputs[i].getAttribute("example")) {
				inputs[i].value = inputs[i].getAttribute("example");
				if(!$(inputs[i]).hasClassName("inactive")) $(inputs[i]).addClassName("inactive");
			}
			
		}
	}
	
	
	var forms = document.getElementsByTagName("FORM");
	
	for(var i=0;i<forms.length;i++) {
		
		$(forms[i]).observe('submit',GL_Form_Submit);
		
	}

}


GL_Form_Submit = function(event) {

	var inputs = event.target.getInputs('text');
	
	for(var i=0;i<inputs.length;i++) {		
		if(inputs[i].getAttribute("example") == $(inputs[i]).getValue() && $(inputs[i]).hasClassName("inactive")) {
			inputs[i].value = "";
		}
	}



}


GL_Text_Focus = function(event) {
	
	if($(event.target).getAttribute("example") == $(event.target).getValue() && $(event.target).hasClassName("inactive"))  $(event.target).value = "";
	
	
	if($(event.target).hasClassName("inactive")) $(event.target).removeClassName("inactive");
	if(!$(event.target).hasClassName("active")) $(event.target).addClassName("active");
	
};

GL_Text_Blur = function(event) {
	
	if($(event.target).value.blank()) {
		if($(event.target).hasClassName("active")) $(event.target).removeClassName("active");
		if(!$(event.target).hasClassName("inactive")) $(event.target).addClassName("inactive");
		if($(event.target).getAttribute("example")) $(event.target).value = $(event.target).getAttribute("example")

	}
	
};





String.prototype.trim=function() { return this.replace(/^\s+|\s+$/g,"");}
String.prototype.ltrim=function() { return this.replace(/^\s+/,"");}
String.prototype.rtrim=function() { return this.replace(/\s+$/,"");}


window.onload = function() {
	GStripeTables();
	GSetupListeners();
};

