/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, regexp: true, undef: true, white: true, indent: 4 */
/*global CartwrightUtil*/
/*global ActiveXObject*/
/*global window*/
  
  
  /* Code for displaying part info */


var CartwrightParts = {

    partinf: function(id) {
        // Do AJAX fun
        var xmlHttp;
        if (!(xmlHttp = new CartwrightUtil.Xhr())) { // Create AJAX object and check it exists, if not open in normal window
            open("partinf.asp?id=" + id + ",config='width=680, height=650, toolbar=no, menubar=no, scrollbars=1, resizable=yes,location=no, directories=no, status=no'");
            return false;
        }
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
                document.getElementById("parttext").innerHTML = xmlHttp.responseText;
            }
        };

        var lb = new CartwrightUtil.BgFade(document.getElementById("parttext"));
        lb.closeimage = 'images/close.png';
        lb.bgimage = 'images/80.png';
        lb.width = '600px';
        lb.height = '500px';
        lb.show();
        document.getElementById("parttext").innerHTML = 'Loading...';
        var geturl = 'partinf1.asp?id=' + id;
        xmlHttp.open("GET", geturl, true);
        xmlHttp.send(null);
        return false; // Prevent link being clicked if everything worked out
    },

    /* end part info */




    /*
    * Checks that the email being used on the account details update page is valid and matches
    */

    checkemail: function() {
        var sub = 1,
            infform = document.forms["det"]; // Form being checked
        infform.demail.style.background = '#EEEEEE';
        infform.demail2.style.background = '#EEEEEE';

        if (infform.demail.value !== infform.demail2.value) {
            alert("Email and confirm email do not match!");
            infform.demail2.style.background = '#ff8080';
            sub = 0;
        }
        if (sub === 1 && !CartwrightUtil.isValidEmail(infform.demail.value)) {
            alert("Please enter a valid email address");
            infform.demail.style.background = '#ff8080';
            infform.demail2.style.background = '#ff8080';
            sub = 0;
        }
        if (sub === 1) {
            infform.submit();
        }
    },

    /* submission function */

    updatedel: function() {
        document.forms['updatedelf'].submit();
    },

    updatedisc: function() {
        document.forms['updatediscb'].submit();
    },



    /*
    *  Checks if the payment option is valid based on the delivery type, and if the user is logged in (stored in loggedin argument)
    */

    checkpayment: function(loggedin) {
        var payform = document.forms['pay'],
	        a = parseInt(payform.payment.value, 10),
	        deloption = document.forms['updatedelf'].deloptionb.value,
		    writeele = document.getElementById("checkpay");

        writeele.innerHTML = "";
        if (loggedin === "") {
            if (a === 3) {
                writeele.innerHTML = "Only available when logged in. Please log in or call 0161 925 2226 to set up an account";
                payform.payment.value = 1;
            }
        }
        if (loggedin !== "") {
            if (a === 3 && deloption === "9|0") {
                writeele.innerHTML = "Cannot pay using PO as delivery price cannot be confirmed.";
                payform.payment.value = 1;
            }
            if (a === 2 && deloption === "9|0") {
                writeele.innerHTML = "Cannot pay using credit card as delivery price cannot be confirmed.";
                payform.payment.value = 1;
            }
        }
    },



    /*
    *  Called from the basket page, checks all the input is valid and if so, submits the form
    */
    checkdata: function() {
        var sub = 1,                           // Contains if the form is valid
	        m = "",                            // Error message
	        badmail = 0,                       // Contains if the email is bad
	        payform = document.forms['pay'];   // Holds the form

        if (payform.name.value === "") {
            m += "\nPlease enter your contact name";
            payform.name.style.background = '#ff8080';
            sub = 0;
        } else {
            payform.name.style.background = '#EEEEEE';
        }
        if (payform.company.value === "") {
            m += "\nPlease enter your company name";
            payform.company.style.background = '#ff8080';
            sub = 0;
        } else {
            payform.company.style.background = '#EEEEEE';
        }

        if (payform.email.value !== payform.cemail.value) {
            badmail = 1;
            sub = 0;
            m += "\nPlease check that the email addresses you have entered match";
            payform.email.style.background = '#ff8080';
            payform.cemail.style.background = '#ff8080';
        }


        if (badmail === 0 && !CartwrightUtil.isValidEmail(payform.email.value)) {
            m += "\nPlease enter a valid email address";
            payform.email.style.background = '#ff8080';
            payform.cemail.style.background = '#ff8080';
            sub = 0;
        } else {
            if (badmail === 0) {
                payform.email.style.background = '#EEEEEE';
                payform.cemail.style.background = '#EEEEEE';
            }
        }

        if ((payform.DelAdddressLine1.value === "" || payform.DelAdddressCity.value === "" || payform.DelAdddressPostcode.value === "") && document.forms['updatedelf'].deloptionb.value !== "0|0") {
            m += "\nPlease enter delivery address line 1, town/city and postcode or select collection as the delivery method";
            payform.DelAdddressLine1.style.background = '#ff8080';
            payform.DelAdddressCity.style.background = '#ff8080';
            payform.DelAdddressPostcode.style.background = '#ff8080';
            sub = 0;
        } else {
            payform.DelAdddressLine1.style.background = '#EEEEEE';
            payform.DelAdddressCity.style.background = '#EEEEEE';
            payform.DelAdddressPostcode.style.background = '#EEEEEE';
        }
        if (payform.email.value === "") {
            m += "\nPlease enter and confirm email address";
            payform.email.style.background = '#ff8080';
            payform.cemail.style.background = '#ff8080';
            sub = 0;
        }

        if (payform.confirmtandcs.checked === false) {
            m += "\nPlease tick the box to confirm you have read and accept our terms and conditions";
            sub = 0;
        }


        if (sub === 1) {
            payform.submit();
        } else {
            alert(m);
        }
    }
}