// check inventory 

function dispAlert(msg){
 document.getElementById('Alert_div').innerHTML = "<h3>" + msg + "</h3>";
 TB_show("Alert","#TB_inline?height=120&width=300&inlineId=Alert_div")
}
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function format_number(pnumber,decimals) 
{
  var strNumber = new String(pnumber);
  var arrParts = strNumber.split('.');
  var intWholePart = parseInt(arrParts[0],10);
  var strResult = '';
  if (isNaN(intWholePart))
    intWholePart = '0';
  if(arrParts.length > 1)
  {
    var decDecimalPart = new String(arrParts[1]);
    var i = 0;
    var intZeroCount = 0;
     while ( i < String(arrParts[1]).length )
     {
       if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       {
         intZeroCount += 1;
         i += 1;
       }
       else
         break;
    }
    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
    Math.round(decDecimalPart); 
    decDecimalPart = parseInt(decDecimalPart)/10; 
    decDecimalPart = Math.round(decDecimalPart); 

    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
    { 
      intWholePart+=1; 
      decDecimalPart="0"; 
    } 
    var stringOfZeros = new String('');
    i=0;
    if( decDecimalPart > 0 )
    {
      while( i < intZeroCount)
      {
        stringOfZeros += '0';
        i += 1;
      }
    }
    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
    var dot = decDecimalPart.indexOf('.');
    if(dot == -1)
    {
      decDecimalPart += '.'; 
      dot = decDecimalPart.indexOf('.'); 
    } 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  else
  {
    var dot; 
    var decDecimalPart = new String(intWholePart); 

    decDecimalPart += '.'; 
    dot = decDecimalPart.indexOf('.'); 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  return strResult;
}
function GetSelectedPrice(){
        var oldprice = document.getElementById('Product_Price').value;
		var currprice = oldprice.replace("$","");
		currprice = trim(currprice);
		numcurrprice = parseFloat(currprice);
		var currQty = document.getElementById('mainqty').value;
		numcurrQty = parseFloat(currQty);
		numcurrprice = numcurrprice * numcurrQty;
	var checkboxGroup = $("input:checkbox", document.productform);
	for (var i = 0; i < checkboxGroup.length; i++) {
		var cbObj = checkboxGroup[i];
		if (cbObj.checked){
			// find the price
			numprice = ""
			for (var j = 0; j < itemProduct_id1.length; j++){
			 if (cbObj.value == itemProduct_id1[j]){
                 addPrice = itemPrice1[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id2.length; j++){
			 if (cbObj.value == itemProduct_id2[j]){
                 addPrice = itemPrice2[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id3.length; j++){
			 if (cbObj.value == itemProduct_id3[j]){
                 addPrice = itemPrice3[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id4.length; j++){
			 if (cbObj.value == itemProduct_id4[j]){
                 addPrice = itemPrice4[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id5.length; j++){
			 if (cbObj.value == itemProduct_id5[j]){
                 addPrice = itemPrice5[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id6.length; j++){
			 if (cbObj.value == itemProduct_id6[j]){
                 addPrice = itemPrice6[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);
			     }
			}
			for (var j = 0; j < itemProduct_id7.length; j++){
			 if (cbObj.value == itemProduct_id7[j]){
                 addPrice = itemPrice7[j];
			     strprice = addPrice.replace("$","");
				 strprice = trim(strprice);
				 numprice = parseFloat(strprice);

			     }
			}
			strQtyobj = 'cqty_' + cbObj.value;
			addQty = document.getElementById(strQtyobj).value;
			numcurrprice = numcurrprice + (numprice * addQty);
		}
	}
	document.getElementById('currentTotalSelected').innerHTML = "$" + format_number(numcurrprice,2);
	document.getElementById('currentTotalSelected2').innerHTML = "$" + format_number(numcurrprice,2);	
	//document.getElementById('currentTotalSelected2').innerHTML = "$" + format_number(numcurrprice,2);

}
function checkoptions(cell){
	Product_id = cell.value;
	opt1 = 'option1_' + Product_id;
	opt2 = 'option2_' + Product_id;
	ctitleobj = document.getElementById('ctitle_' + Product_id);
 	prodtitle = ctitleobj.innerHTML;
	opt_ok = true;
	if(document.getElementById(opt1)){
	 if(document.getElementById(opt1).selectedIndex <= 0){
        dispAlert('You must select a color option to add this item to your basket for ' + prodtitle);
	 	opt_ok = false;
	 }
	}
	if (opt_ok){
	if(document.getElementById(opt2)){
	 if(document.getElementById(opt2).selectedIndex <= 0){
	 	dispAlert('You must select a size option to add this item to your basket for ' + prodtitle);
	 	opt_ok = false;
	 }
	}
	}
	if(!opt_ok){
	if (cell.type == 'hidden'){
		return false;
	}
	if (cell.type != 'hidden'){
	    cell.checked = false;
	}
	}
	if(opt_ok) {
	if (cell.type == 'hidden'){
	    return true;
	}
	}
	GetSelectedPrice();
}
function checkthisoption(cell){
	Product_id = cell.name.split("_");
	ctitleobj = document.getElementById('ctitle_' + Product_id[1]);
 	prodtitle = ctitleobj.innerHTML;
	if(cell.value == "-1"){
	dispAlert('You must select an option to add this item to your basket for '+ prodtitle);
	checkboxID = 'cProduct_id_' + Product_id[1];
	document.getElementById(checkboxID).checked = false;
	GetSelectedPrice();
	}
	// check inventory
	if(cell.value != "-1"){
		Pid = cell.name.split("_");
		Product_id = Pid[1];
		opt1 = 'option1_' + Product_id;
		opt2 = 'option2_' + Product_id;
		opt1Val = 0;
		opt2Val = 0;
		InvAmt = 0;
		optnotready = false;
		if(document.getElementById(opt1)){
	 		if(document.getElementById(opt1).selectedIndex >= 1){	
				opt1Val = document.getElementById(opt1).options[document.getElementById(opt1).selectedIndex].value;
			}else {
			optnotready = true;
			}
		}
		if(document.getElementById(opt2)){
	 		if(document.getElementById(opt2).selectedIndex >= 1){	
		opt2Val = document.getElementById(opt2).options[document.getElementById(opt2).selectedIndex].value;
		}else {
			optnotready = true;
			}
		if (optnotready == false){
		if (inv){
    		numrow = inv.getRowCount( );
			// check for no options
    	 	for (row = 0; row < numrow ; ++row)
		 	{
				if (Product_id == inv.getField(row,"product_id") && inv.getField(row,"option_id1") == opt1Val && inv.getField(row,"option_id2") == opt2Val){			
					InvAmt = inv.getField(row,"qty");
 				}
				if (Product_id == inv.getField(row,"product_id") && inv.getField(row,"option_id2") == opt1Val && inv.getField(row,"option_id1") == opt2Val){			
					InvAmt = inv.getField(row,"qty");
 				}

 			}
 	   	}
 	   if (InvAmt == 0){
 	      ctitleobj = document.getElementById('ctitle_' + Product_id);
 	      prodtitle = ctitleobj.innerHTML;
		if (cell.type == 'hidden'){
			dispAlert('<img src="/store/images/outofstock.jpg" alt="out of stock" /><h3>Sorry, that item size/color combination is currently out of stock for ' + prodtitle + '</h3>');		
			return false;
		}
		if (cell.type != 'hidden'){
 	   		dispAlert('<img src="/store/images/outofstock.jpg" alt="out of stock" /><h3>Sorry, that item size/color combination is currently out of stock for ' + prodtitle + '</h3>');		
 	   		document.getElementById(opt2).selectedIndex = 0;
 	   		document.getElementById(opt1).selectedIndex = 0;
		}
	   }
 	   } 
 	  }
  	}
  }	
function emailvalidate(formobj){
	notifyval = $('input:radio', formobj);
    isitchecked = 'false';
	for (var i = 0; i < notifyval.length; i++) {
		notifyboxObj = notifyval[i];
		if (notifyboxObj.checked){
    		isitchecked = 'true';
    	}
    }
    if (isitchecked == 'false'){
    	dispAlert('You must select an out of stock item so that we can notify you when that item is avaliable') 
		return (false);
    }  	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formobj.notifyemail.value)){
		return (true);
	}
	dispAlert('Invalid E-mail Address! Please re-enter.')
		return (false);	
}

function loadItemHandler1(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList1.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList1[i - 1],itemTitle1[i - 1],itemPrice1[i - 1],itemProduct_id1[i - 1],opt,itemLrgImg1[i - 1], 1));						
         }
    }

	$(".standsClick1").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		

	// Trigger loaded
    carousel.loaded();
};
function loadItemHandler2(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList2.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList2[i - 1],itemTitle2[i - 1],itemPrice2[i - 1],itemProduct_id2[i - 1],opt,itemLrgImg2[i - 1], 2));	
        }
    }
    // Trigger loaded
	
	$(".standsClick2").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});			
	
    carousel.loaded();
};
function loadItemHandler3(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList3.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList3[i - 1],itemTitle3[i - 1],itemPrice3[i - 1],itemProduct_id3[i - 1],opt,itemLrgImg3[i - 1], 3));	
        }
    }
	
	$(".standsClick3").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		
	
    // Trigger loaded
    carousel.loaded();
};
function loadItemHandler4(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList4.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList4[i - 1],itemTitle4[i - 1],itemPrice4[i - 1],itemProduct_id4[i - 1],opt,itemLrgImg4[i - 1], 4));	
        }
    }
	
	$(".standsClick4").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		
	
    // Trigger loaded
    carousel.loaded();
};
function loadItemHandler5(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList5.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList5[i - 1],itemTitle5[i - 1],itemPrice5[i - 1],itemProduct_id5[i - 1],opt,itemLrgImg5[i - 1], 5));	
        }
    }
	
	$(".standsClick5").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		
	
    // Trigger loaded
    carousel.loaded();
};
function loadItemHandler6(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList6.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList6[i - 1],itemTitle6[i - 1],itemPrice6[i - 1],itemProduct_id6[i - 1],opt,itemLrgImg6[i - 1], 6));	
        }
    }
	
	$(".standsClick6").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		
	
    // Trigger loaded
    carousel.loaded();
};
function loadItemHandler7(carousel, start, last, available)
{
    if (!available) {
        for (var i = start; i <= last; i++) {
            if (i > itemList7.length) {
                break;
            }

            carousel.add(i, getItemHTML(itemList7[i - 1],itemTitle7[i - 1],itemPrice7[i - 1],itemProduct_id7[i - 1],opt,itemLrgImg7[i - 1], 7));	
        }
    }
	
	$(".standsClick7").click(function() {
		var strandsUrl = "/view/strandsClick.cfm?itemId=" + this.id;
		jQuery.get(strandsUrl);		
	});		
	
    // Trigger loaded
    carousel.loaded();
};




