/*-------------------------------------------------------
Variables
*/

var arrList = new Array();

/*
 END Variables
-------------------------------------------------------*/


function onFocus(field){
  field.focus();
}

function SelectAll(f,selection){
  for (var i = 0; i < f.options.length; i++){
    f.options[i].selected = selection;
  }
}




/*-------------------------------------------------------
Form Validation Section
*/

function validateAdminLogin(f){
  if(!checkText(f.Admin_UserName,0)){
    alert("Please Enter A Valid User Name.");
    f.Admin_UserName.focus();
    f.Admin_UserName.select();
    return false;
  }
  if(!checkText(f.Admin_Pass,0)){
    alert("Please Enter A Valid Password.");
    f.Admin_Pass.focus();
    f.Admin_Pass.select();
    return false;
  }
  return true;
}

function validateMemLogin(f){
  if(!checkEmail(f.Mem_Email)){
    alert("Please Enter A Valid Email Address.");
    f.Mem_Email.focus();
    f.Mem_Email.select();
    return false;
  }
  if(!checkText(f.Mem_Pass,0)){
    alert("Please Enter A Valid Password.");
    f.Mem_Pass.focus();
    f.Mem_Pass.select();
    return false;
  }
}


/*
END Form Validation Section
-------------------------------------------------------*/

/*-------------------------------------------------------
Field Type checks
*/

function checkCurrency(field){
  if (!isNaN(parseFloat(field.value)) && !isEmpty(field.value)){
    if (field.value.indexOf(".") == -1){
      if (field.value > 0){ return true;}
      else{ return false;}
    }else{
      if(field.value.length - field.value.indexOf(".") - 1 <= 2 && field.value > 0){ return true;}
      else{ return false;}
    }
  }
  else{
    return 1;
  }
  return 1;
}

function checkDate(field){
  rex=/\b(1[0-2]|0?[1-9])\/(0?[1-9]|[12][0-9]|3[01])\/\d\d\d\d/
  if (rex.test(field.value) == true){
    var nowDate = new Date();
    var specDate = new Date(field.value);
    return specDate >= nowDate;
  }
  else{ return false;}
}


function checkEmail(Email)
{
  if(!ValidateEmailAddress(Email.value)){return false;}
  else{return true;}
}

function checkFloat(field){
  if (!isNaN(parseFloat(field.value)) && !isEmpty(field.value)){ return true;}
  else{ return false;}
  return false;
}

function checkZeroPosFloat(field){
  if (!isNaN(parseFloat(field.value)) && !isEmpty(field.value) && field.value >= 0){ return true;}
  else{ return false;}
  return false;
}

function checkPosFloat(field){
  if (!isNaN(parseFloat(field.value)) && !isEmpty(field.value) && field.value > 0){ return true;}
  else{ return false;}
  return false;
}

function CheckPhone(Phone)
{
  if(ValidateUSNumber(Phone)==false && Phone.value.length>0){return false;}
  else{return true;}
}

function checkSelected(opt){
  if(opt.value.length < 1){ return false;}
  else{ return true;}
  return false;
}

function checkText(text,max){
  if(max == 0){
    if(text.value.length < 1){return false;}
  }else{
    if(text.value.length < 1 || text.value.length > max){return false;}
  }
  return true;
}

function isEmpty(s) {
  var regexpWhitespace = /^\s+$/;
  return  ((s == null) || (s.length == 0) || regexpWhitespace.test(s));
}

function ValidateUSNumber(phoneValue)
{
  var CleanedString="";
  var index = 0;
  var LimitCheck;
  var InitialString = phoneValue.value;

  //Get the length of the inputted string, to know how many characters to check
  LimitCheck = InitialString.length;
  
  //Walk through the inputted string and collect only number characters, appending them to CleanedString
  while (index != LimitCheck)
  { 
   if (isNaN(parseInt(InitialString.charAt(index)))) { } 
   else { CleanedString = CleanedString + InitialString.charAt(index); } 
   index = index + 1; 
  }
  
  //If CleanedString is exactly 10 digits long, then format it and allow form submission
  if (CleanedString.length == 10)
  {
   phoneValue.value = CleanedString.substring(0,3) + "-" + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10); 
  }
  //If CleanedString is not 10 digits longs, show an alert and cancel form submission
  else
  {
    CleanedString = InitialString; 
    return false
  }
  return true;
} 

