function confirmAddProduct() {
  var ans = window.confirm("Are you sure you want to add this product?");
  if(ans) return true;
  return false;
}

	function copyNode(fromNode, toNode) {
	  for (var n=0; n<fromNode.childNodes.length; n++) {
	    var fromChild = fromNode.childNodes[n].cloneNode(false);
	    var toChild = toNode.appendChild(fromChild);
	    copyNode(fromNode.childNodes[n], toChild);
	  }
	}

	function addNewRow() {
	  var tbl = document.getElementById("extendedinfo");
	  var cnti = document.getElementById("exc"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "ext_row_" + cnt;
	  
	  var ndef_lbl = document.createElement("td");
	  var ndef_nfo = document.createElement("td");
	  var ndef_btn = document.createElement("td");
	  ndef_btn.style.textAlign = "center";
	  
	  var ninp_lbl = document.createElement("input");
	  var ninp_nfo = document.createElement("input");
	  var ninp_btn = document.createElement("input");
	  
	  ninp_lbl.style.width = "99%";
	  ninp_lbl.name = "lbl_" + cnt;
	  ninp_nfo.style.width = "99%";
	  ninp_nfo.name = "nfo_" + cnt;
	  ninp_btn.setAttribute("type", "button");
	  ninp_btn.name = "btl_del_" + cnt;
	  ninp_btn.value = "Delete";
	  ninp_btn.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeExInfoRow(p);
	    }
	  }(cnt);
	  
	  ndef_lbl.appendChild(ninp_lbl);
	  ndef_nfo.appendChild(ninp_nfo);
	  ndef_btn.appendChild(ninp_btn);
	  
	  nrow.appendChild(ndef_lbl);
	  nrow.appendChild(ndef_nfo);
	  nrow.appendChild(ndef_btn);
	  
	  tbl.appendChild(nrow);
	}
	
	function addNewPriceRow() {
	  var tbl = document.getElementById("prices");
	  var cnti = document.getElementById("exp"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "price_row_" + cnt;
	  
	  var ndef_lbl = document.createElement("td");
	  var ndef_nfo = document.createElement("td");
	  var ndef_prc = document.createElement("td");
	  var ndef_btn = document.createElement("td");
	  ndef_btn.style.textAlign = "center";
	  
	  var ninp_lbl = document.createElement("input");
	  var ninp_nfo = document.createElement("input");
	  var ninp_prc = document.createElement("select");
	  var ninp_btn = document.createElement("input");
	  


	  
	  
	  
	  ninp_lbl.style.width = "99%";
	  ninp_lbl.name = "price_lbl_" + cnt;
	  ninp_nfo.style.width = "99%";
	  ninp_nfo.name = "price_" + cnt;
	  ninp_prc.style.width = "99%";
	  ninp_prc.name = "price_option" + cnt;
	  ninp_btn.setAttribute("type", "button");
	  ninp_btn.name = "btl_del_" + cnt;
	  ninp_btn.value = "Delete";
	  ninp_btn.onclick = function(x) {
	    var p = x;
	    return function() {
	      removePriceRow(p);
	    }
	  }(cnt);
	 
	 
	  var orig = document.getElementById("price_option_1");
	  
	  copyNode(orig, ninp_prc)

	  ndef_lbl.appendChild(ninp_lbl);
	  ndef_nfo.appendChild(ninp_nfo);
	  ndef_prc.appendChild(ninp_prc);
	  ndef_btn.appendChild(ninp_btn);
	  
	  nrow.appendChild(ndef_lbl);
	  nrow.appendChild(ndef_nfo);
	  nrow.appendChild(ndef_prc);
	  nrow.appendChild(ndef_btn);
	  
	  tbl.appendChild(nrow);
	}
	
	function addNewImageRow() {
	  var tbl = document.getElementById("images");
	  var cnti = document.getElementById("exi"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "file_row_" + cnt;
	  
	  var ndef_lbl = document.createElement("td");
	  var ndef_nfo = document.createElement("td");
	  var ndef_btn = document.createElement("td");
	  ndef_btn.style.textAlign = "center";
	  
	  var ninp_lbl = document.createElement("input");
	  var ninp_nfo = document.createElement("input");
	  var ninp_btn = document.createElement("input");
	  
	  ninp_lbl.style.width = "99%";
	  ninp_lbl.name = "file_lbl_" + cnt;
	  ninp_nfo.style.width = "99%";
	  ninp_nfo.name = "file_" + cnt;
	  ninp_nfo.setAttribute("type", "file");	  
	  ninp_btn.setAttribute("type", "button");
	  ninp_btn.name = "btl_del_" + cnt;
	  ninp_btn.value = "Delete";
	  ninp_btn.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeFileRow(p);
	    }
	  }(cnt);

	  ndef_lbl.appendChild(ninp_lbl);
	  ndef_nfo.appendChild(ninp_nfo);
	  ndef_btn.appendChild(ninp_btn);

	  nrow.appendChild(ndef_lbl);
	  nrow.appendChild(ndef_nfo);
	  nrow.appendChild(ndef_btn);
	  
	  tbl.appendChild(nrow);
	}

		function addNewAdditionalImageRow() {
	  var tbl = document.getElementById("additional_images");
	  var cnti = document.getElementById("exi2"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "additional_file_row_" + cnt;
	  
	  var ndef_lbl = document.createElement("td");
	  var ndef_nfo = document.createElement("td");
	  var ndef_btn = document.createElement("td");
	  ndef_btn.style.textAlign = "center";
	  
	  var ninp_lbl = document.createElement("input");
	  var ninp_nfo = document.createElement("input");
	  var ninp_btn = document.createElement("input");
	  
	  ninp_lbl.style.width = "99%";
	  ninp_lbl.name = "additional_file_lbl_" + cnt;
	  ninp_nfo.style.width = "99%";
	  ninp_nfo.name = "additional_file_" + cnt;
	  ninp_nfo.setAttribute("type", "file");	  
	  ninp_btn.setAttribute("type", "button");
	  ninp_btn.name = "additional_btl_del_" + cnt;
	  ninp_btn.value = "Delete";
	  ninp_btn.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeAdditionalFileRow(p);
	    }
	  }(cnt);

	  ndef_lbl.appendChild(ninp_lbl);
	  ndef_nfo.appendChild(ninp_nfo);
	  ndef_btn.appendChild(ninp_btn);

	  nrow.appendChild(ndef_lbl);
	  nrow.appendChild(ndef_nfo);
	  nrow.appendChild(ndef_btn);
	  
	  tbl.appendChild(nrow);
	}
	
	
			function addNewAdditionalDocumentsRow() {
	  var tbl = document.getElementById("additional_documents");
	  var cnti = document.getElementById("exi3"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "additional_documents_row_" + cnt;
	  
	  var ndef_lbl = document.createElement("td");
	  var ndef_nfo = document.createElement("td");
	  var ndef_btn = document.createElement("td");
	  ndef_btn.style.textAlign = "center";
	  
	  var ninp_lbl = document.createElement("input");
	  var ninp_nfo = document.createElement("input");
	  var ninp_btn = document.createElement("input");
	  
	  ninp_lbl.style.width = "99%";
	  ninp_lbl.name = "additional_documents_lbl_" + cnt;
	  ninp_nfo.style.width = "99%";
	  ninp_nfo.name = "additional_documents_" + cnt;
	  ninp_nfo.setAttribute("type", "file");	  
	  ninp_btn.setAttribute("type", "button");
	  ninp_btn.name = "additional_doc_del_" + cnt;
	  ninp_btn.value = "Delete";
	  ninp_btn.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeAdditionalDocumentsRow(p);
	    }
	  }(cnt);

	  ndef_lbl.appendChild(ninp_lbl);
	  ndef_nfo.appendChild(ninp_nfo);
	  ndef_btn.appendChild(ninp_btn);

	  nrow.appendChild(ndef_lbl);
	  nrow.appendChild(ndef_nfo);
	  nrow.appendChild(ndef_btn);
	  
	  tbl.appendChild(nrow);
	}
	
	
	
	function addCatRow() {
	  var tbl = document.getElementById("cattbl");
	  var cnti = document.getElementById("catc");
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "cat_row_" + cnt;
	  
	  var ndef = document.createElement("td");
	  var ndefbtn = document.createElement("td");
	  ndefbtn.style.textAlign = "center";
	  
	  var catdd = document.createElement("select");
	  var catdel = document.createElement("input");
	  catdd.style.width = "99%";
	  catdd.name = "cat_" + cnt;
	  catdel.setAttribute("type", "button");
	  catdel.name = "btl_del_" + cnt;
	  catdel.value = "Delete";
	  catdel.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeCatRow(p);
	    }
	  }(cnt);

	  var orig = document.getElementById("cat_1");
	  
	  copyNode(orig, catdd)
	  
	  ndef.appendChild(catdd);
	  ndefbtn.appendChild(catdel);
	  
	  nrow.appendChild(ndef);
	  nrow.appendChild(ndefbtn);
	  
	  tbl.appendChild(nrow);
	}
	
	function addNewRelatedRow() {
	  var tbl = document.getElementById("related");
	  var cnti = document.getElementById("exr");
	  var cnt = parseInt(cnti.value); //get the value
	  
	  cnt++; //+= 1
	  
	  cnti.value = cnt; //set the new value
	  
	  //create 2 new inputs
	  var nrow = document.createElement("tr");
	  nrow.id = "related_row_" + cnt;
	  
	  var ndef = document.createElement("td");
	  var ndefbtn = document.createElement("td");
	  ndefbtn.style.textAlign = "center";
	  
	  var catdd = document.createElement("select");
	  var catdel = document.createElement("input");
	  catdd.style.width = "99%";
	  catdd.name = "related_row_" + cnt;
	  catdel.setAttribute("type", "button");
	  catdel.name = "btl_del_" + cnt;
	  catdel.value = "Delete";
	  catdel.onclick = function(x) {
	    var p = x;
	    return function() {
	      removeRelatedRow(p);
	    }
	  }(cnt);

	  var orig = document.getElementById("related_1");
	  
	  copyNode(orig, catdd)
	  
	  ndef.appendChild(catdd);
	  ndefbtn.appendChild(catdel);
	  
	  nrow.appendChild(ndef);
	  nrow.appendChild(ndefbtn);
	  
	  tbl.appendChild(nrow);
	}

	
	function removePriceRow(rowid) {
	  //alert(rowid);
	  var tbl = document.getElementById("prices");
	  var cnti = document.getElementById("exp"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("price_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;
	  
	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(trs[x].childNodes[0].childNodes[0].name);
	    trs[x].id = "price_row_" + x;
	    //alert(trs[x].id);
	    trs[x].childNodes[0].childNodes[0].name = "price_lbl_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "price_" + x;
	    trs[x].childNodes[2].childNodes[0].name = "btl_del_" + x;
      //alert(x);
	    trs[x].childNodes[2].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removePriceRow(c);
	      }
	    }(x);
	  }
	}

  function removeExInfoRow(rowid) {
    //extendedinfo
    //exc
	  var tbl = document.getElementById("extendedinfo");
	  var cnti = document.getElementById("exc"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value

	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("ext_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }

	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(trs[x].childNodes[0].childNodes[0].name);
	    trs[x].id = "ext_row_" + x;
	    //alert(trs[x].id);
	    trs[x].childNodes[0].childNodes[0].name = "lbl_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "nfo_" + x;
	    trs[x].childNodes[2].childNodes[0].name = "btl_del_" + x;
      //alert(x);
	    trs[x].childNodes[2].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeExInfoRow(c);
	      }
	    }(x);
	  }
  }
  
  function removeCatRow(rowid) {
	  var tbl = document.getElementById("cattbl");
	  var cnti = document.getElementById("catc"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  if(cnt == 1) {
	    alert("Sorry, there must be at least 1 category");
	    return; //MUST have at least 1 category
	  }
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("cat_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    trs[x].id = "cat_row_" + x;
	    trs[x].childNodes[0].childNodes[0].name = "cat_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "bnt_del_" + x;
	    trs[x].childNodes[1].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeCatRow(c);
	      }
	    }(x);//coolest feature ever / worst hack ever
	  }

  }
  
  function removeRelatedRow(rowid) {
	  var tbl = document.getElementById("related");
	  var cnti = document.getElementById("exr"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  if(cnt == 1) {
	    return; //MUST have at least 1 category
	  }
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("related_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    trs[x].id = "related_row_" + x;
	    trs[x].childNodes[0].childNodes[0].name = "related_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "bnt_del_" + x;
	    trs[x].childNodes[1].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeRelatedRow(c);
	      }
	    }(x);//coolest feature ever / worst hack ever
	  }

  }

  
  function removeFileRow(rowid) {
	  var tbl = document.getElementById("images");
	  var cnti = document.getElementById("exi"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("file_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(trs[x].childNodes[0].childNodes[0].name);
	    trs[x].id = "file_row_" + x;
	    //alert(trs[x].id);
	    trs[x].childNodes[0].childNodes[0].name = "file_lbl_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "file_" + x;
	    trs[x].childNodes[2].childNodes[0].name = "btl_del_" + x;
      //alert(x);
	    trs[x].childNodes[2].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeFileRow(c);
	      }
	    }(x);
	  }

  }
  
  

  function removeAdditionalFileRow(rowid) {
	  var tbl = document.getElementById("additional_images");
	  var cnti = document.getElementById("exi2"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("additional_file_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(trs[x].childNodes[0].childNodes[0].name);
	    trs[x].id = "additional_file_row_" + x;
	    //alert(trs[x].id);
	    trs[x].childNodes[0].childNodes[0].name = "additional_file_lbl_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "additional_file_" + x;
	    trs[x].childNodes[2].childNodes[0].name = "additional_btl_del_" + x;
      //alert(x);
	    trs[x].childNodes[2].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeAdditionalFileRow(c);
	      }
	    }(x);
	  }

  }
  
  
  function removeAdditionalDocumentsRow(rowid) {
	  var tbl = document.getElementById("additional_documents");
	  var cnti = document.getElementById("exi3"); //get the hidden input
	  var cnt = parseInt(cnti.value); //get the value
	  
	  var trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(rowid);
	    if(trs[x].id == ("additional_documents_row_" + rowid)) {
	      tbl.removeChild(trs[x]);
	      break;
	    }
	  }
	  
	  cnt--; //-= 1
	  
	  cnti.value = cnt;

	  trs = tbl.getElementsByTagName("tr");
	  for(var x=1; x<trs.length; x++) {
	    //alert(trs[x].childNodes[0].childNodes[0].name);
	    trs[x].id = "additional_documents_row_" + x;
	    //alert(trs[x].id);
	    trs[x].childNodes[0].childNodes[0].name = "additional_documents_lbl_" + x;
	    trs[x].childNodes[1].childNodes[0].name = "additional_documents_" + x;
	    trs[x].childNodes[2].childNodes[0].name = "additional_doc_del_" + x;
      //alert(x);
	    trs[x].childNodes[2].childNodes[0].onclick = function(p) {
	      var c = p;
	      return function() {
	        removeAdditionalDocumentsRow(c);
	      }
	    }(x);
	  }

  }

