//  Commonly used functions for Javascript
//  By Joel Bauca
var errhnd=false
function seekVal(to,val){
  for (ctr1=0;ctr1<to.options.length;ctr1++){
	 if(to.options[ctr1].value==val){
		to.options[ctr1].selected=true;
		break;
	 }
  } 
}
function IsInt(iobj){ //Integer Validation
   var myString = iobj
   var inThere = myString.match(/^(\d)+$/g)
   if (inThere) 
      return true //Invalid number
   return false //Valid Number
}
function IsFloat(iobj){ //Float Validation
   var myString = iobj
   var inThere = myString.match(/^(\d)+\.\d{1,2}$/g)
   if (inThere) 
      return true //Invalid number
   return false //Valid Number
}
function IsColor(iobj){ //Color Validation
   var myString = iobj
   var inThere = myString.match(/\#([0-9a-fA-F]){6}/g)
   if (inThere) {
	  return true //Valid Color
   }
   return false //Invalid Color
}
function IsEmail(ieobj){//Email Validation
   var estr = ieobj          
   var eres = estr.match(/^(\s)*[_a-z0-9A-Z-]+(\.[_a-z0-9A-Z-]+)*@[a-z0-9A-Z-]+(\.[a-z0-9A-Z-]+)+$/g)
   if (eres)
	  return true  //Valid Email
   return false  //Invalid Email
}
function IsNull(obj){ //Null Validation
   var myString = obj
   var inThere = myString.match(/\S/g)
   if (inThere) 
	  return false //Invalid number
   return true //Valid Number
}
 
function IsDate(dateStr){
    var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Y-m-d
    var matchArray = dateStr.match(datePat); // is the format ok?
    var monthname=""
    if (matchArray == null) 
        return false;

    month = matchArray[3]; // parse date into variables
    day = matchArray[5];
    year = matchArray[1];
	
    if (year<1900)
	   return false;
	if (year>2500)
	   return false;
    if (month < 1 || month > 12)  // check month range
        return false;

    if (day < 1 || day > 31)
        return false;
		
	if (month == 2)monthname="February";
	else if (month == 4)monthname="April";
	else if (month == 6)monthname="June";
	else if (month == 9)monthname="September";
	else if (month == 11)monthname="November";

    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
        return false;

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) 
            return false;
    }
    return true; // date is valid
}
function DateDiff(date1,date2){ //from,to
 
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Y-m-d
    var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Y-m-d
    var matchArray1 = date1.match(datePat); // is the format ok?
    var matchArray2 = date2.match(datePat); // is the format ok?

    mm1 = matchArray1[3]; // parse date into variables
    dd1 = matchArray1[5];
    yy1 = 20 + matchArray1[1];
    mm2 = matchArray2[3]; // parse date into variables
    dd2 = matchArray2[5];
    yy2 = 20 + matchArray2[1];
    var dat2 = new Date(mm2 + "/" + dd2 +"/" + yy2)
    var dat1 = new Date(mm1 + "/" + dd1 +"/" + yy1)
	var diff = new Date()
	diff=dat2.getTime() - dat1.getTime();
    days = Math.floor(diff / (1000 * 60 * 60 * 24)); 
	return days
}

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) { //v3.0
  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); 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 delrec(a,xm,f,act){
   if (delrec!=""){
      if(xm==null)
	     xm = "Do you really want to delete this record?"
      if(confirm(xm)){
		  if(f!=null)
			 document.form1.func.value=f
		  if(act!=null)
			 document.form1.action=act
	     document.form1.delid.value=a
		 document.form1.method="POST"
		 document.form1.submit()
	  }
   }
}
function str_replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += str_replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function opennewwindow(mypage,h,w,n,winname){
  if ((IsNull(h)) || (IsNull(w)) || (IsNull(n)) || (!IsInt(h)) || (!IsInt(w)) || ((n!="no")&&(n!="yes")))
     prpty=""
  else {
     locx = (screen.width - w)/2; 
     locy = (screen.height - h)/2; 
     prpty="status=yes,resizable=no,height="+h+",width="+w+",left="+locx+",top="+locy+",menubar=no,toolbar=no,scrollbars=" + n
  }
  if (IsNull(winname))
     wn = null
  else
     wn = winname
  if (!IsNull(mypage)){	 
	 mywin =  window.open(mypage,wn,prpty)
	 mywin.focus()
  }
}
function setOrder(a,b){
   document.form1.method="post"
   document.form1.ord.value=a
   document.form1.so.value=b
   document.form1.submit()
}
function setPage(a){
   document.form1.method="post"
   document.form1.ts.value=a
   document.form1.submit()
}

function IntKeypress(){
	var string="1234567890";
	if (string.indexOf(unescape('%' + window.event.keyCode.toString(16))) != -1) 
		window.event.returnValue = true;
	else
		window.event.returnValue = false;
}
function FloatKeypress(){
	var string="1234567890.";
	if (string.indexOf(unescape('%' + window.event.keyCode.toString(16))) != -1) 
		window.event.returnValue = true;
	else
		window.event.returnValue = false;
}
function displayAlert() {   
	 errhnd=false
 }   
