// JavaScript Document

var validator_error = false;
var validator_error_start = "";
var validator_error_show_front = true;
var validator_error_msg = "";
function validator_register_form() {
	var form = document.register_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	/*
	var firstname = trim(form.firstname.value);
	if( str_length(firstname) == 0 ) {
		alert("Please enter the first name!");
		form.firstname.focus();
		return false;
	}
	if( form.lastname.value == "0" ) {
		alert("Please enter the last name!");
		form.lastname.focus();
		return false;
	}
	*/
	validator_input_check_valid(form, "contact_name", "Please enter the Contact Name!");
	validator_input_check_valid(form, "account_number", "Please enter the Account Number!");
	validator_input_check_valid(form, "company", "Please enter the company name!");
	//validator_input_check_valid(form, "firstname", "Please enter the first name!");
	//validator_input_check_valid(form, "lastname", "Please enter the last name!");
	validator_input_check_valid(form, "email", "Please enter the E-mail!");
	validator_mail_check_valid(form, "email", "Please enter a valid E-mail!");
	//username
	validator_input_check_valid(form, "name", "Please enter the User Name");
	//password
	validator_mail_check_password(form, "password", "password_confirm");
	//
	validator_input_check_valid(form, "street_address", "Please enter the street address!");
	//validator_input_check_valid(form, "postcode", "Please enter the postcode!");
	validator_input_check_valid(form, "city", "Please enter the city!");
	validator_input_check_valid(form, "state", "Please enter the state!");
	validator_input_check_valid(form, "country", "Please enter the country!");
	validator_input_check_valid(form, "telephone", "Please enter the telephone!");
	validator_input_check_valid(form, "fax", "Please enter the fax!");
	//
	//validator_input_check_valid(form, "register_securitycode", "Please enter the securitycode!");
	//investgation
	/*
	var invest_result = validator_register_form_investgation(form);
	if( invest_result == false ) {
		validator_error = true;
	}*/
	
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	//alert(validator_error + " " + validator_error_start);
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}

function validator_register_form_investgation(form) {
	if( $("validator_investgation_error_msg_box") ) {
		$("validator_investgation_error_msg_box").innerHTML = "";
		$("validator_investgation_error_msg_box").style.display = "none";
	}
	var error_msg = "";
	if( form["invest_hear_about"].value == "N/A" ) {
		error_msg += "1. Please select where you hear about us\n";
	}
	if( form.invest_business_type_selected.value == 0 ) {
		error_msg += "2. Please select a business type\n";
	}
	if( str_length(form.invest_location.value) == 0 ) {
		error_msg += "3. Please enter how many locations you are currently operating type\n";
	}
	if( validator_input_isSelected("invest_years_shutter", form) == false ) {
		error_msg += "4. Please select how many years of shutter experience you have\n";
	}
	if( validator_input_isSelected("invest_current_buy", form) == false ) {
		error_msg += "5. Please select Are you currently buying Norman Shutters products\n";
	}
	if( form.invest_other_shutter.value == "" ) {
		error_msg += "6. What other shutter manufacturers have you worked with in the past\n";
	}
	if( validator_input_isSelected("invest_percentage", form) == false ) {
		error_msg += "7. Please select What percentage of your business is shutters\n";
	}
	if( validator_input_isSelected("invest_volume", form) == false ) {
		error_msg += "8. Please select What is your current monthly shutter volume\n";
	}
	if( validator_input_isSelected("invest_owninstaller", form) == false ) {
		error_msg += "9. Please select Do you currently employ your own installers\n";
	}
	if( form.invest_shutter_selected.value == 0 ) {
		error_msg += "10.Please select What type of shutter program will best fit your business\n";
	}
	if( str_length(form.invest_brief_desc.value) < 20 ) {
		error_msg += "11.Please give a brief description of the shutter market in your region, min 20 charaters\n";
	}
	if( str_length(form.invest_purpose.value) < 20 ) {
		error_msg += "12.The purpose of your inquiry, min 20 charaters\n";
	}
	
	if( error_msg ) {
		if( $("validator_investgation_error_msg_box") ) {
			var error_msg = error_msg.replace(/\n/g, "<br>");
			$("validator_investgation_error_msg_box").innerHTML = error_msg;
			$("validator_investgation_error_msg_box").style.display = "";
		} else {
			alert(error_msg);
		}
		return false;
	} else {
		return true;
	}
}

// checkbox
function validator_input_isChecked(isitchecked, target, form){
	if( typeof(form) == "undefined" ) {
		var form = document.register_form;
	}
	if( isitchecked == true ){
		form[target].value++;
	} else {
		form[target].value--;
	}
}

// radio
function validator_input_isSelected(element, form) {
	if( typeof(form) == "undefined" ) {
		var form = document.register_form;
	}
	for (i=0;i<form[element].length;i++) {
		if (form[element][i].checked) {
			return form[element][i].value
		}
	}
	return false;
}

