var xmlHttp
var alertTimerId = 0;
var resetTimerId = 0;

function doLiveSearch() {
   clearTimeout(alertTimerId);
   clearTimeout(resetTimerId);
   alertTimerId = setTimeout("DoShowLiveResult()", 500);
} /* end doLiveSearch */

function DoShowLiveResult() {
   clearTimeout(alertTimerId);
   clearTimeout(resetTimerId);
   var str = document.getElementById("product").value;
   var eBrand = document.getElementById("brand_id");
   var brand = 0;
   if (eBrand) {
      brand = eBrand.options[eBrand.selectedIndex].value;
   }
   if (str.length==0) {
      resetLiveSearchResult();
      return
   }
   xmlHttp=GetXmlHttpObject()

   if (xmlHttp==null) {
       return
   }
   changeObjectDisplay('brlivesearchcontainer', 'block');
   document.getElementById("brlivesearchimg").src = "gfx/busy18.gif";
   var url = "live_product_search.php?v=livesearch&st=" + str + "&brand_id=" + brand.toString() + "&sid=" + Math.random()
   xmlHttp.onreadystatechange=searchStateChanged
   xmlHttp.open("GET",url,true)
   xmlHttp.send(null)
} /* end DoShowLiveResult */

function searchStateChanged() {
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
      document.getElementById("brlivesearchcontainer").style.border="1px solid #A5ACB2";
      document.getElementById("brlivesearch").innerHTML=xmlHttp.responseText;
      document.getElementById("brlivesearchimg").src = "gfx/e.gif";
      resetTimerId = setTimeout("resetLiveSearchResult()", 10000);
   }
} /* end searchStateChanged */

function resetLiveSearchResult() {
   document.getElementById("brlivesearchimg").src = "gfx/e.gif";
   document.getElementById("brlivesearch").innerHTML="";
   document.getElementById("brlivesearchcontainer").style.border="0px";
   changeObjectDisplay('brlivesearchcontainer', 'none');
} /* end resetLiveSearchResult */

function GetXmlHttpObject() {
   var xmlHttp=null;
   try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
   } catch (e) {
      // Internet Explorer
      try {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   return xmlHttp;
} /* end GetXmlHttpObject */

function setField(name, value) {
   msg_field = getObject(name);
   msg_field.innerHTML = value;
} /* end setField */

function isValidEmail(frm, elem) {
   reEmail = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,4}$/;

   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
         return(reEmail.test(ref.value));
      } else {
         alert('isValidEmail: ' + elem + ' not found');
      }
   } else {
      alert('isValidEmail: form not found');
   }
   return true;
} /* end isValidEmail */

function textCounter(field, countfield, maxlimit) {
   if (field.value.length > maxlimit) {
      field.value = field.value.substring(0, maxlimit);
   } else {
      setField(countfield, (maxlimit - field.value.length));
   }
} /* end textCounter */

function isValidInt(frm, elem) {
   reInteger = /(^-?\d\d*$)/;

   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
         return(reInteger.test(ref.value));
      } else {
         alert('isValidEmail: ' + elem + ' not found');
      }
   } else {
      alert('isValidEmail: form not found');
   }
   return true;
} /* end isValidInt */

function isValidNumeric(frm, elem) {
   reNumeric = /(^\d\d*\.\d*$)|(^\d\d*$)|(^\.\d\d*$)/;

   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
          ref.value = ref.value.replace(',', '.');
         return(reNumeric.test(ref.value));
      } else {
         alert('isValidEmail: ' + elem + ' not found');
      }
   } else {
      alert('isValidEmail: form not found');
   }
   return true;
} /* end isValidNumeric */

function trim(s) {
   var sRet = '';
   var sC = '';

   for (var i = 0; i < s.length; i++) {
      sC = s.charAt(i);
      if ((sC != ' ') && (sC != '\t'))
         sRet = sRet + sC
   }
   return(sRet);
} /* end trim */

function checkString(frm, elem) {
   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
         if (trim("" + ref.value) == "") {
       return false;
    }
      } else {
         alert('checkString: ' + elem + ' not found');
      }
   } else {
      alert('checkString: form not found');
   }
   return true;
} /* end checkString */

function checkboxIsChecked(frm, elem) {
   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
         if (ref.length) {
       for (i = 0; i < ref.length; i++) {
          if (ref[i].checked == true) {
             return(true);
          }
       }
    } else {
       return(ref.checked);
    }
      } else {
         alert('checkboxIsChecked: ' + elem + ' not found');
      }
   } else {
      alert('checkboxIsChecked: form not found');
   }
   return(false);
} /* end checkboxIsChecked */