function confirmUpdateProduct() {
  var ans = window.confirm("Are you sure you want to update this product?");
  if(ans) return true;
  return false;
}



function areYouSure(idx, manufac, model) {
  var ans = window.confirm("Are you sure you want to delete this product:\r\nID: " + idx + "\r\nManufacturer: " + manufac + "\r\nModel: " + model);
  if(ans) return true;
  return false;
}

function confirmDelCat() {
  var ans = window.confirm("Are you sure you want to delete this category?");
  if(ans) return true;
  return false;
}


function silentErrorHandler() {return true;}
window.onerror=silentErrorHandler;



function getOrder() {
  var orderList = '';
  orderedNodes = document.getElementById("order_list").getElementsByTagName("li");
  for (var i=0;i < orderedNodes.length;i++) {
    orderList += orderedNodes[i].getAttribute('recordid') + ', ';
    }
  alert(orderList);
  }


	
	function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
		
	}
	return ajaxRequest;
}
function showData() {
	//alert("i m here");
	document.getElementById("textplace").innerHTML="<table style='width: 100%; height: 100%; background-image: url(template/images/loading.gif); background-position: center center; background-repeat: no-repeat;'><tr><td>&nbsp;</td></tr></table>";
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request"); // Alert Them!
		return; // Returns.
	} // End If.
	
	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
		document.getElementById("textplace").innerHTML = htmlRequest.responseText;
		}
	}
	htmlRequest.open("GET", "outputinfo.php?accno="+document.subform.accno.value, true);
	htmlRequest.send(null);
}

	