function validator_mail_check_password(form, password, password_confirm) {
	var passwordvalue = trim(form[password].value);
	var passwordconfirmvalue = trim(form[password_confirm].value);
	if( $(password + "_msg") ) {
		$(password + "_msg").innerHTML = "";
	}
	
	if( str_length(passwordvalue) == 0 ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = password;
		}
		if( validator_error_show_front == false && $(password + "_msg") ) {
			$(password + "_msg").innerHTML = "Please enter the password!";
		} else {
			validator_error_msg += "Please enter the password!" + "\n";
		}
		return false;
	} else if( str_length(passwordvalue) < 5 ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = password;
		}
		if( validator_error_show_front == false && $(password + "_msg") ) {
			$(password + "_msg").innerHTML = "The password is more than 5 characters!";
		} else {
			validator_error_msg += "The password is more than 5 characters!" + "\n";
		}
		return false;
	} else if( str_length(passwordconfirmvalue) == 0 ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = password_confirm;
		}
		if( validator_error_show_front == false && $(password_confirm + "_msg") ) {
			$(password_confirm + "_msg").innerHTML = "Please enter the password again!";
		} else {
			validator_error_msg += "Please enter the password again!" + "\n";
		}
		return false;
	} else if( passwordvalue != passwordconfirmvalue ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = password_confirm;
		}
		if( validator_error_show_front == false && $(password_confirm + "_msg") ) {
			$(password + "_msg").innerHTML = "The password donot match!";
		} else {
			validator_error_msg += "The password donot match!" + "\n";
		}
		return false;
	}
	
	return true;
}


function validator_input_check_valid(form, inputname, error_msg) {
	var inputvalue = trim(form[inputname].value);
	if( $(inputname + "_msg") ) {
		$(inputname + "_msg").innerHTML = "";
	}
	if( str_length(inputvalue) == 0 ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = inputname;
		}
		if( validator_error_show_front == false && $(inputname + "_msg") ) {
			$(inputname + "_msg").innerHTML = error_msg;
		} else {
			validator_error_msg += error_msg + "\n";
		}
		return false;
	}
	
	return true;
}

function validator_input_check_length_valid(form, inputname, error_msg) {
	var inputvalue = trim(form[inputname].value);
	if( $(inputname + "_msg") ) {
		$(inputname + "_msg").innerHTML = "";
	}
	if( str_length(inputvalue) > 0 && str_length(inputvalue) < 20 ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = inputname;
		}
		if( validator_error_show_front == false && $(inputname + "_msg") ) {
			$(inputname + "_msg").innerHTML = error_msg;
		} else {
			validator_error_msg += error_msg + "\n";
		}
		return false;
	}
	
	return true;
}

function validator_login_form() {
	var form = document.login_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	//validator
	validator_input_check_valid(form, "username", "Please enter the login name!");
	validator_input_check_valid(form, "password", "Please enter the password!");
	validator_input_check_valid(form, "customers_securitycode", "Please enter the security code!");
	
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}

function validator_contact_us_form() {
	var form = document.contact_us_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	//validator
	validator_input_check_valid(form, "company", "Please enter the Company Name!");
	validator_input_check_valid(form, "telephone", "Please enter the Telephone No.!");
	validator_input_check_valid(form, "email", "Please enter a Email!");
	validator_mail_check_valid(form, "email", "Please enter a Valid Email!");
	validator_input_check_valid(form, "subject", "Please enter the Subject!");
	validator_input_check_valid(form, "message", "Please enter the Message!");
	validator_input_check_length_valid(form, "message", "More than 20 characters!");
	
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}

function validator_mail_check_valid(form, inputname, error_msg) {
	var inputvalue = trim(form[inputname].value);
	if( $(inputname + "_msg") ) {
		$(inputname + "_msg").innerHTML = "";
	}
	if( str_length(inputvalue) > 0 && checkEmail(inputvalue) == false ) {
		validator_error = true;
		if( validator_error_start == "" ) {
			validator_error_start = inputname;
		}
		if( validator_error_show_front == false && $(inputname + "_msg") ) {
			$(inputname + "_msg").innerHTML = error_msg;
		} else {
			validator_error_msg += error_msg + "\n";
		}
		return false;
	}
	
	return true;
}

function validator_password_form() {
	var form = document.password_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	//validator
	validator_input_check_valid(form, "username", "Please enter the login name or number!");
	validator_input_check_valid(form, "email", "Please enter the email address!");
	validator_mail_check_valid(form, "email", "Please enter valid email address!");
	//view
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}

function validator_customer_address_form() {
	var form = document.customer_address_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	//validator
	validator_input_check_valid(form, "entry_firstname", "Please enter the first name!");
	validator_input_check_valid(form, "entry_lastname", "Please enter the last name!");
	validator_input_check_valid(form, "entry_company", "Please enter the company!");
	validator_input_check_valid(form, "entry_street_address", "Please enter the street address!");
	validator_input_check_valid(form, "entry_suburb", "Please enter the suburb!");
	validator_input_check_valid(form, "entry_postcode", "Please enter the postcode!");
	validator_input_check_valid(form, "entry_city", "Please enter the city!");
	validator_input_check_valid(form, "entry_state", "Please enter the state!");
	validator_input_check_valid(form, "entry_country", "Please enter the country!");
	//view
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}

function validator_customer_order_form() {
	var form = document.customer_order_form;
	//init
	validator_error = false;
	validator_error_start = "";
	validator_error_msg = "";
	if( $("validator_error_msg_box") ) {
		$("validator_error_msg_box").innerHTML = "";
		$("validator_error_msg_box").style.display = "none";
	}
	//validator
	validator_input_check_valid(form, "orders_contents", "Please enter the orders contents!");
	//view
	if( str_length(validator_error_msg) && validator_error_show_front == true ) {
		if( $("validator_error_msg_box") ) {
			var error_msg = validator_error_msg.replace(/\n/g, "<br>");
			$("validator_error_msg_box").innerHTML = error_msg;
			$("validator_error_msg_box").style.display = "";
		} else {
			alert(validator_error_msg);
		}
		//validator_error = true;
	}
	if( validator_error_start ) {
		form[validator_error_start].focus();
	}
	
	if( validator_error == true ) {
		return false;
	} else {
		return true;
	}
}
