function findElementIdSmart(id) {
    var d = document;
    return d.getElementById ? d.getElementById(id) : (d.all ? d.all[id] : (d.layers ? d.layers[id] : null));
}

function OnProductPriceClientSelectedIndexChangedEventHandler(sender, args) {
    // sender represents the combobox that has fired the event
    // the code below obtains the item that has been changed
    // var item = args.get_item();
    // var valsArr = item.get_value().split('|');
    // findElementIdSmart(item.get_comboBox().get_id() + '_Price').innerHTML = '$' + valsArr[0];
    // findElementIdSmart(item.get_comboBox().get_id() + '_HRef').href = valsArr[1];

    var qtyCtrl = findElementIdSmart(sender.get_id() + '_Qty');
    UpdateProductVariantPrice(sender, qtyCtrl.control.get_value());
}

function OnProductQtyClientValueChanged(sender, args) {
    // we need infromation about currently selected item in the combobox
    var comboCtrlId = sender._clientID.substring(0, sender._clientID.length - 4);
    var comboCtrl = findElementIdSmart(comboCtrlId);

    UpdateProductVariantPrice(comboCtrl.control, args.get_newValue());
}

function UpdateProductVariantPrice(variantsComboBox, qty) {
    if (qty < 1)
        qty = 1;
    var selectedVariant = variantsComboBox.get_selectedItem();
    var valsArr = selectedVariant.get_value().split('|');

    var itemPrice = parseFloat(valsArr[0]);
    var totalPrice = itemPrice * qty;
    var totalPriceWithDiscount = GetTotalpriceWithQtyDiscount(totalPrice, qty);
    findElementIdSmart(variantsComboBox.get_id() + '_Price').innerHTML = '$' + NumberToMoneyString(totalPriceWithDiscount);

    // valsArr - 3 items - price:productid:variantid. The Buy Now link will be created for AddToCart mode
    // valsArr - 2 items - price:static url. The Buy Now link will be created for the Static Url
    var buyUrl;
    
    if (valsArr.length == 3) {
        var productId = valsArr[1];
        var variantId = valsArr[2];

        buyUrl = "https://www.s-code.com/store/addtocart.aspx?productid=" + productId + "&variantid=" + variantId + "&quantity=" + qty;
    } 
    else {
        buyUrl = valsArr[1];
    }

    findElementIdSmart(variantsComboBox.get_id() + '_HRef').href = buyUrl;
}

function OnUpdateSelectedVariantQtyPrice(controlId) {
    var comboCtrlParent = findElementIdSmart(controlId + '_LicenseComboBox');
    var comboCtrl = $find(controlId + '_LicenseComboBox');
    var qtyCtrl = findElementIdSmart(controlId + '_LicenseComboBox_Qty');

    //UpdateProductVariantPrice(comboCtrl.control, qtyCtrl.value);
}

function GetTotalpriceWithQtyDiscount(price, qty) {
    if (qty < 5)
        return price;
    else if (qty < 10)
        return price * 0.9;
    else if (qty < 15)
        return price * 0.8;
    else if (qty < 20)
        return price * 0.7;
    
    return price * 0.6;
}

//function OnProductPriceOrderNotesChanging(sender, args) {
//}

function NumberToMoneyString(amount) {
    var decimalSeparator = Number("1.2").toLocaleString().substr(1, 1);

    var amountWithCommas = amount.toLocaleString();
    var arParts = String(amountWithCommas).split(decimalSeparator);
    var intPart = arParts[0];
    var decPart = (arParts.length > 1 ? arParts[1] : '');
    decPart = (decPart + '00').substr(0, 2);

    return intPart + decimalSeparator + decPart;
}
// Removes leading whitespaces
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim(str)
{
	if (str == null)
		return false;
   return str.replace(/^\s*|\s*$/g,"");
}

//function IsNumeric(sText)
//{
//	if (isNaN(parseInt(form.field1.value))) {
//		alert('Please enter only numerical values into Field 1.');
//	}
//}

function IsNumeric(sText)
{
	sText = trim(sText);
	if (sText.length == 0)
		return false;

	var validChars = "0123456789";
	var charAt;
			
	for (i = 0; i < sText.length; i++)
	{
	    charAt = sText.charAt(i);
	    if (validChars.indexOf(charAt) == -1) 
		{
			return false;
		}
	}
	return true;
}

//validates if text box is not empty and shows a message box if it's
function vaidate_has_text(ctrl, errorText)
{
	var len = trim(ctrl.value).length;
	if (len == 0) 
	{
		ctrl.focus();
		return false;
	}
	else
	{	
		return true;
	}
}