function getItemHTML(url,Title,Price,Product_id,optdata,lrgimg,carousel)
{
	ret = '';
	ret += '<div class="Product_Cell_Carousel">';
  	ret += '<div class="cimage">';
  	strTitle = Title;
  	ret += '<a href="' + lrgimg + '" title="'+ strTitle +'" onclick="this.blur();TB_show(this.title, this.href);return false;">';
    ret += '<img src="' + url + '" class="cimage" border="0" title="Click to view a large image of: ' + strTitle + '" alt="' + strTitle  + '" />';
	ret += '</a>';
  	ret += '</div>';
	ret += '<div class="ctitle" id="ctitle_' + Product_id + '">';
	ret += '<a href="/p' + Product_id + '/product.html" class="standsClick' + carousel + '" id="' + Product_id + '">';
    ret += Title;
	ret += '</a>';
	ret += '</div>';
    ret += '<div class="cprices">';
	ret += Price;
    ret += '</div>';
    numrow = optdata.getRowCount( );
    o = ""; 
    for (row = 0; row < numrow ; ++row)
	{
	if (Product_id == optdata.getField(row,"product_id")){
		if (1 == optdata.getField(row,"optLevel")){
  		o += '<option value=' + optdata.getField(row,"opt_id") + '>' + optdata.getField(row,"optTitle") + '</option>';
		}
	}
	}
 	if (o != ""){
    ret +=   '<div class="cOption1">';
	ret +=   '<select id="option1_' + Product_id + '" name="coption1_' + Product_id +'" onchange="checkthisoption(this);" style="font-size:11px;">';
	ret +=   '<option value="-1">Select Color</option>';
	ret +=  o ;
	ret +=   '</select>';
	ret +=  '</div>';
	}
    o = ""; 
    for (row = 0; row < numrow ; ++row)
	{
	if (Product_id == optdata.getField(row,"product_id")){
		if (2 == optdata.getField(row,"optLevel")){
  		o += '<option value=' + optdata.getField(row,"opt_id") + '>' + optdata.getField(row,"optTitle") + '</option>';
		}
	}
	}
 	if (o != ""){
    ret +=   '<div class="cOption2">';
	ret +=   '<select id="option2_' + Product_id + '" name="coption2_' + Product_id +'" onchange="checkthisoption(this);" style="font-size:11px;">';
	ret +=   '<option value="-1">Select Size</option>';
	ret +=  o ;
	ret +=   '</select>';
	ret +=  '</div>';
	}
    ret +=	'<div class="cQty">';
    ret +=       '<label>Quantity:';
	ret +=   '<input type=text name="cqty_' + Product_id + '" id="cqty_' + Product_id + '" size=3 maxlength=6 value="1" onblur="GetSelectedPrice()" style="font-size:11px;" /></label>';
	ret +=  '</div>';
	ret +=  '<div class="cselect">';
	ret +=    '<label>';
	ret +=    '<input type="checkbox" name="cProduct_ID" Value ="' + Product_id + '" id="cProduct_id_' + Product_id  +'" onclick="checkoptions(this);">add this item</label>';
	ret +=  '</div>';
  	ret += '</div>';
	
 	return ret;
};

