/*
This is the form validator for the tip signup form.  Different from the other form validators because of the field name 
differences required for compatibility with dockside.net.

last updated: 1/8/02
*/

var debug = false;

function checkForm(form){
	if(!debug){
		var isErr = false;
		var errStr = "Please check the following parts of the form\nand ensure they were completed:\n\n";
		var cardType, cardNumber, okToSubmit = 0;
	
		// check all pertinent fields for empty vals
		with(form){
			if(trim(first_name.value) == ""){
				errStr += "First Name\n";
				isErr = true;
			}
			if(trim(last_name.value) == ""){
				errStr += "Last Name\n";
				isErr = true;
			}
			if(trim(email.value) == ""){
				errStr += "Email Address\n";
				isErr = true;			
			}
			
			// jobTitle = title
			/*if(jobtitle.options[title.selectedIndex].value == ""){
				errStr += "Job Title\n";
				isErr = true;
			}
			
			if(jobfunction.options[jobfunction.selectedIndex].value == ""){
				errStr += "Industry\n";
				isErr = true;
			}
			
			if(companysize.options[companysize.selectedIndex].value == ""){
				errStr += "Company Size\n";
				isErr = true;
			}*/
			
			if(trim(street1.value) == ""){
				errStr += "Address\n";
				isErr = true;
			}
			if(trim(city.value) == ""){
				errStr += "City\n";
				isErr = true;
			}
			if(state.options[state.selectedIndex].value == ""){
				errStr += "State/Province\n";
				isErr = true;
			}	
			if(trim(zip.value) == ""){
				errStr += "Zip/Postal Code\n";
				isErr = true;
			}
			if(country.options[country.selectedIndex].value == ""){
				errStr += "Country\n";
				isErr = true;
			}
		}
		if(isErr){
			// show the user the error string
			alert(errStr);return false
		}else{
			// check the email address
			if(!checkEmail(form.email.value)){
				okToSubmit++;
				errStr += "Email Address (ex. you@domain.com)\n";
			}
			
			// check the work phone number (not required)
			/*
			if(trim(form.work_phone.value) != ""){
				if(!formatPhone(form, form.work_phone, form.work_phone.value)){
					okToSubmit++;
					errStr += "Work Phone Number (ex. 1235552266)\n";
				}
			}
			
			// check the home phone number (not required)
			if(trim(form.home_phone.value) != ""){
				if(!formatPhone(form, form.home_phone, form.home_phone.value)){
					okToSubmit++;
					errStr += "Home Phone Number (ex. 1235552266)\n";
				}
			}
			
			// check the fax number (not required)
			if(trim(form.fax_phone.value) != ""){
				if(!formatPhone(form, form.fax_phone, form.fax_phone.value)){
					okToSubmit++;
					errStr += "Fax Number (ex. 1235552266)\n";
				}
			}
			*/
			
			//we must send a value to m4 so if the box is not checked the value must be set to NO
			if(form.mailing_statusBox.checked){
				form.mailing_status.value = "Yes";
			
			}else{
				form.mailing_status.value = "No";
			
			}

			//we must send a value to m4 so if the box is not checked the value must be set to NO
			if(form.journalsemailBox.checked){
				form.journalsemail.value = "Yes";
			
			}else{
				form.journalsemail.value = "No";
			}
			
			if(trim(form.state.value) == "") {
				form.state.value = "OT";
				alert(form.state.value);
			}
			form.state.disabled = false;
			// if all is well, submit 'er
			if(okToSubmit == 0){
				
				form.submit();
			}else{
				alert(errStr);return false
			}
		}
	}else{
		alert("Debug mode!");
		form.submit();
	}
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

function formatPhone(frm, element, val){
	var txt = element;
	var str = "";
	
	for(var i=0; i<val.length; i++){
		if(!isNaN(val.charAt(i))){
			str += val.charAt(i);
		}
	}
	txt.value = str;
	
	if(isNaN(str) || str.length != 10){
		return false;
	}else{
		return true;
	}
}

function checkEmail(str){
	var supported = 0;
	
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	
	if (!supported) 
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^[^@ ]+@[^@ ]+\.[^@ \.]+$");
	return (!r1.test(str) && r2.test(str));
}