function multipleSelectChecked(frm, elem, first_field) {
   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
          if (ref.length) {
       for (i = 0; i < ref.length; i++) {
          if ((ref[i].selected == true) && (i >= first_field)) {
             return(true);
          }
       }
          } else {
             if (ref.selectedIndex >= first_field) {
               return(true);
             }
          }
      } else {
         alert('multipleSelectChecked: ' + elem + ' not found');
      }
   } else {
      alert('multipleSelectChecked: form not found');
   }
   return(false);
} /* end multipleSelectChecked */

function setCheckboxStatus(frm, elem, value) {
   if (frm) {
      var ref = eval('frm.' + elem);
      if (ref) {
         if (ref.checked != value) {
            ref.checked = value
         }
      } else {
         alert('setCheckboxStatus: ' + elem + ' not found');
      }
   } else {
      alert('setCheckboxStatus: form not found');
   }
} /* end setCheckboxStatus */

function getObject(objectId) {
   if(document.getElementById && document.getElementById(objectId)) {
      return document.getElementById(objectId);
   } else if (document.all && document.all(objectId)) {
      return document.all(objectId);
   } else {
      return false;
   }
} /* end getObject */

function getStyleObject(objectId) {
   if(document.getElementById && document.getElementById(objectId)) {
      return document.getElementById(objectId).style;
   } else if (document.all && document.all(objectId)) {
      return document.all(objectId).style;
   } else if (document.layers && document.layers[objectId]) {
      return document.layers[objectId];
   } else {
      return false;
   }
} /* end getStyleObject */

function changeObjectVisibility(objectId, newVisibility) {
   // get a reference to the cross-browser style object and make sure the object exists
   var styleObject = getStyleObject(objectId);
   if (styleObject) {
      styleObject.visibility = newVisibility;
      return true;
   } else {
      // we couldn't find the object, so we can't change its visibility
      return false;
   }
} /* changeObjectVisibility */

function switchObjectDisplay(objectId) {
   // get a reference to the cross-browser style object and make sure the object exists
   var styleObject = getStyleObject(objectId);
   if (styleObject) {
      styleObject.display = ((styleObject.display == 'none') ? 'block' : 'none');
      return true;
   } else {
      // we couldn't find the object, so we can't change its visibility
      return false;
   }
} /* switchObjectDisplay */

function changeObjectDisplay(objectId, newDisplay) {
   // get a reference to the cross-browser style object and make sure the object exists
   var styleObject = getStyleObject(objectId);
   if (styleObject) {
      styleObject.display = newDisplay;
      return true;
   } else {
      // we couldn't find the object, so we can't change its visibility
      return false;
   }
} /* changeObjectDisplay */

function showAndFocus(div_id, bVisible) {
  var the_div = getStyleObject(div_id);
  if (the_div != false) {
    changeObjectVisibility(div_id, ((bVisible == true) ? "visible" : "hidden"));
  }
} /* end showAndFocus */

function isValidDate(strValue) {
  var objRegExp = /^\d{8}$/;

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = new Array(); //split date into month, day, year
    arrayDate[0] = strValue.substring(2, 4);
    arrayDate[1] = strValue.substring(0, 2);
    arrayDate[2] = strValue.substring(4, 8);

    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31, '03' : 31, '04' : 30, '05' : 31, '06' : 30, '07' : 31,
                        '08' : 31, '09' : 30, '10' : 31, '11' : 30, '12' : 31}
    var intDay = parseInt(arrayDate[1], 10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    //check for February (bugfix 20050322)
    var intMonth = parseInt(arrayDate[0], 10);
    if (intMonth == 2) {
       var intYear = parseInt(arrayDate[2], 10);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
          return true; //Feb. had valid number of days
    }
  }
  return false; //any other values, bad date
} /* end isValidDate */

function isValidTime(strValue) {
  var objRegExp = /^\d{4}$/;

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = new Array(); //split date into hours, minutes
    arrayDate[0] = parseInt(strValue.substring(0, 2), 10);
    arrayDate[1] = parseInt(strValue.substring(2, 4), 10);

    if ((arrayDate[0] >= 0) && (arrayDate[0] < 24)) {
      if ((arrayDate[1] >= 0) && (arrayDate[1] < 60)) {
         return(true);
      }
    }
  }
  return false; //any other values, bad date
} /* end isValidTime */

function showLookupDiv(elem, url) {
   var uid = new Date().getTime();
   var iframe_ref = getObject('hidLookupData_' + elem);
   if (iframe_ref) {
      iframe_ref.src = url + '&ts=' + uid;
      changeObjectDisplay(elem, '');
   } else {
      alert('iframe not found');
   }
} /* end showLookupDiv */