// Next-Button handling...
var nextOver = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/next-over.gif");
};

var nextOut = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/next.gif");
};

var nextDown = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/next-down.gif");
};

function nextButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "/Store/images/jcarousel/horizontal-ie7/next.gif")
                      .bind("mouseover", nextOver)
                      .bind("mouseout", nextOut)
                      .bind("mousedown", nextDown);
    } else {
        jQuery(button).attr("src", "/Store/images/jcarousel/horizontal-ie7/next-disabled.gif")
                      .unbind("unmouseover", nextOver)
                      .unbind("unmouseout", nextOut)
                      .unbind("unmousedown", nextDown);
    }
}

// Prev-Button handling
var prevOver = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/prev-over.gif");
};

var prevOut = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/prev.gif");
};

var prevDown = function() {
    jQuery(this).attr("src", "/Store/images/jcarousel/horizontal-ie7/prev-down.gif");
};

function prevButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "/Store/images/jcarousel/horizontal-ie7/prev.gif")
                      .bind("mouseover", prevOver)
                      .bind("mouseout", prevOut)
                      .bind("mousedown", prevDown);
    } else {
        jQuery(button).attr("src", "/Store/images/jcarousel/horizontal-ie7/prev-disabled.gif")
                      .unbind("unmouseover", prevOver)
                      .unbind("unmouseout", prevOut)
                      .unbind("unmousedown", prevDown);
    }
}