// bImgPreload:
//
function bImgPreload() {
  var appVer=parseInt(navigator.appVersion);
  var isNC=(document.layers && (appVer >= 4));
  var isIE=(document.all    && (appVer >= 4));
  if (isNC || isIE) {
    if (document.images) {
      var imgName = bImgPreload.arguments[0];
      var cnt;
      swImg[imgName] = new Array;
      for (cnt = 1; cnt < bImgPreload.arguments.length; cnt++) {
        swImg[imgName][bImgPreload.arguments[cnt]] = new Image();
        swImg[imgName][bImgPreload.arguments[cnt]].src = bImgPreload.arguments[cnt];
      }
    }
  }
}
// bImgFind:
//
function bImgFind(doc, imgName) {
  for (var i=0; i < doc.layers.length; i++) {
    var img = doc.layers[i].document.images[imgName];
    if (!img) img = bImgFind(doc.layers[i], imgName);
    if (img) return img;
  }
  return null;
}
// bImgSwap:
//
function bImgSwap(imgName, imgSrc) {
  var appVer=parseInt(navigator.appVersion);
  var isNC=(document.layers && (appVer >= 4));
  var isIE=(document.all    && (appVer >= 4));
  if (isNC || isIE) {
    if (document.images) {
      var img = document.images[imgName];
      if (!img) img = bImgFind(document, imgName);
      if (img) img.src = imgSrc;
    }
  }
}
function initilaizeButons() {
  bImgPreload('_B_ROLLOVER1', './clipart/buttons/home_active.gif', './clipart/buttons/home_inactive.gif', './clipart/buttons/home_over.gif');
  bImgPreload('_B_ROLLOVER2', './clipart/buttons/galley_active.gif', './clipart/buttons/gallery_inactive.gif', './clipart/buttons/gallery_over.gif');
  bImgPreload('_B_ROLLOVER3', './clipart/buttons/direction_active.gif', './clipart/buttons/direction_inactive.gif', './clipart/buttons/direction_over.gif');
  bImgPreload('_B_ROLLOVER4', './clipart/buttons/contact_active.gif', './clipart/buttons/contact_inactive.gif', './clipart/buttons/contact_over.gif');
  bImgPreload('_B_ROLLOVER5', './clipart/buttons/inventory_active.gif', './clipart/buttons/inventory_inactive.gif', './clipart/buttons/inventory_over.gif');
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openPopup(id,age,width,height){
  var popurl="imageViewer.php";
  popurl += "?id=" + id;
  popurl += "&age=" + age;
  popurl += "&width=" + width;
  popurl += "&height=" + height;
  winpops=window.open(popurl,"","width=500,height=800,scrollbars,resizable,")
}

function blockKeys(event){
  if (event.keyCode < 48 || event.keyCode > 57) 
    return false;
  return true;
}

function formatPhone(textfield) {
  val = textfield.value;
  if(val.length == 3) {
    var newVal = val;
    if(val.indexOf("(") == -1)
      newVal = "(" + newVal;
    textfield.value = newVal;
  }
  val = textfield.value;
  if(val.length == 4) {
    var newVal = val;
    if(val.indexOf(")") == -1)
      newVal = newVal + ")";
    textfield.value = newVal;
  }
  val = textfield.value;
  if(val.length == 8)
    textfield.value = val + "-";
}

// ************************ Inventory Script
function showabout() {
  OpenWindow=window.open("", "_blank", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=465,height=350");
  OpenWindow.document.write("<html>\n");
  OpenWindow.document.write("<head>\n");
  OpenWindow.document.write("  <title>About Inventory System</title>\n");
  OpenWindow.document.write("  <link href='../theme/shaherazad.css' type=text/css rel=stylesheet>\n");
  OpenWindow.document.write("</head>\n");
  OpenWindow.document.write("<body bgcolor='#a572b1'>\n");
  OpenWindow.document.write("  <div align='center'>\n");
  OpenWindow.document.write("    <table border='0' cellpadding='0' cellspacing='0'>\n");
  OpenWindow.document.write("      <tr>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='10' height='1'></td>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='60' height='1'></td>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='300' height='1'></td>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='10' height='1'></td>\n");
  OpenWindow.document.write("      </tr>\n");
  OpenWindow.document.write("      <tr><td></td><td></td>\n");
  OpenWindow.document.write("        <td nowrap><img border='0' src='../images/logo2.gif' width='130' height='100'><font size=4 color=darkblue><b><br>AdvanceView Technology Corp.</b></font></td>\n");
  OpenWindow.document.write("        <td></td><td></td>\n");
  OpenWindow.document.write("      </tr>\n");
  OpenWindow.document.write("      <tr><td></td><td></td><td></td><td></td></tr>\n");
  OpenWindow.document.write("      <tr>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='1' height='70'></td>\n");
  OpenWindow.document.write("        <td></td>\n");
  OpenWindow.document.write("        <td class='ccsFont'>Version 2.0<br>Service Level SL3, May 10, 2004</td>\n");
  OpenWindow.document.write("        <td></td>\n");
  OpenWindow.document.write("      </tr>\n");
  OpenWindow.document.write("      <tr>\n");
  OpenWindow.document.write("        <td><img border='0'  src='../images/pixel.gif' width='1' height='70'></td>\n");
  OpenWindow.document.write("        <td></td>\n");
  OpenWindow.document.write("        <td class='ccsFont' colspan=2><textarea rows='5' cols='43' name='License' readonly>WITHOUT LIMITING THE FOREGOING, COPYING OR REPRODUCTION OF THE SOFTWARE TO ANY OTHER SERVER OR LOCATION FOR FURTHER REPRODUCTION OR REDISTRIBUTION IS EXPRESSLY PROHIBITED, UNLESS SUCH REPRODUCTION OR REDISTRIBUTION IS EXPRESSLY PERMITTED BY THE LICENSE AGREEMENT ACCOMPANYING SUCH SOFTWARE. THE SOFTWARE IS WARRANTED, IF AT ALL, ONLY ACCORDING TO THE TERMS OF THE LICENSE AGREEMENT. EXCEPT AS WARRANTED IN THE LICENSE AGREEMENT, ADVANCEVIEW TECHNOLOGY CORP. HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS WITH REGARD TO THE SOFTWARE, INCLUDING ALL WARRANTIES AND CONDITIONS OF MERCHANTABILITY, WHETHER EXPRESS, IMPLIED OR STATUTORY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. FOR YOUR CONVENIENCE, ADVANCEVIEW TECHNOLOGY CORP. MAY MAKE AVAILABLE AS PART OF THE SERVICES OR IN ITS TOOLS AND UTILITIES FOR USE AND/OR DOWNLOAD. ADVANCEVIEW TECHNOLOGY CORP. DOES NOT MAKE ANY ASSURANCES WITH REGARD TO THE ACCURACY OF THE RESULTS OR OUTPUT THAT DERIVES FROM SUCH USE OF ANY SUCH TOOLS AND UTILITIES. PLEASE RESPECT THE INTELLECTUAL PROPERTY RIGHTS OF OTHERS WHEN USING THE TOOLS AND UTILITIES MADE AVAILABLE ON THE SERVICES PRODUCTS.</textarea></td>\n");
  OpenWindow.document.write("        <td></td>\n");
  OpenWindow.document.write("      </tr>\n");
  OpenWindow.document.write("      <tr>\n");
  OpenWindow.document.write("        <td></td>\n");
  OpenWindow.document.write("        <td><img border='0' src='../images/logo2.gif' width='45' height='35'></td>\n");
  OpenWindow.document.write("        <td class='ccsFont'><font size=2>Copyright <b>&#169;</b> 2001-2006 AdvanceView Technology Corp.</font></td>\n");
  OpenWindow.document.write("        <td><input type='button' name='okbutton' value='OK' style='height: 22px; width:70px' onclick='javascript:window.close()'></td>\n");
  OpenWindow.document.write("      </tr>\n");
  OpenWindow.document.write("    </table>\n");
  OpenWindow.document.write("  </div>\n");
  OpenWindow.document.write("</body>\n");
  OpenWindow.document.write("</html>\n");
}

function initialize(err) {
  document.isChanged=eval(document.form1.changeFlag.value);
  if (!document.isChanged) {return;}

  document.form1.insert.disabled = false;
  document.form1.update.disabled = false;
  document.form1.delete1.disabled = false;
  document.form1.browse.disabled = false;
  document.form1.inventory.disabled = false;
  document.form1.soldlst.disabled = false;
  document.form1.consigned.disabled = false;
  document.form1.speciallst.disabled = false;
  document.form1.all.disabled = false;

//  alert(err);
  if (err == 0) {
    document.form1.changeFlag.value = "false";
    document.isChanged=eval(document.form1.changeFlag.value);
  }
  else {
    document.form1.inventory.disabled = true;
    document.form1.soldlst.disabled = true;
    document.form1.consigned.disabled = true;
    document.form1.speciallst.disabled = true;
    document.form1.all.disabled = true;
    if (err == 1 || err == 2)  //Already exists
      document.form1.insert.disabled = true;
    else
      if (err == 3) {
        document.form1.update.disabled = true;
        document.form1.delete1.disabled = true;
        document.form1.browse.disabled = true;
      }
  }
  index=0;
}

function openindex(path1, winfilename, punixfilename, ppath, stock) {
  if (path1=="") {
    alert("Please enter 'Type of rug!!!'");
    return false;
  }
  if (stock=="") {
    alert("Please enter 'Stock #!!!'");
    return false;
  }
  var url = "http://www.shaherazad.net/inventory/script/upload_files.cgi?";
  url += "path1="+path1;
  url += "&winfilename="+winfilename;
  url += "&punixfilename="+punixfilename;
  url += "&ppath="+ppath;
  url += "&stock="+stock;
  aWin =window.open(url, "_blank", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=700,height=400");
}

function reset_all() {
  var theForm = document.form1;
//  alert(document.isChanged);
//  alert(theForm.changeFlag.value);
  if (!document.isChanged && !eval(theForm.changeFlag.value)) {return true;}

//alert(index);
  for (i=0; i<index; i++) {
//alert(ids[i]);
    document.getElementById(ids[i]).style.color="red";
  }

  var ans = confirm('Are you sure you want to discard the changes?');
  if (ans) {
    theForm.changeFlag.value = 'false';
    theForm.reset();
    theForm.insert.disabled = false;
    theForm.update.disabled = false;
    theForm.delete1.disabled = false;
    theForm.browse.disabled = false;
    theForm.inventory.disabled = false;
    theForm.soldlst.disabled = false;
    theForm.consigned.disabled = false;
    theForm.speciallst.disabled = false;
    theForm.all.disabled = false;
    
    theForm.special.checked=false;
    theForm.salePrice.value = "";
    hideme(theForm.special);
    
    theForm.sold.checked=false;
    theForm.soldPrice.value = "";
    theForm.invoice.value = "";
    hideme(theForm.sold);
    
    theForm.consigned_c.checked=false;
    theForm.consignedTo.value = "";
    theForm.consignedFrom.value = "";
    hideme(theForm.consigned_c);
    
    for (i=0; i<index; i++) {
      document.getElementById(ids[i]).style.color="black";
    }
    index = 0;
    document.isChanged=false;
    theForm.changeFlag.value='false';
    theForm.reset();
  }
  return ans;
}

function clear_all() {
  var theForm = document.form1;

  var ans = 0;
  var warn = 0;
  for (i=0; i<index; i++) {
    document.getElementById(ids[i]).style.color="red";
//alert(ids[i]);
    warn = 1;
  }

  if (warn) {ans = confirm('Are you sure you want to discard the changes?');}
  if ((!warn||ans)&&(!ans||warn)) {
    theForm.stock.value = "";
    theForm.type.value = "";
    theForm.origin.value = "";
    theForm.design.value = "";
    theForm.source.value = "";
    theForm.circa.value = "";
    theForm.width.value = "";
    theForm.height.value = "";
    theForm.askPrice.value = "";
    theForm.buyPrice.value = "";

    theForm.special.checked=false;
    theForm.salePrice.value = "";
    hideme(theForm.special);

    theForm.sold.checked=false;
    theForm.soldPrice.value = "";
    theForm.invoice.value = "";
    hideme(theForm.sold);

    theForm.consigned_c.checked=false;
    theForm.consignedTo.value = "";
    theForm.consignedFrom.value = "";
    hideme(theForm.consigned_c);

    theForm.rugmnth.checked=false;
    theForm.display.checked=false;
    theForm.desc.value = "";
    theForm.note.value = "";
    theForm.winfilename.value = "";
    theForm.path.value = "";
    document.getElementById('imageid').src="../images/template.gif";

    theForm.insert.disabled = false;
    theForm.update.disabled = false;
    theForm.delete1.disabled = false;
    theForm.browse.disabled = false;
    theForm.inventory.disabled = false;
    theForm.soldlst.disabled = false;
    theForm.consigned.disabled = false;
    theForm.speciallst.disabled = false;
    theForm.all.disabled = false;

    for (i=0; i<index; i++) {
      document.getElementById(ids[i]).style.color="black";
    }
    index = 0;
    document.isChanged=false;
    theForm.changeFlag.value='false';
  }
}

function set_param(bottn) {
  local_button = bottn;
}

function disableButtons(object) {
/*
  document.form1.insert.disabled = true;
  document.form1.update.disabled = true;
  document.form1.delete1.disabled = true;
  document.form1.browse.disabled = true;
  document.form1.inventory.disabled = true;
  document.form1.soldlst.disabled = true;
  document.form1.consigned.disabled = true;
  document.form1.speciallst.disabled = true;
  document.form1.all.disabled = true;
*/
  ids[index++] = object.name+'lbl';
//alert(ids[index-1]);
  if (object.name == 'stock') {
    document.form1.insert.disabled = false;
    document.form1.update.disabled = false;
    document.form1.inventory.disabled = true;
    document.form1.soldlst.disabled = true;
    document.form1.consigned.disabled = true;
    document.form1.speciallst.disabled = true;
    document.form1.all.disabled = true;
    document.form1.path.value="";
    document.form1.winfilename.value="";
    document.form1.unixfilename.value="";
    document.form1.image.src="../images/template.gif";
    ids[index++] = 'pathlbl';
    ids[index++] = 'winfilenamelbl';
  }
  else {
    document.form1.delete1.disabled = true;
    document.form1.browse.disabled = true;
    document.form1.inventory.disabled = true;
    document.form1.soldlst.disabled = true;
    document.form1.consigned.disabled = true;
    document.form1.speciallst.disabled = true;
    document.form1.all.disabled = true;
  }
//alert(ids[index-1]);
  document.isChanged=true;
  document.form1.changeFlag.value='true';
}

function removeSpace(object) {
  var st="";
  for (i=0; i<object.value.length; i++) {
    if (object.value.charAt(i)==" " || 
        object.value.charAt(i)=="\\" ||
//       object.value.charAt(i)=="-" ||
        object.value.charAt(i)=="+" ||
        object.value.charAt(i)=="^" ||
//       object.value.charAt(i)=="_" ||
        object.value.charAt(i)=="=" ||
        object.value.charAt(i)=="[" ||
        object.value.charAt(i)=="]" ||
        object.value.charAt(i)=="{" ||
        object.value.charAt(i)=="}" ||
        object.value.charAt(i)=="&" ||
        object.value.charAt(i)=="%" ||
        object.value.charAt(i)=="$" ||
        object.value.charAt(i)=="#" ||
        object.value.charAt(i)=="@" ||
        object.value.charAt(i)=="(" ||
        object.value.charAt(i)==")" ||
        object.value.charAt(i)=="." ||
        object.value.charAt(i)==":" ||
        object.value.charAt(i)==";" ||
        object.value.charAt(i)=="," ||
        object.value.charAt(i)=="*" ||
        object.value.charAt(i)=="?" ||
        object.value.charAt(i)=="\"" ||
        object.value.charAt(i)=="<" ||
        object.value.charAt(i)==">" ||
        object.value.charAt(i)=="|" ||
        object.value.charAt(i)=="!" ||
        object.value.charAt(i)=="`" ||
        object.value.charAt(i)=="'" ||
        object.value.charAt(i)=="~" ||
        object.value.charAt(i)=="/" ) continue;
    st += object.value.charAt(i);
  }
  object.value = st;
}

function finalizeType() {
  if (document.getElementById('typeid').value.length == 0) {
    alert("Please enter 'Type of rug!!!'");
    return false;
  }
  document.getElementById('typeid').value = document.getElementById('typeid').value.toLowerCase();
  document.getElementById('typeid').value = document.getElementById('typeid').value.charAt(0).toUpperCase()+document.getElementById('typeid').value.substring(1);
  return true;
}

function finalizePath() {
  if (document.getElementById('path2id').value.length == 0) return false;
  document.getElementById('path2id').value = document.getElementById('path2id').value.toLowerCase();
  document.getElementById('path2id').value = document.getElementById('path2id').value.charAt(0).toUpperCase()+document.getElementById('path2id').value.substring(1);
  return true;
}

function Form1_Validate(theForm) {
  document.getElementById('typeid').value = document.getElementById('typeid').value.toLowerCase();
  document.getElementById('typeid').value = document.getElementById('typeid').value.charAt(0).toUpperCase()+document.getElementById('typeid').value.substring(1); 
  if (local_button=='delete1') {
    if (theForm.stock.value!="") {
      if (confirm("Are you sure you want to delete record with "+theForm.stock.value+" key?"))
        return true;
    }
    else {
      alert("Please enter 'Stock Number'");
      return false;
    }
    return false;
  }
  if (local_button=='browse' || local_button=='insert' || local_button=='update') {
    if (theForm.stock.value=="") {
      alert("Please enter 'Stock Number'");
      return false;
    }
    return true;
  }
  if (local_button=='about') {
    showabout();
    return false;
  }
  return true;
}

function hideme(object) {
  if (object.name=="special") {
    if (!object.checked) {
      document.getElementById('salePricelbl').style.display = 'none';
      document.getElementById('spcialid2').style.display = 'none';
    }
    else {
      document.getElementById('salePricelbl').style.display = '';
      document.getElementById('spcialid2').style.display = '';
    }
  }
  if (object.name=="sold") {
    if (!object.checked) {
      document.getElementById('soldPricelbl').style.display = 'none';
      document.getElementById('soldid2').style.display = 'none';
      document.getElementById('invoicelbl').style.display = 'none';
      document.getElementById('soldid4').style.display = 'none';
    }
    else {
      document.getElementById('soldPricelbl').style.display = '';
      document.getElementById('soldid2').style.display = '';
      document.getElementById('invoicelbl').style.display = '';
      document.getElementById('soldid4').style.display = '';
    }
  }
  if (object.name=="consigned_c") {
    if (!object.checked) {
      document.getElementById('consignedTolbl').style.display = 'none';
      document.getElementById('consignedid2').style.display = 'none';
      document.getElementById('consignedFromlbl').style.display = 'none';
      document.getElementById('consignedid4').style.display = 'none';
    }
    else {
      document.getElementById('consignedTolbl').style.display = '';
      document.getElementById('consignedid2').style.display = '';
      document.getElementById('consignedFromlbl').style.display = '';
      document.getElementById('consignedid4').style.display = '';
    }
  }
}

function confirm_discard(theForm) {
//alert(index);
//alert(document.isChanged);
//alert(eval(theForm.changeFlag.value));

  if (!document.isChanged && !eval(theForm.changeFlag.value)) {return true;}
  theForm.changeFlag.value = 'true';
  if (local_button != "update" && 
      local_button != "insert" && 
      local_button != "delete1" && 
      local_button != "browse" && 
      local_button != "about") {
    var ans = confirm('Are you sure you want to discard the changes?');
    if (ans) {theForm.changeFlag.value = 'false';}
    return ans;
  }
  return true;
}

function validate(){
  var doc = document.contactForm;
  var error_msg = "Please correct the following before proceeding:\n\n";
  var errors = 0;
  if(doc.name.value.length == 0) {
    error_msg += "\tName\n";
    errors++;  
  }
  if(doc.email.value.length == 0 || doc.email.value.indexOf("@") == -1 || doc.email.value.indexOf(".") == -1) {
    error_msg += "\tEmail Address\n";
    errors++;  
  }
  if(doc.phone.value.length == 0) {
    error_msg += "\tPhone\n";
    errors++;  
  }
  if(errors > 0) {
    alert(error_msg);
    return false;
  }
  return true;
}
