function chkField(strId, intType){
  if(intType == 1 || intType == 5){
	if(document.getElementById(strId).value == ""){
	  return false;
	}
  }else if(intType == 2){
    if(document.getElementById(strId).value.length == 0){
	  return false;
	}
  }else if(intType == 4){
    return document.getElementById(strId).checked;
  }else{
	if(document.getElementById("radio"+strId).value != "1"){
	  return false;	
	}
  }
  return true;
}

function doRadio(intId){
  document.getElementById("radioopt"+intId).value = "1";
  doCalc();
}

function chkProductForm(frm){
  doCalc();
  for(i=0;i<arrReq.length;i++){
    var arrTemp = new Array();
	arrTemp = arrReq[i].split("|");
	if(!(chkField(arrTemp[0],arrTemp[1]))){
	  alert(arrTemp[2]+" is required")
	  document.getElementById(arrTemp[0]).focus();
	  return false;
	  break;
	}
  }
  if(frm.qty.value.length == 0){
    alert("Please enter a Quantity");
	frm.qty.focus();
	return false;
  }
  if(isNaN(frm.qty.value) || frm.qty.value == "0"){
    alert("Quantity must be a number great than 0");
	frm.qty.select();
	frm.qty.focus();
	return false;
  }
  return true;
}

function showLargeAdd(strImage, strCap){
  var mouseX = document.getElementById("icoViewLarge").offsetLeft;
  var popUpHTML = '<table width="420" cellpadding="0" cellspacing="0" border="0" class="popUp">';
    popUpHTML += '<tr>';
	  popUpHTML += '<td align="center"><a href="javascript:hideLarge()">close</a></td>';
	popUpHTML += '</tr>';
	popUpHTML += '<tr>';
	  popUpHTML += '<td align="center"><img src="/images/products/'+strImage+'" border="0" alt="" /></td>';
	popUpHTML += '</tr>';
	popUpHTML += '<tr>';
	  popUpHTML += '<td align="center">'+strCap+'</td>';
	popUpHTML += '</tr>';
  popUpHTML += '</table>';
  if(navigator.appName == 'Microsoft Internet Explorer'){
    for(i=1;i<=numOptions;i++){
	  document.getElementById("opt"+i).style.visibility = "hidden";
	}
  }
  document.getElementById("largeImageDiv").style.left = mouseX+ "px";
  document.getElementById("largeImageDiv").innerHTML = popUpHTML;
  document.getElementById("largeImageDiv").style.visibility = "visible";
}

function showLarge(){
  var mouseX = document.getElementById("icoViewLarge").offsetLeft;
  var arrTemp = largeImage.split("|");
  var popUpHTML = '<table width="420" cellpadding="0" cellspacing="0" border="0" class="popUp">';
    popUpHTML += '<tr>';
	  if(arrMainImages.length == 1){
	    popUpHTML += '<td width="140">&nbsp;</td>';
	  }else{
	    popUpHTML += '<td width="140">&nbsp;<a href="javascript:prevLarge()">&lt;&nbsp;previous</a></td>';
	  } 
	  popUpHTML += '<td width="140" align="center"><a href="javascript:hideLarge()">close</a></td>';
	  if(arrMainImages.length == 1){
	    popUpHTML += '<td width="140">&nbsp;</td>';
	  }else{
	    popUpHTML += '<td width="140" align="right"><a href="javascript:nextLarge()">next&nbsp;&gt;</a>&nbsp;</td>';
	  }
	popUpHTML += '</tr>';
	popUpHTML += '<tr>';
	  popUpHTML += '<td colspan="3" align="center">';
	    popUpHTML += '<img src="/images/products/'+arrTemp[0]+'" border="0" alt="" />';
	  popUpHTML += '</td>';
	popUpHTML += '</tr>';
	popUpHTML += '<tr>';
	  popUpHTML += '<td colspan="3" align="center">'+arrTemp[1]+'</td>';
	popUpHTML += '</tr>';
  popUpHTML += '</table>';
  if(navigator.appName == 'Microsoft Internet Explorer'){
    for(i=1;i<=numOptions;i++){
	  document.getElementById("opt"+i).style.visibility = "hidden";
	}
  }
  document.getElementById("largeImageDiv").style.left = mouseX+ "px";
  document.getElementById("largeImageDiv").innerHTML = popUpHTML;
  document.getElementById("largeImageDiv").style.visibility = "visible";
}

function hideLarge(){
  document.getElementById("largeImageDiv").style.visibility = "hidden";
  if(navigator.appName == 'Microsoft Internet Explorer'){
    for(i=1;i<=numOptions;i++){
	  document.getElementById("opt"+i).style.visibility = "visible";
	}
  }
}

