// General Javascript functions
var FirstSelection = true;

function DumpArray(Arr) {
	for(var i = 0; i < Arr.length; i++) {
		document.write(Arr[i]);
		if ( i + 1 != Arr.length ) {
			document.write(",");
		}
	}
}

function DumpArrayAsAlert(Arr) {
	var s = "";
	for(i in Arr) {
		s = s + "[" + i + "] = " + Arr[i];
		if ( i + 1 != Arr.length ) {
			s = s + "\n\r";
		}
	}
	alert(s);
}


function ValueInArray(Arr, Value) {
	pos = 0;
	Found = false;
	while ( pos < Arr.length && Found == false ) {
		// alert("Comparing " + Arr[pos] + " : " + Value);
		if ( Arr[pos] == Value ) {
			Found = true;
		}
		pos++;
	}
	
	return Found;
}

function AddItemToAssociativeArray(Arr, Key, Value) {
	if ( typeof(Arr[Key]) != "object" ) {
		Arr[Key] = new Array();
	}
	NumberOfItems = Arr[Key].length;
	Arr[Key][NumberOfItems] = Value;
	
	return Arr;
}

function MakeUnique(Arr) {
	var unduped = new Object;

	for (var i = 0; i < Arr.length; i++) {   
		unduped[Arr[i]] = Arr[i];
	}
	var uniques = new Array;for (var k in unduped) {
   	uniques.push(unduped[k]);
	}

	return uniques;
}

function disableIt(obj, AccessoryOptionName) {
	// obj.disabled = !(obj.disabled);
	// var z = (obj.disabled) ? 'disabled' : 'enabled';
	if ( obj.disabled ) {
		SKUs.UnsetOptionSelected(AccessoryOptionName, obj.value);
	}
	else {
		if ( obj.checked ) {
			SKUs.SetOptionSelected(AccessoryOptionName, obj.value);
		}
		else {
			SKUs.UnsetOptionSelected(AccessoryOptionName, obj.value);
		}
	}
	// alert(obj.name + ' now ' + z);
}

function ValidateAccessorySelection() {
	var IsValid = false;
	var SKUId = SKUs.IsSKUSelected();

	P = new Number(0.00);
	TotalPrice = parseFloat(OurPrice);
	if ( SKUId != "" ) {
		//alert("SKUId = " + SKUId);
		for(Id in SKUId) {
			Price = SKUId[Id];
		}
		
		// Don't display the message the 1st time thru.
		if ( FirstSelection == false ) {
			P = new Number(Price);
			TotalPrice = new Number(parseFloat(Price) + parseFloat(OurPrice));
			if ( Price > 0.00 ) {
				// document.getElementById("status").innerHTML = "The options below cost an additional $" + commify(P.toFixed(2)) + ".<BR>Total price for this configuration is $" + commify(TotalPrice.toFixed(2));
			}
			else {
				// document.getElementById("status").innerHTML = "There is no additional charge for the options below." ;
			}
		}
		else {
			FirstSelection = false;
		}
		
		document.forms['Products'].elements['sku_id'].value = Id;
		IsValid = true;
	}
	else {
		//alert("You have not selected options which comprise a valid accessories SKU.");
		document.getElementById("status").innerHTML = "&nbsp;"
	}

   if(document.getElementById("optionsprice"))
   {
	   document.getElementById("optionsprice").innerHTML = "$" + commify(P.toFixed(2));
	   document.getElementById("totalprice").innerHTML = "$" + commify(TotalPrice.toFixed(2));
   }
   
//	return SKUId.length > 0;
	return IsValid;
}

function IsOKToSubmit() {
	var IsValid = ValidateAccessorySelection();
	if ( IsValid == false ) {
		alert("Please customize your product before pressing the 'Add To Cart' button.");
	}
	return IsValid;
}

function checkItem(obj, AccessoryOptionName) {
	obj.checked = true;
	disableIt(obj, AccessoryOptionName);
}

function commify(Num) {
    var newNum = "";
    var newNum2 = "";
    var count = 0;
    
    //check for decimal number
    if (Num.indexOf('.') != -1){  //number ends with a decimal point
        if (Num.indexOf('.') == Num.length-1){
            Num += "00";
        }
        if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit
            Num += "0";
        }
        
        var a = Num.split("."); 
        Num = a[0];   //the part we will commify
        var end = a[1] //the decimal place we will ignore and add back later
    }
    else {var end = "00";}  
 
    //this loop actually adds the commas   
    for (var k = Num.length-1; k >= 0; k--){
      var oneChar = Num.charAt(k);
      if (count == 3){
        newNum += ",";
        newNum += oneChar;
        count = 1;
        continue;
      }
      else {
        newNum += oneChar;
        count ++;
      }
   }  //but now the string is reversed!
   
  //re-reverse the string
  for (var k = newNum.length-1; k >= 0; k--){
      var oneChar = newNum.charAt(k);
      newNum2 += oneChar;
  }
   
   // add dollar sign and decimal ending from above
   newNum2 = newNum2 + "." + end;
	return newNum2;
}
