/**************
 * Certified Mail Landing Pages JS
 * 7/22/08
 **************/

//http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') { 
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	} 
}
addLoadEvent(setup_form);




/**********************
 * set up forms so they auto-populate with the label, and clear the default text when clicked
 * @params: id of form, array of default values
 *********************/
function setup_form(){
	thelist = document.getElementById('booksellers');
	
	if(!thelist){
		return false;
	}
	
	thelist.onsubmit = function(){
		return check_form(this);
	}
}


/********
 * Check all form fields and empty any that contain default values
 *******/
function check_form(theform){
	
	/*for (var i=0;i<theform.length;i++){
		
		if(theform.elements[i].className == "text" && theform.elements[i].value == ""){
			if(theform.elements[i].id != "company" && theform.elements[i].id != "title"){
				form_error("Please tell us your " + theform.elements[i].id);
				return false;
			}
		}else if(theform.elements[i].className == "select" && theform.elements[i].value == ""){
			form_error("Please tell us your " + theform.elements[i].id);
			return false;
		}
		
	}
	
	if(!checkEmail(theform.email.value)){
		form_error('Please check your email and try again.');
		return false;
	}
	*/
	
	form_error("processing...");
	document.getElementById('submit').disabled = true;
	
	var mysack = new sack( theform.action );    

  mysack.execute = 1;
  mysack.method = 'POST';
 
	mysack.setVar( "name", theform.name.value);
	mysack.setVar( "title", theform.title.value);
	mysack.setVar( 'company', theform.company.value);
	mysack.setVar( 'address', theform.address.value);
	mysack.setVar( 'city', theform.city.value);
	mysack.setVar( 'state', theform.state.value);
	mysack.setVar( 'zip', theform.zip.value);
	mysack.setVar( 'phone', theform.phone.value);
	mysack.setVar( "email",theform.email.value );
	mysack.setVar( 'ajax', 1);
	mysack.setVar( 'url', theform.url.value);
	
	
  mysack.onError = function() { alert('There was an error submitting the form. Please refresh this page and try again.' )};
  mysack.runAJAX();

  return false;
}

function form_error(m){
	if(m == ''){
		return false;
	}
	
	document.getElementById('form_error').innerHTML = "<p>" + m + "</p>";
	
	document.getElementById('submit').disabled = false;
}

function form_success(){
	
	document.getElementById('form_error').style.display = "none";
	
	document.getElementById('form_notice').innerHTML = "<p>Thank you for your request.</p>";
	document.getElementById('booksellers').style.display = "none";
}

/*************
 * check valid email address
 * http://www.douglaskarr.com/2007/10/28/javascript-regex-emailaddress/
 *************/
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}
	return true;
}