if(!Array.indexOf){
   Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
         if(this[i]===obj){
            return i;
         }
      }
      return -1;
   }
}
//Plans page, register.php (?act=step1). Switches mode to the selected register time period and scrolls down.
$(document).ready(function(){
    $('.fancybox').fancybox();
    // Saves original URLs addresses to restore later on.
    $('a.plan-item').each(function(){
        this.srcHref = this.href;
    });

	$('#plans-period li').click(function(){
        var period;
		$('#plans-period li.selected').removeClass('selected');
		$(this).addClass('selected');
		$('html,body').animate({scrollTop: $('#plans-price-a').offset().top},'slow');
		switch($(this).attr('id')){
			case 'plan-period-1':
                period = 1;
				$('#plans-prices-wrapper').animate({height:'155px'});
				$('#plans-prices-inner').animate({top:'0px'});	
				break;
			case 'plan-period-2':
                period = 12;
				$('#plans-prices-wrapper').animate({height:'192px'});
				$('#plans-prices-inner').animate({top:'-155px'});
				break;
			case 'plan-period-3':
                period = 24;
				$('#plans-prices-wrapper').animate({height:'192px'});
				$('#plans-prices-inner').animate({top:'-347px'});
				break;
		}

        // Sets links to pass period param.
        $('a.plan-item').each(function(){
            this.href = this.srcHref + '&period=' + period;
        });
	});
});
function GetXmlHttpObject() {
    var objXMLHttp = null;
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}



function goToTrial(url)
{
    document.location.href='http://www.izzonet.com/free-trial';
}

function goToPlans(url)
{
    document.location.href='http://www.izzonet.com/plans';
}


function RunURL(url) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    if (xmlHttp) {
        xmlHttp.open('POST', url, false);
        xmlHttp.send(null);
        return xmlHttp.responseText;
    } else {
        return "Error - Please try again";
    }
}

function showRequest(url, fid, sid) {
    if (document.getElementById(fid).style.visibility == 'collapse') {
        document.getElementById(fid).style.visibility = 'visible';
        document.getElementById(sid).innerHTML = RunURL(url);
    } else {
        document.getElementById(fid).style.visibility = 'collapse';
    }
}

function updateRequest(url, fid, sid) {
    if (document.getElementById(fid).style.visibility == 'collapse') {
        document.getElementById(fid).style.visibility = 'visible';
    }

    document.getElementById(sid).innerHTML = RunURL(url);
}

function showRequestByVal(url, fid, sid) {
    document.getElementById(sid).innerHTML = RunURL(url + '&val='
        + document.getElementById(fid).selectedIndex);
}

function sureOp(url) {
    confirmObj = confirm("Do you really want to delete ?");
    if (confirmObj) {
        document.location.href = url;
    }
    return false;
}

