var error_count = 0;
var click_count = 0;
function check_account_create(){
	if (document.account_create.email_address.value.length == 0){
		error_count ++;
	}else if(!CheckEmail(document.account_create.email_address.value)){
		error_count ++;
	}
	if (document.account_create.password.value.length == 0){
		error_count ++;
	}else if(document.account_create.password.value.length < 5){
		error_count ++;
	}
	if (document.account_create.password.value != document.account_create.password_confirm.value){
		error_count ++;
	}

	if (document.account_create.firstname.value.length == 0){
		error_count ++;
	}

	if (document.account_create.lastname.value.length == 0){
		error_count ++;
	}

	if (document.account_create.street_address.value.length == 0){
		error_count ++;
	}

	if (document.account_create.city.value.length == 0){
		error_count ++;
	}

	if (document.account_create.postcode.value.length == 0){
		error_count ++;
	}else if(!CheckPost(document.account_create.postcode.value)){
		error_count ++;
	}

	 var obj = document.getElementsByName("phone_area");
     for(i=0; i<obj.length;i++)    {
          if(obj[i].checked){
                  phonearea = obj[i].value;
          }
     }
	if (phonearea == "US"){
		if (document.account_create.telephone_1.value.length != 3){
			error_count ++;
		}else if(!isNumber(document.account_create.telephone_1.value)){
			error_count ++;
		}
		if (document.account_create.telephone_2.value.length != 3){
			error_count ++;
		}else if(!isNumber(document.account_create.telephone_2.value)){
			error_count ++;
		}
		if (document.account_create.telephone_3.value.length != 4){
			error_count ++;
		}else if(!isNumber(document.account_create.telephone_3.value)){
			error_count ++;
		}
	}else{
		if (document.account_create.telephone.value.length == 0){
			error_count ++;
		}else if(!isNumber(document.account_create.telephone.value)){
			error_count ++;
		}
	}

	if (error_count > 0){
		error_count = 0;
		return false;
	}

	document.account_create.action = "/customer_infocenter/add_account.html";

	document.account_create.submit();

}

function check_my_account(){
	if (document.my_account.firstname.value.length == 0){
		error_count ++;
	}

	if (document.my_account.lastname.value.length == 0){
		error_count ++;
	}

	if (document.my_account.street_address.value.length == 0){
		error_count ++;
	}

	if (document.my_account.city.value.length == 0){
		error_count ++;
	}

	if (document.my_account.postcode.value.length == 0){
		error_count ++;
	}else if(!CheckPost(document.my_account.postcode.value)){
		error_count ++;
	}

	 var obj = document.getElementsByName("phone_area");
     for(i=0; i<obj.length;i++)    {
          if(obj[i].checked){
                  phonearea = obj[i].value;
          }
     }
	if (phonearea == "US"){
		if (document.my_account.telephone_1.value.length != 3){
			error_count ++;
		}else if(!isNumber(document.my_account.telephone_1.value)){
			error_count ++;
		}
		if (document.my_account.telephone_2.value.length != 3){
			error_count ++;
		}else if(!isNumber(document.my_account.telephone_2.value)){
			error_count ++;
		}
		if (document.my_account.telephone_3.value.length != 4){
			error_count ++;
		}else if(!isNumber(document.my_account.telephone_3.value)){
			error_count ++;
		}
	}else{
		if (document.my_account.telephone.value.length == 0){
			error_count ++;
		}else if(!isNumber(document.my_account.telephone.value)){
			error_count ++;
		}
	}

	if (error_count > 0){
		error_count = 0;
		return false;
	}

	document.my_account.action = "/customer_infocenter/my_account_edit_result.html";

	document.my_account.submit();

}

function show_phonearea(num){
	element_id = "area_" + num;
	for (var i=1;i<=2;i++){
		if (i == num){
			document.getElementById(element_id).style.display = "";
			document.getElementById("alert_telephone").style.display = "";
			document.getElementById("alert_area_1").style.display = "none";
			document.getElementById("alert_area_2").style.display = "none";
			document.getElementById("alert_area_3").style.display = "none";
		}else{
			document.getElementById("area_" + i).style.display = "none";
			document.getElementById("alert_area_1").style.display = "";
			document.getElementById("alert_area_2").style.display = "";
			document.getElementById("alert_area_3").style.display = "";
			document.getElementById("alert_telephone").style.display = "none";
		}
	}
}

