﻿
// Ensure only numbers in a input box
// Usage = onkeypress="return isNumberKey(event)"
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {

        _gaq.push(['_trackEvent', 'User Error', 'Purchase Quantity', 'User tried to enter a non numeric value into a numeric quantity field.']);

        return false;
    }

    return true;
}



// ensures colour options set and at least 1 quantity is set > 0  when adding to cart
// Usage = onsubmit="return checkAdd2Cart();"
function checkAdd2Cart(){
    var arr = [];
    var errors = false;
    var itemsSelected = false;

    // check colours
    $("select[id^='colour_']").each(function () {
        if (this.style.color == 'red') { errors = true; }
    });

    // check at least 1 quantity >0
    $("input[id^='productid_']").each(function () {
        if (this.value != '0') { itemsSelected = true; }
    });

    if (errors || !itemsSelected) {

        _gaq.push(['_trackEvent', 'User Error', 'Colour Drop Down', 'User failed to select a colour or quantity.']);

        return false;
    }
    else {

        _gaq.push(['_trackEvent', 'Cart', 'Items added', 'User added Items to the cart.']);

        return true;
    }
}



///** 
//* Override the Alert function, use facebox instead.
//*/
//(function () {
//    nalert = window.alert;
//    Type = {
//        native: 'native',
//        custom: 'custom'
//    };
//})();



///**
//* Factory method for calling alert(). 
//* It will be call a native alert() or a custom redefined alert() by a Type param.
//* This definition needed for IE
//*/
//(function (proxy) {

//    proxy.alert = function () {
//        var message = (!arguments[0]) ? 'null' : arguments[0];
//        var type = (!arguments[1]) ? '' : arguments[1];

//        if (type && type == 'native') {
//            nalert(message);
//        }
//        else {
//            $.facebox('' + message + '');
//        }
//    };
//})(this);

///** 
//* /Override the Alert function, use facebox instead.
//*/





function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email) == false) {
        return false;
    }
    return true;
}



function toProperCase(s) {
    return s.toLowerCase().replace(/^(.)|\s(.)/g,
          function ($1) { return $1.toUpperCase(); });
}


