// create a way to compare two arrays for equality, where [1,2] == [2,1]
Array.prototype.compareArrays = function(arr) {
    if (this.length != arr.length) return false;
    for (var i = 0; i < arr.length; i++) {
        if (this[i].compareArrays) { //likely nested array
            if (!this[i].compareArrays(arr[i])) return false;
            else continue;
        }
        // loop through all elements to check 
        var bFound = false;
        for (var x = 0; x < arr.length; x++) {          
          for (var y = 0; y < this.length; y++) {           
            if (trim(arr[x]) == trim(this[y])) {
              bFound = true;
              break;
            }
          }
          if (!bFound)
            return false;
          bFound = false;
        }
    }
    return true;
}
// create a way to check if an element exists in an array
Array.prototype.exists = function (x) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == x) return true;
    }
    return false;
}
// create some trim functions
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

// create a string Right function since javascript doesn't give us one
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
// and a left
function Left(str, n){
        if (n <= 0)
            return "";
        else if (n > String(str).length)
            return str;
        else
            return String(str).substring(0,n);
}

// add a selected option to a string list of all options selected so far.
function addOptionToList(obj) {
    if (obj.value && (obj.value != -1)) { // ignore if they go back to the default "select me" option
        var value = Right(obj.value, obj.value.length-obj.value.indexOf("^")-1);
        var opt = Left(obj.value, obj.value.indexOf("^")-1);
        var bFound = false;
        for (x=0; x < optionsSelectedArray.length; x++) {
            if (optionsSelectedArray[x].opt == opt) {
            // this option already exists in the array, remove it and insert the arguement instead
                var o = new Object();
                o.opt = opt;
                o.value = value;
                optionsSelectedArray[x] = o;   
                bFound = true;
            }
        }
        // new option
        if (!bFound) {
            var o = new Object();
            o.opt = opt;
            o.value = value;
            optionsSelectedArray.push(o);
        }
    }
}

// loop through the array of selected options, since it contains objects, and build just an array of the values 
//that have been selected
function buildArrayOfSelectedOptions() {
    var theArray = new Array();
    for (x = 0; x < optionsSelectedArray.length; x++) {
        theArray.push(optionsSelectedArray[x].value);
    }
    return theArray;
}

function checkChildren() {
    // first, make sure the customer has selected ALL product options. Since all child options, if any exist, have the same
    // amount of options, we just need to check the first element in the childrenList.
    // also adding in an OR conditional, so that products with no options can still be checked using this function.

    if ((optionsSelectedArray.length == eval("options_" + childrenList[0] + ".length")) || (childrenList.length == 1)) { 
    // they've filled out all the options. now, lets check to see if there is a child option who matches
    // the choices they selected.
        var x = 0;
        var bMatch = -1;
        while ((x < childrenList.length) && (bMatch == -1)) { // loop over all child option combos
            if (buildArrayOfSelectedOptions().compareArrays(eval("options_" + childrenList[x])) && (buildArrayOfSelectedOptions().length > 0)) 
                bMatch = x;
            x++;    
        }
        // if we found a match
        if (bMatch != -1) {
            // check the inventory/backorder status for this option combo, and display a message as appropriate.
            if ((eval("inventory_" + childrenList[bMatch]) <= 0) && (eval("bAllowBackorder_" + childrenList[bMatch]))) {
                // if the inventory for this option combo is <= 0 and we allow backorder, show a message.
                var sText = "<font color=\"red\">We apologize, but this item in the option combination you've chosen is backordered";
					 if (eval("backorderDate_" + childrenList[bMatch]).length > 0) {				
	                sText += " until " + eval("backorderDate_" + childrenList[bMatch]);
					 }
					 sText += ". You may still place an order to reserve this item and it will be shipped as soon as it's available.";
		// do not show message - requested by client so we're going to comment out.
//                        document.getElementById("backorderMessageDiv").innerHTML = sText;
					 document.getElementById("sbutton").disabled = false;
					 if(document.getElementById("sbutton2"))
					   document.getElementById("sbutton2").disabled = false;
            }
            else if ((eval("inventory_" + childrenList[bMatch]) <= 0) && (!eval("bAllowBackorder_" + childrenList[bMatch]))) {
                // if the inventory is <= 0 and we do not allow backorder, show a message and disable add to cart.
                var sText = "<font color=\"red\">We apologize, but this item option combination is backordered";
                if (eval("backorderDate_" + childrenList[bMatch]).length > 0) {
                    sText += " and will not be available until ";
                    sText += eval("backorderDate_" + childrenList[bMatch]);
                    sText += ". Please check back after this date to place an order, or choose a different product option.</font>";
                }
                else
                    sText += ". Please check back again later.</font>";
                document.getElementById("backorderMessageDiv").innerHTML = sText;
                document.getElementById("sbutton").disabled = true;
                if(document.getElementById("sbutton2"))
                  document.getElementById("sbutton2").disabled = true;
            }
            else {
                // clear any div message and reset submit button
                document.getElementById("backorderMessageDiv").innerHTML = "";
                document.getElementById("sbutton").disabled = false;
                if(document.getElementById("sbutton2"))
                  document.getElementById("sbutton2").disabled = false;
            }
        }
        // if we didn't find a match for these options, then we'll use the last element in the childrenOptions array,
        // which is a "psuedo-option" that stands for the product record itself.
        else { 
            // check inventory/backorder status for the product and display message as appropriate.
            if ((eval("inventory_" + childrenList[childrenList.length-1]) <= 0) && (eval("bAllowBackorder_" + childrenList[childrenList.length-1]))) {
               // if the inventory for this option combo is <= 0 and we allow backorder, show a message.
               var sText = "<font color=\"red\">We apologize, but this item is backordered";
			if (eval("backorderDate_" + childrenList[childrenList.length-1]).length > 0) {				
	           sText += " until " + eval("backorderDate_" + childrenList[childrenList.length-1]);
			}
			sText += ". You may still place an order to reserve this item and it will be shipped as soon as it's available.";
               // commented out by client - they no longer want to show this message on product detail page.
               //document.getElementById("backorderMessageDiv").innerHTML = sText;
			document.getElementById("sbutton").disabled = false;
			if(document.getElementById("sbutton2"))
			  document.getElementById("sbutton2").disabled = false;
            }
            else if ((eval("inventory_" + childrenList[childrenList.length-1]) <= 0) && (!eval("bAllowBackorder_" + childrenList[childrenList.length-1]))) {
                // if the inventory is <= 0 and we do not allow backorder, show a message and disable add to cart.
                var sText = "<font color=\"red\">We apologize, but this item is backordered";
                if (eval("backorderDate_" + childrenList[childrenList.length-1]).length > 0) {
                    sText += " and will not be available until ";
                    sText += eval("backorderDate_" + childrenList[childrenList.length-1]);
                    sText += ". Please check back after this date to place an order.</font>";
                }
                else
                    sText += ". Please check back again later.</font>";
                document.getElementById("backorderMessageDiv").innerHTML = sText;
                document.getElementById("sbutton").disabled = true;
                if(document.getElementById("sbutton2"))
                  document.getElementById("sbutton2").disabled = true;
            }
            else {
                // clear any div message and reset submit button
                document.getElementById("backorderMessageDiv").innerHTML = "";
                document.getElementById("sbutton").disabled = false;
                if(document.getElementById("sbutton2"))
                  document.getElementById("sbutton2").disabled = false;
            }
        }
    }

}