function ValidateEmailAddress(incoming)
{
  var emailstring = incoming;
  var ampIndex = emailstring.indexOf("@");
  var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
  // find a dot in the portion of the string after the ampersand only
  var dotIndex = afterAmp.indexOf(".");
  // determine dot position in entire string (not just after amp portion)
  dotIndex = dotIndex + ampIndex + 1;
  // afterAmp will be portion of string from ampersand to dot
  afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
  // afterDot will be portion of string from dot to end of string
  var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
  var beforeAmp = emailstring.substring(0,(ampIndex));
  //old regex did not allow subdomains and dots in names
  //var email_regex = /^[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[\w\d\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~])*\@(((\w+[\w\d\-]*[\w\d]\.)+(\w+[\w\d\-]*[\w\d]))|((\d{1,3}\.){3}\d{1,3}))$/;
  var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
  // index of -1 means "not found"
  if ((emailstring.indexOf("@") != "-1") &&
    (emailstring.length > 5) &&
    (afterAmp.length > 0) &&
    (beforeAmp.length > 1) &&
    (afterDot.length > 1) &&
    (email_regex.test(emailstring)) ){
      return true;
  }else{
    return false;
  }
}
/*
END Field Type checks
-------------------------------------------------------*/

/*-------------------------------------------------------
Confirm Deletions
*/

function ConDelCat(){
  if(!window.confirm("Are your sure you want to delete this category?")){return false;}
  return true;
}

function ConDelProd(){
  if(!window.confirm("Are your sure you want to delete this product?")) {return false;}
  return true;
}

function ConDelOpt()
{
  if(!window.confirm("Are your sure you want to delete this option?")){return false;}
  return true;
}

function ConDelSpec()
{
  if(!window.confirm("Are you sure you wish to delete this special?")){return false;}
  return true;
}
/*
END Confirm Deletions
-------------------------------------------------------*/



function addToList(i,j,name){arrList[i] = new Array(j,name)}
  
function outputList(ar, name, size){
  var strIDs = "<SELECT SIZE=\"" + size + "\" NAME=\"ro_lst" + name + "\">"
  var sel = " SELECTED"
  for (var i=0;i<ar.length;i++){
    strIDs += "<OPTION " + sel + " VALUE=\"" + ar[i][0] + "\">" + ar[i][1]
    sel = ""
  }
  strIDs+="</SELECT>"
  strIDs+="<INPUT NAME=\"" + name + "\" TYPE=hidden>"
  return strIDs
}

function outputButton(bDir,name,val){return "<INPUT TYPE=button VALUE=\"" + val + "\" ONCLICK=\"move(this.form," + bDir + ",'" + name + "')\">"}

function move(f,bDir,sName){
  var el = f.elements["ro_lst" + sName]
  var idx = el.selectedIndex
  if (idx==-1){alert("You must first select the item to reorder.")}
  else{
    var nxidx = idx+( bDir? -1 : 1)
    if (nxidx<0) {nxidx=el.length-1}
    if (nxidx>=el.length) {nxidx=0}
    var oldVal = el[idx].value
    var oldText = el[idx].text
    el[idx].value = el[nxidx].value
    el[idx].text = el[nxidx].text
    el[nxidx].value = oldVal
    el[nxidx].text = oldText
    el.selectedIndex = nxidx
  }
}

function processForm(f){
  for (var i=0;i<f.length;i++){
    var el = f[i]
    if (el.name.substring(0,6)=="ro_lst"){
      var strIDs = ""
      for (var j=0;j<f[i].options.length;j++) {strIDs += f[i].options[j].value + ", "}
      f.elements[f.elements[i].name.substring(6)].value = strIDs.substring(0,strIDs.length-2)
    }
  }
}

function HelpPopup(help){
  window.open(help,"HelpPopup","height=300,width=300,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=200,top=50")
}
