/**
 *  JS Functions Dec '09. Kevin Leitch. kevin.leitch@mossplastics.com
 */
window.onload = function() {
    //ToggleDiv("catorder", "catrequest");
	//refCheck();
//checkURL();
}

function ToggleDiv(divID,linkID) {
    if (!document.getElementById) return false;
    var catViewStatus = document.getElementById(divID);
    var catLink = document.getElementById(linkID);
    catViewStatus.style.display = "none"	
	catLink.onclick = function() {
	    catViewStatus.style.display = (catViewStatus.style.display == 'block') ? "none" : "block"; 
	    return false;
	}
}


function refCheck() {
	var myArray = []; 
	myArray[0]= 'http://www.plastic-parts.co.uk'; 
	myArray[1]= 'http://ppc.mossplastics.com/'; 
	var currentreferrer=document.referer; 
alert(currentreferrer);
	var i; 
	for (i=0;i<myArray.length;i++) 
	if (myArray[i] == currentreferrer) { 
		alert("Plastic Parts Centre is now called Moss Express"); 
	}

}


function checkURL() {
var origin=window.location;
var str="plastic-parts"; 
var patt1="/" + window.location + "/gi";
alert(str.match(patt1));
}


function validateFormOnSubmit(theForm) {
    var reason = "";

    reason += validateFName(theForm.fname);
    reason += validateLName(theForm.lname);
    reason += validateEmail(theForm.email);
    reason += validateTel(theForm.tel);
    reason += validateAdd(theForm.address1);
    reason += validateTown(theForm.town);
    reason += validateCounty(theForm.county);
    reason += validatePCode(theForm.pcode);

    if (reason != "") {
        alert("Some fields need correction:\n" + reason);
        return false;
    }

    // All fields are filled correctly so send via AJAX

    //if (navigator.appName == "Microsoft Internet Explorer") {
       // var http = new ActiveXObject("Microsoft.XMLHTTP");
    //} else {
       // var http = new XMLHttpRequest();
   // }
    var thebody = "First Name: " + theForm.fname.value + ", Last Name: " + theForm.lname.value + " ,Email: " + theForm.email.value + " ,Tel. No.: " + theForm.tel.value + " ,Address: " + theForm.address1.value + ", " + theForm.address2.value + ", " + theForm.address3.value + ", " + theForm.town.value + ", " + theForm.county.value + ", " + theForm.pcode.value;
    //alert(thebody);
	//http.open("GET", "mailto:kevin.leitch@mossplastics.com?subject=Cat Request&body=thebody", true);
       // http.onreadystatechange = function() {
          //  if (http.readyState == 4) {
                //document.getElementById("responseDiv").innerHTML = http.responseText;
          //  }
        //}

    // end AJAX send

	location.href = "mailto:kevin.leitch@mossplastics.com?subject=Catalogue Request&body=" + thebody;
	//document.getElementById("responseDiv").innerHTML = "Thanks";
	alert("Thanks, your email will soon start up. Press 'send' to complete your catalogue order.")
    theForm.fname.value = "";
	theForm.lname.value = "";
	theForm.email.value = "";
	theForm.tel.value = "";
	theForm.address1.value = "";
	theForm.address2.value = "";
	theForm.address3.value = "";
	theForm.town.value = "";
	theForm.county.value = "";
	theForm.pcode.value = "";
    //return false;
}
function validateFName(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a First Name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLName(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a Last Name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateTel(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a Tel. No..\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateAdd(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered an address.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateTown(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a Town Name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCounty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a County Name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePCode(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "You have not entered a Post Code.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error = "";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}