function sureOp2(url,txt) {
    confirmObj = confirm(txt);
    if (confirmObj) {
        document.location.href = url;
    }
    return false;
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.-";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}


function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function IsValidPhoneNumber(sText) {
    var ValidChars = "0123456789.-()+ ";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}
function checkMail(mail) {
    var x = mail;
    var at = x.indexOf("@");
    var dot = x.lastIndexOf(".");
    if ((at < 1) || (dot <= (at + 2)) || ((dot + 2) >= x.length)) {
        return false;
    }
    else {
        return true;
    }
}
function checkMultiMail(mail) {
    if(mail.indexOf(';') == -1) {
        return checkMail(mail);
    }
    else {
       var mailArr =  mail.split(';');
        for(var i = 0; i < mailArr.length; i++) {
            if(i+1 == mailArr.length && mailArr[i] == '') continue; // allows ; at the end of field
            if(!checkMail(mailArr[i])) {
               return false;
            }
        }
        return true;
    }
}
function checkMailOLD(mail) {
    var x = mail;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return (filter.test(x));
}
function switchDisplay(objId) {
    if (document.getElementById(objId).style.display == 'none') {

        document.getElementById(objId).style.display = '';

    } else {
        document.getElementById(objId).style.display = 'none';
    }
    if(document.getElementById(objId).style.visibility == 'collapse') {
        document.getElementById(objId).style.visibility = 'visible';
        document.getElementById(objId).style.display = '';
    }
    else {
        if(document.getElementById(objId).style.visibility == 'visible') {
            document.getElementById(objId).style.visibility = 'collapse';
            document.getElementById(objId).style.display = 'none';
        }
    }
}
function switchDisplay2(objId, size, color) {
    for ( var i = 1; i <= size; i++) {
        if (document.getElementById(i)) {
            document.getElementById(i).style.display = 'none';
        }
        if (document.getElementById('d' + i)) {
            document.getElementById('d' + i).style.backgroundColor = '#FFFFFF';
        }
    }
    if (document.getElementById(objId).style.display == 'none') {
        document.getElementById(objId).style.display = 'block';
        document.getElementById('d' + objId).style.backgroundColor = color;
    }
}
function clearAll(num) {
    var confirmQ = confirm('Are you sure you want to clear all the suppliers\' data?');
    if(confirmQ) {
        for ( var i = 1; i <= num; i++) {
            document.getElementById('cost' + i).value = '';
            document.getElementById('ship' + i).value = '';
            document.getElementById('reship' + i).value = '';
        }
    }
}

/* ================================================================================================ */

/*
 * 
 * This routine checks the credit card number. The following checks are made:
 * 
 * 1. A number has been provided 2. The number is a right length for the card 3.
 * The number has an appropriate prefix for the card 4. The number has a valid
 * modulus 10 number check digit if required
 * 
 * If the validation fails an error is reported.
 * 
 * The structure of credit card formats was gleaned from a variety of sources on
 * the web, although the best is probably on Wikepedia ("Credit card number"):
 * 
 * http://en.wikipedia.org/wiki/Credit_card_number
 * 
 * Parameters: cardnumber number on the card cardname name of card as defined in
 * the card list below
 * 
 * Author: John Gardner Date: 1st November 2003 Updated: 26th Feb. 2005
 * Additional cards added by request Updated: 27th Nov. 2006 Additional cards
 * added from Wikipedia Updated: 18th Jan. 2008 Additional cards added from
 * Wikipedia Updated: 26th Nov. 2008 Maestro cards extended Updated: 19th Jun.
 * 2009 Laser cards extended from Wikipedia Updated: 11th Sep. 2010 Typos
 * removed from Diners and Solo definitions (thanks to Noe Leon)
 * 
 */

/*
 * If a credit card number is invalid, an error reason is loaded into the global
 * ccErrorNo variable. This can be be used to index into the global error string
 * array to report the reason to the user if required:
 * 
 * e.g. if (!checkCreditCard (number, name) alert (ccErrors(ccErrorNo);
 */

var ccErrorNo = 0;
var ccErrors = new Array();

function checkCreditCard(cardnumber, cardname) {

    ccErrors[0] = "Unknown card type";
    ccErrors[1] = "No card number provided";
    ccErrors[2] = "Credit card number is in invalid format";
    ccErrors[3] = "Credit card number is invalid";
    ccErrors[4] = "Credit card number has an inappropriate number of digits";

    // Array to hold the permitted card characteristics
    var cards = new Array();

    // Define the cards we support. You may add addtional card types as follows.

    // Name: As in the selection box of the form - must be same as user's
    // Length: List of possible valid lengths of the card number for the card
    // prefixes: List of possible prefixes for the card
    // checkdigit: Boolean to say whether there is a check digit

    cards[0] = {
        name : "Visa",
        length : "13,16",
        prefixes : "4",
        checkdigit : true
    };
    cards[1] = {
        name : "MasterCard",
        length : "16",
        prefixes : "51,52,53,54,55",
        checkdigit : true
    };
    cards[2] = {
        name : "DinersClub",
        length : "14,16",
        prefixes : "305,36,38,54,55",
        checkdigit : true
    };
    cards[3] = {
        name : "CarteBlanche",
        length : "14",
        prefixes : "300,301,302,303,304,305",
        checkdigit : true
    };
    cards[4] = {
        name : "AmericanExpress",
        length : "15",
        prefixes : "34,37",
        checkdigit : true
    };
    cards[5] = {
        name : "Discover",
        length : "16",
        prefixes : "6011,622,64,65",
        checkdigit : true
    };
    cards[6] = {
        name : "JCB",
        length : "16",
        prefixes : "35",
        checkdigit : true
    };
    cards[7] = {
        name : "enRoute",
        length : "15",
        prefixes : "2014,2149",
        checkdigit : true
    };
    cards[8] = {
        name : "Solo",
        length : "16,18,19",
        prefixes : "6334,6767",
        checkdigit : true
    };
    cards[9] = {
        name : "Switch",
        length : "16,18,19",
        prefixes : "4903,4905,4911,4936,564182,633110,6333,6759",
        checkdigit : true
    };
    cards[10] = {
        name : "Maestro",
        length : "12,13,14,15,16,18,19",
        prefixes : "5018,5020,5038,6304,6759,6761",
        checkdigit : true
    };
    cards[11] = {
        name : "VisaElectron",
        length : "16",
        prefixes : "417500,4917,4913,4508,4844",
        checkdigit : true
    };
    cards[12] = {
        name : "LaserCard",
        length : "16,17,18,19",
        prefixes : "6304,6706,6771,6709",
        checkdigit : true
    };

    // Establish card type
    var cardType = -1;
    for ( var i = 0; i < cards.length; i++) {

        // See if it is this card (ignoring the case of the string)
        if (cardname.toLowerCase() == cards[i].name.toLowerCase()) {
            cardType = i;
            break;
        }
    }

    // If card type not found, report an error
    if (cardType == -1) {
        ccErrorNo = 0;
        return false;
    }

    // Ensure that the user has provided a credit card number
    if (cardnumber.length == 0) {
        ccErrorNo = 1;
        return false;
    }

    // Now remove any spaces from the credit card number
    cardnumber = cardnumber.replace(/\s/g, "");

    // Check that the number is numeric
    var cardNo = cardnumber;
    var cardexp = /^[0-9]{13,19}$/;
    if (!cardexp.exec(cardNo)) {
        ccErrorNo = 2;
        return false;
    }

    // Now check the modulus 10 check digit - if required
    if (cards[cardType].checkdigit) {
        var checksum = 0; // running checksum
        // total
        var mychar = ""; // next char to process
        var j = 1; // takes value of 1 or 2

        // Process each digit one by one starting at the right
        var calc;
        for (i = cardNo.length - 1; i >= 0; i--) {

            // Extract the next digit and multiply by 1 or 2 on alternative
            // digits.
            calc = Number(cardNo.charAt(i)) * j;

            // If the result is in two digits add 1 to the checksum total
            if (calc > 9) {
                checksum = checksum + 1;
                calc = calc - 10;
            }

            // Add the units element to the checksum total
            checksum = checksum + calc;

            // Switch the value of j
            if (j == 1) {
                j = 2;
            } else {
                j = 1;
            }
        }

        // All done - if checksum is divisible by 10, it is a valid modulus 10.
        // If not, report an error.
        if (checksum % 10 != 0) {
            ccErrorNo = 3;
            return false;
        }
    }

    // The following are the card-specific checks we undertake.
    var LengthValid = false;
    var PrefixValid = false;

    // We use these for holding the valid lengths and prefixes of a card type
    var prefix = new Array();
    var lengths = new Array();

    // Load an array with the valid prefixes for this card
    prefix = cards[cardType].prefixes.split(",");

    // Now see if any of them match what we have in the card number
    for (i = 0; i < prefix.length; i++) {
        var exp = new RegExp("^" + prefix[i]);
        if (exp.test(cardNo))
            PrefixValid = true;
    }

    // If it isn't a valid prefix there's no point at looking at the length
    if (!PrefixValid) {
        ccErrorNo = 3;
        return false;
    }

    // See if the length is valid for this card
    lengths = cards[cardType].length.split(",");
    for (j = 0; j < lengths.length; j++) {
        if (cardNo.length == lengths[j])
            LengthValid = true;
    }

    // See if all is OK by seeing if the length was valid. We only check the
    // length if all else was
    // hunky dory.
    if (!LengthValid) {
        ccErrorNo = 4;
        return false;
    }
    ;

    // The credit card is in the required format.
    return true;
}

function updatestock(stockinfo,quickview){
    if (stockinfo.value == '-1'){
        document.getElementById('cartaddition').innerHTML = 'Out Of Stock';
    } else {
        if(quickview == 'yes'){
            document.getElementById('cartaddition').innerHTML = '<input class="action_button" type="submit" value="Add to cart" />';
        } else {
            document.getElementById('cartaddition').innerHTML = '<a href="javascript:submitForm();"><span itemprop="availability" content="in_stock"><span class="add_to_cart">Add To Shopping Cart</span></span></a>';
        }
    }
}
function getEditorValue(editor_id,id){
    if(editor_id===undefined){
            var editor = document.getElementById(editor_id);
    return editor.getHTML().replace(/(<([^>]+)>)/ig,'');
    }elseif(id===undefined)
    {
    var editor = document.getElementById('CE_'+editor_id+'_ID');
    return editor.getHTML().replace(/(<([^>]+)>)/ig,'');
    }
}
function setFocus(editor_id,id){
      if(editor_id===undefined){
    var editor = document.getElementById(editor_id);
    editor.FocusDocument();
      }elseif(id===undefined)
    {
        var editor = document.getElementById('CE_'+editor_id+'_ID');
    editor.FocusDocument();
    }
}


function quickview(itemid){
     document.getElementById('quickviewdiv').innerHTML = RunURL('/product.php?act=quickview&id='+itemid);
    window.scrollTo(0,100);
}
function removequickview() {
     document.getElementById('quickviewdiv').innerHTML = '' ;

}
function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}
function loading(title,text){
	if(text==undefined||text=='')
		text='Please wait<br/>(may take awhile)';
	if(title==undefined||title=='')
		title='Loading';
	if($('#loader-div').length==0){
		$('body').append('<div style="display:none"><div id="loader-div" style="text-align:center;"><img src="images/ajax-loader.gif" style="padding:15px;"/><br/><strong style="font-size:16px;">'+title+'</strong><br/><span>'+text+'</span></div></div>');
	}
	$("#loader-div").attr("href","#loader-div").fancybox({
		modal:true,
		centerOnScroll: true
	}).click();
}
function checkWidgetForm(){
	loading();
}
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
/* ================================================================================================ */