function check_input_value(info,type,string){
	var element_id = "alert_" + string;
	if (info.length == 0){
		document.getElementById(element_id).innerHTML = "Please Enter Your " + string;
		return false;
	}

	if (type == "normal"){
		document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
	}

	if (type == "area_1"){
		if (info.length != 3){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else if(!isNumber(info)){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "area_2"){
		if (info.length != 3){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else if(!isNumber(info)){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "area_3"){
		if (info.length != 4){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else if(!isNumber(info)){
			document.getElementById(element_id).innerHTML = "Phone format error";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "email"){
		if (!CheckEmail(info)){
			document.getElementById(element_id).innerHTML = "Please Check Your Email Address";
			return false;
		}else{
			createXMLHttpRequest();

		    url = "/customer_infocenter/check_email.html?email=" + info;

		    xmlObj.onreadystatechange = handleStateChange1;

		    xmlObj.open("GET", url, true);

		    xmlObj.send(null);

		}
	}

	if (type == "password"){
		if (info.length < 5){
			document.getElementById(element_id).innerHTML = "Please at least enter 5 characters";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "password_confirm"){

		var password_new = document.getElementById("password-new").value;
		if (info != password_new){
			document.getElementById(element_id).innerHTML = "Password check Error!";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "telephone"){
		if (!isNumber(info)){
			document.getElementById(element_id).innerHTML = "Please Check Your Telephone";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

	if (type == "zip"){
		if (!CheckPost(info)){
			document.getElementById(element_id).innerHTML = "Please Check Your Postcode";
			return false;
		}else{
			document.getElementById(element_id).innerHTML = "<img src='/images/yes.png' border='0' align='absmiddle'>";
		}
	}

}

function handleStateChange1() {

    if(xmlObj.readyState == 4) {

        if(xmlObj.status == 200) {

			div_text = xmlObj.responseText;

			document.getElementById("alert_email_address").innerHTML = "";

			document.getElementById("alert_email_address").innerHTML = div_text;

			div_text = "";

        }

    }

}

function check_forgotten(){
	var email_address = document.forgotten_password.email_address.value;
	var firstname = document.forgotten_password.firstname.value;
	if (email_address.length == 0){
			document.getElementById("alert_email_address").innerHTML = "Please Enter Your Email Address";
			return false;
	}
	if (!CheckEmail(email_address)){
			document.getElementById("alert_email_address").innerHTML = "Please Check Your Email Address";
			return false;
	}
	if (firstname.length == 0){
			document.getElementById("alert_firstname").innerHTML = "Please Enter Your First Name";
			return false;
	}
	document.forgotten_password.action = "/customer_infocenter/get_password_confirm.html";
	document.forgotten_password.submit();

}


function handleStateChange10() {
	if(xmlObj.readyState == 1){
      document.getElementById("ajax_loader").className = "login3top";
	}
    if(xmlObj.readyState == 4) {
        if(xmlObj.status == 200) {
        	div_text = xmlObj.responseText;
        	if (div_text == "Login OK"){
        		location.href="/index.html";
        	}else{
        		document.getElementById("message").style.display = "";
        		document.getElementById("message").innerHTML = "<img src='/images/alert.gif' />" + div_text;
        		document.getElementById("ajax_loader").className = "hide";
        		if (click_count % 2 == 1){
        			document.getElementById("code").src = "/code-rcode.html";
        		}else{
        			document.getElementById("code").src = "/code-scode.html";
        		}
        	}
        }
    }
}

function checkLogging(){
	click_count ++;
	var email_address = document.login.email_address.value;
	var password = document.login.password.value;
	var security_code = document.login.security_code.value
	if (document.login.email_address.value.length == 0){
		document.getElementById("ajax_email").className = "red";
		document.getElementById("ajax_email").innerHTML = "*";
		return false;
	}else if(!CheckEmail(document.login.email_address.value)){
		document.getElementById("ajax_email").className = "red";
		document.getElementById("ajax_email").innerHTML = "Please Check Your Email Address";
	}else{
		document.getElementById("ajax_email").className = "hide";
	}
	if (document.login.password.value.length == 0){
		document.getElementById("ajax_password").className = "red";
		return false;
	}else{
		document.getElementById("ajax_password").className = "hide";
	}
	if (document.login.security_code.value.length == 0){
		document.getElementById("ajax_security").className = "red";
		return false;
	}else{
		document.getElementById("ajax_security").className = "hide";
	}
	createXMLHttpRequest();
	url = "/customer_infocenter/login.html?email_address=" + email_address + "&password=" + password + "&security_code=" + security_code;
    xmlObj.onreadystatechange = handleStateChange10;
    xmlObj.open("GET", url, true);
    xmlObj.send(null);
}

function check_password(){
	var password_old = document.change_password.password.value;
	var password_confirm = document.change_password.password_confirm.value;
	var password_confirm_1 = document.change_password.password_confirm_1.value;
	if (password_old.length == 0){
		document.getElementById("message").style.display = "";
		document.getElementById("message").innerHTML = "<img src='/images/alert.gif' border='0' align='absmiddle'>Please Enter Your Old Password!";
		return false;
	}
	if (password_confirm.length == 0){
		document.getElementById("message").style.display = "";
		document.getElementById("message").innerHTML = "<img src='/images/alert.gif' border='0' align='absmiddle'>Please Enter Your New Password!";
		return false;
	}
	if (password_confirm.length == 0){
		document.getElementById("message").style.display = "";
		document.getElementById("message").innerHTML = "<img src='/images/alert.gif' border='0' align='absmiddle'>Please Enter Your Password Confirm!";
		return false;
	}
	if (password_confirm != password_confirm_1){
		document.getElementById("message").style.display = "";
		document.getElementById("message").innerHTML = "<img src='/images/alert.gif' border='0' align='absmiddle'>Please Check Your New Passowrd And Password Confirm!";
		return false;
	}
	document.change_password.action="/customer_infocenter/change_password_result.html";
	document.change_password.submit();
}
