function dollars(amt)
{
   if (amt == 0) { return "$0.00"; }

   with (Math) {
      dlr = floor(amt / 100);
      cent = amt - dlr * 100;
   }
   if (cent < 10) { 
      cent = "0" + cent;
   }
   return "$" + dlr + "." + cent;
}   



function grandtot(ordqty, ordtot, listpfx)
{

   fld = "";
   
   for(sub = 1; sub < top.frames[1].document.forms[0].elements.length; sub++) {
      fld = top.frames[1].document.forms[0].elements[sub].name;
      if (!startsw) {
         if (fld == "qty" + listpfx || fld == "val" + listpfx) { continue; }
      }
      if (fld == "lastfield") { break; }
      if (fld.indexOf("qty") ==0) {
          ordqty = eval(ordqty + "+" + top.frames[1].document.forms[0].elements[sub].value);
      }    
      if (fld.indexOf("val") ==0) {
          ordtot = eval(ordtot + "+" + top.frames[1].document.forms[0].elements[sub].value);
      }   
   }    
   parent.frames[2].document.forms[0].ordqty.value = ordqty;
   parent.frames[2].document.forms[0].ordval.value = dollars(ordtot);
   parent.frames[2].document.forms[0].savqty.value = ordqty;
   parent.frames[2].document.forms[0].savval.value = ordtot;
   top.frames[1].document.forms[0].savqty.value = ordqty;
   top.frames[1].document.forms[0].savval.value = ordtot;
}   

function loadtot()
{
   parent.frames[2].document.forms[0].ordqty.value = top.frames[1].document.forms[0].ordqty.value;
   parent.frames[2].document.forms[0].ordval.value = top.frames[1].document.forms[0].ordval.value;
}
   

function chkload()
{
   while (top.frames[1].document.forms[0].loaded.value != "loaded") {
       timeout = setTimeout('',2000);
       if (top.frames[1].document.forms[0].loaded.value != "loaded") {
          alert('Waiting for frame to load - Press OK to continue');
       }   
   }   
}   
   
function numeric(num)
{
   if (num.length == 0) { return true; }
   for (x = 0; x < num.length; x++) {
      if (num.charAt(x) < "0" || num.charAt(x) > "9") {
         alert(num + " is not a number");
         return false;
      }   
   }
   return true;
}

 
function getbrowser(alrt)
{

   var versok = true;
   var btype = "z";

   browser = navigator.appName;
   bvers =  navigator.appVersion;

   if (browser == "Netscape") {
      btype = "N"; 
      if (eval(bvers.substring(0,1)) < 4) {
         versok = false;
      }
   }
   if (browser == "Microsoft Internet Explorer") {
      btype = "X";
      if (eval(bvers.substring(0,3)) < 4) {
         versok = false;
      }
   }
   
   if (alrt == "Y") {
      if (btype == "z" || !versok) { 
         alert("This page was developed for use with Netscape 4.0+ and IE 4+. Correct results cannot be guaranteed with other browsers");
      }
   }
   return btype;

}


function getprice(picsize)
{
   slpr = 0;

   if (picsize == "a") {
      slpr = 160;   
   }
   if (picsize == "b") {
      slpr = 230;   
   }
   if (picsize == "c") {
      slpr = 350;
   }   
   return slpr;  
}

function clearorder()
{

   for(sub = 1; sub <=40; sub++) {
      fld = top.frames[1].document.forms[0].elements[sub].name;
      if (fld == "lastfield") { break; }
      if (fld.indexOf("qty") ==0 || fld.indexOf("val") ==0) {
          top.frames[1].document.forms[0].elements[sub].value = 0;
      }    
      if (fld.indexOf("ordline") > 0) {
          top.frames[1].document.forms[0].elements[sub].value = "endline:";
      }   
   }    
}

function chkcust(form,statenq) 
{

   var charx;
   var tot = 0;
   var error = true;
   custok = false;
   custvalid = false;
   form.custno.blur();
//alert("chkcust1")
      error = false;
      if (blankfld(form.custno.value)) {
//         setTimeout("document.forms[0].name.select()",1);
//         setTimeout("document.forms[0].name.focus()",1);
         custok = true;
//alert("chkcust2")
         return true;
      }
      while (form.custno.value.length < 7) {
         form.custno.value = "0" + form.custno.value;
      }
      if (form.custno.value != "0000000") {
         charx = form.custno.value.substring(0,1);
         tot += eval(charx * 3);
         charx = form.custno.value.substring(1,2);
         tot += eval(charx * 5);
         charx = form.custno.value.substring(2,3);
         tot += eval(charx * 7);
         charx = form.custno.value.substring(3,4);
         tot += eval(charx * 11);
         charx = form.custno.value.substring(4,5);
         tot += eval(charx * 13);
         charx = form.custno.value.substring(5,6);
         tot += eval(charx * 17);
   
         if (tot == 0) {
            error = true;
        } else {
            tot %= 37;
            tot = eval(37 - tot);
            if (tot > 10) {
               error = true;
     
            } else {
               if (tot == 10) {
                  tot = 0;
               }   
               if (tot != form.custno.value.substring(6,7)) {
                  error = true;
               }
            }
         }
         if (error) {
            alert("Invalid Customer Number - Please re-enter");
            setTimeout("document.forms[0].custno.select()",1);
            setTimeout("document.forms[0].custno.focus()",1);
//alert("chkcust3")
            return false;
         } else {
            /*cook("xno",form.custno.value.substring(0,6));*/
            if (statenq) {
              /* return true; */
            } else {
               custvalid = true;
               custok = true;
//               setTimeout("document.forms[0].name.select()",1);
//               setTimeout("document.forms[0].name.focus()",1);
//alert("chkcust4")
               return true;
            }
         } 
      } else {
         custvalid = false;
         form.custno.value = " ";
         
      }
/*
   ccexp = new createccexp();
alert("b4 getccvars" + form.custno.value)
   ccvars = getccvars(form.custno.value);
   
   if (ccvars == "X") {
      getccexp = true;
      getcvv = true;
      alert("ccexp");
   }
   
   if (ccvars == "V") {
      getcvv = true;
      alert("cvv");
   }
      
*/
//alert("chkcust5")
   return true;
}