function displayerror(){
	errhnd=true
}
function testimage(J){
	errhnd=false
	imageA = new Image(100,100)
	imageA.onload=displayAlert
	imageA.onerror=displayerror
	imageA.src=J
}
function chkimg(imgname,nnflag,noneflag){
var imgsrc,imgold,uploadpath,nflag,nopic
    tf = document.form1
    uploadpath = tf.uploadpath.value
	nopic = tf.nopic.value
	imgsrc = document.getElementById(imgname).value
	imgold = document.getElementById("h"+imgname).value
	if (noneflag=="1"){
	   if (document.getElementById("n"+imgname).checked==true){	
	      imgsrc=""
		  if (nnflag=='1'){
			  alert('Invalid Image File!')
			  errhnd=true
			  return false
		  }
	      document.images["d"+imgname].src=uploadpath+nopic
		  return false
	   }
	}
    if(IsNull(imgsrc)){
	   if ((nnflag=='1')&&(imgold==nopic)){
		  alert('Invalid Image File!')
		  errhnd=true
		  return false
	   }
	   if (imgold==nopic)
		  document.getElementById("n"+imgname).checked=true
	   document.images["d"+imgname].src=uploadpath+imgold
	   return false
	}
    else {
	    imgsrc = "file:///" + str_replace(imgsrc,"\\","/")
		ext=imgsrc.substr(imgsrc.lastIndexOf(".")+1)
		if ((ext.toUpperCase()!="JPG") && (ext.toUpperCase()!="JPE") && (ext.toUpperCase()!="JPEG") && (ext.toUpperCase()!="GIF") && (ext.toUpperCase()!="PNG")){
	       alert('Invalid Image File!')
	       errhnd=true
		   return false
		}	
        testimage(imgsrc)
	    if (errhnd) {
	       alert('Invalid Image File!')
	       errhnd=true
		   return false
	    }
		else {
	       document.images["d"+imgname].src=imgsrc
		   document.getElementById("n"+imgname).checked=false
		   return false
		}
    }   
}

//This is use to detect older versions of the browser being used.
var BrowserDetect = {
 init: function () {
  this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
  this.version = this.searchVersion(navigator.userAgent)
   || this.searchVersion(navigator.appVersion)
   || "an unknown version";
  this.OS = this.searchString(this.dataOS) || "an unknown OS";
 },
 searchString: function (data) {
  for (var i=0;i<data.length;i++) {
   var dataString = data[i].string;
   var dataProp = data[i].prop;
   this.versionSearchString = data[i].versionSearch || data[i].identity;
   if (dataString) {
    if (dataString.indexOf(data[i].subString) != -1)
     return data[i].identity;
   }
   else if (dataProp)
    return data[i].identity;
  }
 },
 searchVersion: function (dataString) {
  var index = dataString.indexOf(this.versionSearchString);
  if (index == -1) return;
  return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
 },
 dataBrowser: [
  {
   prop: window.opera,
   identity: "Opera"
  },
  {
   string: navigator.userAgent,
   subString: "Firefox",
   identity: "Firefox"
  },
  { // for newer Netscapes (6+)
   string: navigator.userAgent,
   subString: "Netscape",
   identity: "Netscape"
  },
  {
   string: navigator.userAgent,
   subString: "MSIE",
   identity: "Explorer",
   versionSearch: "MSIE"
  },
  {  // for older Netscapes (4-)
   string: navigator.userAgent,
   subString: "Mozilla",
   identity: "Netscape",
   versionSearch: "Mozilla"
  }
 ],
 dataOS : [
  {
   string: navigator.platform,
   subString: "Win",
   identity: "Windows"
  },
  {
   string: navigator.platform,
   subString: "Mac",
   identity: "Mac"
  },
  {
   string: navigator.platform,
   subString: "Linux",
   identity: "Linux"
  }
 ]
};
BrowserDetect.init();

switch(BrowserDetect.browser) {
	case "Explorer":
		if (BrowserDetect.version <= 5.0) {
			alert("You are currently using an old version of Internet Explorer.\nTo best view this site, it is recommended that you download \nand install the latest version. You will be redirected there shortly.");
			document.location.href("http://www.microsoft.com/windows/ie/downloads/default.mspx");
		};
		break;
	case "Netscape":
		if (BrowserDetect.version <= 5.0) {
			alert("You are currently using an old version of Netscape.\nTo best view this site, it is recommended that you download \nand install the latest version. You will be redirected there shortly.");
			document.location.href("http://browser.netscape.com/ns8/download/archive72x.jsp");
		};
		break;
	case "Firefox":
		if (BrowserDetect.version <= 1.5) {
			alert("You are currently using an old version of Firefox.\nTo best view this site, it is recommended that you download \nand install the latest version. You will be redirected there shortly.");
			document.location.href("http://www.mozilla.com/firefox/");
		};
		break;
	case "Opera":
		if (BrowserDetect.version <= 8.0) {
			alert("You are currently using an old version of Opera.\nTo best view this site, it is recommended that you download \nand install the latest version. You will be redirected there shortly.");
			document.location.href("http://www.opera.com/download/");
		};
		break;
}