function doOptionSelect(strValue, boolImage){
  if(boolImage){
    for(i=0;i<arrMainImages.length;i++){
      var arrTemp = arrMainImages[i].split("|");
   	  if(arrTemp[1] == strValue){
	    document.getElementById("mainImage").src = "/images/products/lg/"+arrTemp[0];
	    largeImage = arrMainImages[i];
	  }
    }
  }
 doCalc();
}

function doCalc(){
  if(!isNaN(document.getElementById("qty").value) && document.getElementById("qty").value != 0 && document.getElementById("qty").value.length != 0){ 
    var intTotal = document.getElementById("hid_price").value/1;
    for(i=1;i<=document.getElementById("num_opt").value;i++){
	  if(document.getElementById("radioopt"+i)){
		for(j=1;j<=arrRadios[i];j++){
		  if(document.getElementById("opt"+i+"_"+j).checked){
			if(document.getElementById("opt"+i+"_"+j).value.indexOf("+") != -1){
		      var intAdd = document.getElementById("opt"+i+"_"+j).value.substr(document.getElementById("opt"+i+"_"+j).value.indexOf("+")+1);
		      intAdd = intAdd.replace(/\s/g, '');
		      intAdd = intAdd.replace(/\$/g, '');
		      intAdd = intAdd/1;
		      intTotal += intAdd;
	        }else if(document.getElementById("opt"+i+"_"+j).value.indexOf("-") != -1){
	          var intMinus = document.getElementById("opt"+i+"_"+j).value.substr(document.getElementById("opt"+i+"_"+j).value.indexOf("-"))+1;
		      intMinus = intMinus.replace(/\s/, '');
		      intMinus = intMinus.replace(/\$/, '');
		      intMinus = intMinus/1;
		      intTotal -= intMinus;
	        }
		  }
		}
	  }else{
	    if(document.getElementById("opt"+i).type == "select-one"){
		  if(document.getElementById("opt"+i).value.indexOf("+") != -1){
		    var intAdd = document.getElementById("opt"+i).value.substr(document.getElementById("opt"+i).value.indexOf("+")+1);
		    intAdd = intAdd.replace(/\s/g, '');
		    intAdd = intAdd.replace(/\$/g, '');
		    intAdd = intAdd/1;
		    intTotal += intAdd;
	      }else if(document.getElementById("opt"+i).value.indexOf("-") != -1){
	        var intMinus = document.getElementById("opt"+i).value.substr(document.getElementById("opt"+i).value.indexOf("-"))+1;
		    intMinus = intMinus.replace(/\s/, '');
		    intMinus = intMinus.replace(/\$/, '');
		    intMinus = intMinus/1;
		    intTotal -= intMinus;
	      }
	  }
	  
	  }
	  
    }
    var intQty = document.getElementById("qty").value;
	intQty = intQty/1;
	intTotal = Math.round(intTotal*intQty * 100) / 100;
	if(intTotal > intTotal.toFixed(2)){
	  intTotal = intTotal.toFixed(2) + .01;
	}else{
	  intTotal = intTotal.toFixed(2);
	}
	
	document.getElementById("hid_total").value = intTotal;
    document.getElementById("total").innerHTML = "$"+intTotal;
  }
}

function doOption(strImage, strValue, strField){
  if(strField.length == 0 || strField == firstImageOption){
    document.getElementById("mainImage").src = "/images/products/lg/"+strImage;
  }
  if(strField.length == 0){
    document.getElementById(firstImageOption).value = strValue;
  }else{
    document.getElementById(strField).value = strValue;
  }
  largeImage = strImage+"|"+strValue;
}

function nextLarge(){
  for(i=0;i<arrMainImages.length;i++){
    if(arrMainImages[i] == largeImage){
	  if(i == arrMainImages.length - 1){
	    var arrTemp = arrMainImages[0].split("|");
		document.getElementById("mainImage").src = "/images/products/lg/"+arrTemp[0];
		doOption(arrTemp[0], arrTemp[1], '');
		showLarge();
		break;
	  }else{
	    var arrTemp = arrMainImages[i+1].split("|");
		document.getElementById("mainImage").src = "/images/products/lg/"+arrTemp[0];
		doOption(arrTemp[0], arrTemp[1], '');
		showLarge();
		break;
	  }
	}
  }
  
}

function prevLarge(){
  for(i=0;i<arrMainImages.length;i++){
	if(arrMainImages[i] == largeImage){
	  if(i == 0){
	    var arrTemp = arrMainImages[arrMainImages.length-1].split("|");
		document.getElementById("mainImage").src = "/images/products/lg/"+arrTemp[0];
		doOption(arrTemp[0], arrTemp[1], '');
		showLarge();
		break;
	  }else{
	    var arrTemp = arrMainImages[i-1].split("|");
		document.getElementById("mainImage").src = "/images/products/lg/"+arrTemp[0];
		doOption(arrTemp[0], arrTemp[1], '');
		showLarge();
		break;
	  }
	}
  }
}