function chkcust2(form) 
{

   var charx;
   var tot = 0;
   var error = true;
   custok = false;
   custvalid = false;
   form.custno.blur();

      error = false;
      while (form.custno.value.length < 7) {
         form.custno.value = "0" + form.custno.value;
      }
      if (form.custno.value != "0000000") {
         charx = form.custno.value.substring(0,1);
         tot += eval(charx * 3);
         charx = form.custno.value.substring(1,2);
         tot += eval(charx * 5);
         charx = form.custno.value.substring(2,3);
         tot += eval(charx * 7);
         charx = form.custno.value.substring(3,4);
         tot += eval(charx * 11);
         charx = form.custno.value.substring(4,5);
         tot += eval(charx * 13);
         charx = form.custno.value.substring(5,6);
         tot += eval(charx * 17);
   
         if (tot == 0) {
            error = true;
        } else {
            tot %= 37;
            tot = eval(37 - tot);
            if (tot > 10) {
               error = true;
     
            } else {
               if (tot == 10) {
                  tot = 0;
               }   
               if (tot != form.custno.value.substring(6,7)) {
                  error = true;
               }
            }
         }
         if (error) {
            alert("Invalid Customer Number - Please re-enter");
            setTimeout("document.forms[0].custno.select()",1);
            setTimeout("document.forms[0].custno.focus()",1);
            return false;
         }
      }
   
}

function blankfld(fld)
{

   var spc = "                                                                ";
   if (fld.length == 0) { return true; }
    
   if (fld == spc.substr(0,fld.length)) {
      return true;
   }
   
   return false;
}        

function chkemail(form)
{
   error = true;
   var ct1=0;
   var ct2=0;
   var ct3=0;
   var ct4=0;
   var space = false;
   var lastch; 
   var thisch;
   form.email.blur();

   if (blankfld(form.email.value)) {
      alert("Enter E-mail address");
      setTimeout("document.forms[0].email.focus()",1);
      setTimeout("document.forms[0].email.select()",1);
      return false;
      }   
      error = false;
      while (form.email.value.charAt(form.email.value.length - 1) == " ") {
         form.email.value = form.email.value.substring(0,form.email.value.length - 1);
      }
      while (form.email.value.charAt(0) == " ") {
         form.email.value = form.email.value.substring(1,form.email.value.length);
      }
      if (form.email.value.indexOf(" ") >= 0) {
         error = true;
      } 
      ldot = 0;
      while (form.email.value.indexOf(".",ldot + 1)  >= 0) {
         ldot = form.email.value.indexOf(".",ldot + 1);
      }   
      if (form.email.value.indexOf("@") < 1) {
         error = true;
      }   
      if (form.email.value.indexOf(".") < 1) {
         error = true;
      }   

      if (form.email.value.indexOf("@",form.email.value.indexOf("@") + 1) >= 0) {
         error = true;
      }
      if (ldot < form.email.value.indexOf("@") + 2) {
         error = true;
      }
      if (ldot + 2 > form.email.value.length) {
         error = true;
      }   
      if (!error) {
         for(sub=0; sub <= form.email.value.length - 1; sub++) {
            thisch = form.email.value.substring(sub, sub + 1)
        
            if ((thisch >= "a") && (thisch <= "z")) {
               continue;
            }  
            if ((thisch >= "A") && (thisch <= "Z")) {
               continue;
            }  
            if  ((thisch >= "0") && (thisch <= "9")) {
               continue;
            }  
            validchars = "_-/[].@";
            if (validchars.indexOf(thisch) > -1) {
               continue;
            }   
            error = true;
            break;
         }
      }   
      
      if (form.email.value.indexOf("@afavia-fotos") >= 0) {
         emailok = false;
         alert("Please enter YOUR e-mail address");
         setTimeout("document.forms[0].email.focus()",1);
         setTimeout("document.forms[0].email.select()",1);
         error = true;
         return false;
      } 

      if (error) {
         emailok = false;
         alert("Invalid e-mail address - Please re-enter");
         setTimeout("document.forms[0].email.focus()",1);
         setTimeout("document.forms[0].email.select()",1);
         return false;
      } else {
         error = false;
         emailok = true;
         setTimeout("document.forms[0].custno.select()",1);
         setTimeout("document.forms[0].custno.focus()",1);
      }   
   
   return true;   
}

function getccvars(cust)
{
   if (blankfld(cust)) {
      return "O";
   }
   ccexp = new loadccexp();

   cno = cust.substring(0,6);

   for (x = 0; x < 999; x++) {
      if (ccexp[x] == "END") {
         return "O";
      }
      ccno = ccexp[x].substring(0,6);
      if (ccno > cno) {
         return "O";
      }
      if (ccno == cno) {
//         if (ccexp[x].substring(6,7) == "X") {
//            return "X";
//         }  
//         if (ccexp[x].substring(6,7) == "V") {
//            return "V";
//         }
//         return "O";  
         return ccexp[x].substr(6)
      }
   }
   return "O";
}


function ce(email)
{
   var chksum = 0
   for(sub = 0; sub < email.length; sub++){
      chksum += email.charCodeAt(sub) * (sub + 1);
   }
   return chksum;
}