var exts = "jpg|gif|png|bmp|mp3|mpg|mpeg|avi|rar|zip|7z|gz|txt|pdf";
//var exts = ".*"; //Use this to accept all Extensions

var UID,NF=0,cx=0;
function openStatusWindow()
{ 
 if(document.F1.popup.checked == true)
 {
   win1 = window.open('cgi-bin/upload_status.cgi?upload_id='+UID,'win1','width=320,height=240,resizable=1');
   win1.window.focus();
 }
}

function generateSID()
{
 UID = Math.round(10000*Math.random())+'0'+Math.round(10000*Math.random());
 var f1=document.F1;
 f1.action = f1.action.split('?')[0]+'?upload_id='+UID;
}

function StartUpload()
{
    NF=0;
    for (var i=0;i<document.F1.length;i++)
    {
     current = document.F1.elements[i];
     if(current.type=='file' && current.value!='')
      {
         if(!checkExt(current.value))return false;
         NF++;
      }
    }
    if(NF==0){alert('Select at least one file to upload');return false;};
    generateSID();
    openStatusWindow();
}

function checkExt(value)
{
    if(value=="")return true;
    var re = new RegExp("^.+\.("+exts+")$","i");
    if(!re.test(value))
    {
        alert("Extension not allowed for file: \"" + value + "\"\nOnly these extensions are allowed: "+exts.replace(/\|/g,',')+" \n\n");
        return false;
    }
    return true;
}