//validates if text box has numeric value and shows a message box if it's
function vaidate_has_num(ctrl, errorText)
{
	if (IsNumeric(ctrl.value) == false)
	{
		ctrl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

//inverses checkbox check state
function OnInverseCheckbox(checkboxID) {
	var ctrl = document.getElementById(checkboxID);
	ctrl.checked = !ctrl.checked;
}


if (document.images) {
    var footer_cust_logo1_gray = new Image();
    var footer_cust_logo1_color = new Image();

    var footer_cust_logo2_gray = new Image();
    var footer_cust_logo2_color = new Image();

    var footer_cust_logo3_gray = new Image();
    var footer_cust_logo3_color = new Image();

    var footer_cust_logo4_gray = new Image();
    var footer_cust_logo4_color = new Image();

    var footer_cust_logo5_gray = new Image();
    var footer_cust_logo5_color = new Image();

    var footer_cust_logo6_gray = new Image();
    var footer_cust_logo6_color = new Image();

    var footer_cust_logo7_gray = new Image();
    var footer_cust_logo7_color = new Image();

    var footer_cust_logo8_gray = new Image();
    var footer_cust_logo8_color = new Image();

    var footer_cust_logo9_gray = new Image();
    var footer_cust_logo9_color = new Image();

    var footer_cust_logo10_gray = new Image();
    var footer_cust_logo10_color = new Image();

    var footer_cust_logo11_gray = new Image();
    var footer_cust_logo11_color = new Image();

    var footer_cust_logo12_gray = new Image();
    var footer_cust_logo12_color = new Image();

    var footer_cust_logo13_gray = new Image();
    var footer_cust_logo13_color = new Image();

    var footer_cust_logo14_gray = new Image();
    var footer_cust_logo14_color = new Image();

    var footer_cust_logo15_gray = new Image();
    var footer_cust_logo15_color = new Image();

    var footer_cust_logo16_gray = new Image();
    var footer_cust_logo16_color = new Image();

//    var footer_cust_logo17_gray = new Image();
//    var footer_cust_logo17_color = new Image();

//    var footer_cust_logo18_gray = new Image();
//    var footer_cust_logo18_color = new Image();

//    var footer_cust_logo19_gray = new Image();
//    var footer_cust_logo19_color = new Image();

    var footer_cust_logo20_gray = new Image();
    var footer_cust_logo20_color = new Image();

    var footer_cust_logo21_gray = new Image();
    var footer_cust_logo21_color = new Image();
    
    var footer_cust_logo22_gray = new Image();
    var footer_cust_logo22_color = new Image();
}

function loadFooterColorImages() {
    if (document.images) {
        footer_cust_logo1_gray.src = "/images/site/logo/microsoft18_gray.png";
        footer_cust_logo1_color.src = "/images/site/logo/microsoft18.png";

        footer_cust_logo2_gray.src = "/images/site/logo/dell18_gray.png";
        footer_cust_logo2_color.src = "/images/site/logo/dell18.png";

        footer_cust_logo3_gray.src = "/images/site/logo/google18_gray.png";
        footer_cust_logo3_color.src = "/images/site/logo/google18.png";

        footer_cust_logo4_gray.src = "/images/site/logo/intel18_gray.png";
        footer_cust_logo4_color.src = "/images/site/logo/intel18.png";

        footer_cust_logo5_gray.src = "/images/site/logo/symantec18_gray.png";
        footer_cust_logo5_color.src = "/images/site/logo/symantec18.png";

        footer_cust_logo6_gray.src = "/images/site/logo/seagate18_gray.png";
        footer_cust_logo6_color.src = "/images/site/logo/seagate18.png";

        footer_cust_logo7_gray.src = "/images/site/logo/emc18_gray.png";
        footer_cust_logo7_color.src = "/images/site/logo/emc18.png";

        footer_cust_logo8_gray.src = "/images/site/logo/rackspace18_gray.png";
        footer_cust_logo8_color.src = "/images/site/logo/rackspace18.png";

        footer_cust_logo9_gray.src = "/images/site/logo/boeing18_gray.png";
        footer_cust_logo9_color.src = "/images/site/logo/boeing18.png";

        footer_cust_logo10_gray.src = "/images/site/logo/dreamworks18_gray.png";
        footer_cust_logo10_color.src = "/images/site/logo/dreamworks18.png";

        footer_cust_logo11_gray.src = "/images/site/logo/fedex18_gray.png";
        footer_cust_logo11_color.src = "/images/site/logo/fedex18.png";

        footer_cust_logo12_gray.src = "/images/site/logo/ikea18_gray.png";
        footer_cust_logo12_color.src = "/images/site/logo/ikea18.png";

        footer_cust_logo13_gray.src = "/images/site/logo/mazda18_gray.png";
        footer_cust_logo13_color.src = "/images/site/logo/mazda18.png";

        footer_cust_logo14_gray.src = "/images/site/logo/puma18_gray.png";
        footer_cust_logo14_color.src = "/images/site/logo/puma18.png";

        footer_cust_logo15_gray.src = "/images/site/logo/tissot18_gray.png";
        footer_cust_logo15_color.src = "/images/site/logo/tissot18.png";

        footer_cust_logo16_gray.src = "/images/site/logo/tetrapak18_gray.png";
        footer_cust_logo16_color.src = "/images/site/logo/tetrapak18.png";

//        footer_cust_logo17_gray.src = "/images/site/logo/us_army18_gray.png";
//        footer_cust_logo17_color.src = "/images/site/logo/us_army18.png";

//        footer_cust_logo18_gray.src = "/images/site/logo/us_courts18_gray.png";
//        footer_cust_logo18_color.src = "/images/site/logo/us_courts18.png";

//        footer_cust_logo19_gray.src = "/images/site/logo/genesys18_gray.png";
//        footer_cust_logo19_color.src = "/images/site/logo/genesys18.png";

        footer_cust_logo20_gray.src = "/images/site/logo/rim18_gray.png";
        footer_cust_logo20_color.src = "/images/site/logo/rim18.png";

        footer_cust_logo21_gray.src = "/images/site/logo/toyota18_gray.png";
        footer_cust_logo21_color.src = "/images/site/logo/toyota18.png";
        
        footer_cust_logo22_gray.src = "/images/site/logo/petronas18_gray.png";
        footer_cust_logo22_color.src = "/images/site/logo/petronas18.png";
    }
}

function activateImage(imgObj, imgName) {
  if (document.images) {
    if ( eval(imgName + "_color.complete") ) {
        imgObj.src = eval(imgName + "_color.src");
    }
  }
}

function deactivateImage(imgObj, imgName) {
    if (document.images) {
        if (eval(imgName + "_gray.complete")) {
            imgObj.src = eval(imgName + "_gray.src");
        }
    }
}
