//
function checkNumber(fld, decSep, e) {
  var keyPressed = '';
  var i = 0;
  var len = 0;
  var decStr = '0123456789';
  var strCheck = '';
  var fv = fld.value;
  //
  len = fv.length;
  if (decSep != '') {
    strCheck = decStr + decSep;
    for(i = 0; i < len; i++){
      if (fv.charAt(i) == decSep){
        strCheck = decStr;
        break;
      }
    }
  }
  else{
    strCheck = decStr;
  }

  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13) return true;  // Enter

  // get value and check for
  keyPressed = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(keyPressed ) == -1) return false;  // Not a valid key
  //
  fld.value = fv + keyPressed;

  return false;
}
// check date function, for correct date
function checkdate(objName, emptyIsOk) {
  var datefield = objName;
  var dv = datefield.value;
  if (dv.length < 1){
    if (emptyIsOk){
      return true;
    }
    else{
      datefield.focus();
      alert("Please supply a date");
      return false;
    }
  }
  // alert("checkdate's  dv=" + dv);
  if (chkdate(objName) == false) {
    datefield.select();
    alert("That date [" + datefield.value + "] is invalid.  Please try again.");
    datefield.focus();
    return false;
  }
  else {
    return true;
    }
  }
//
function chkdate(objName) {
  //var strDatestyle = "US"; //United States date style
  var strDatestyle = "EU";  //European date style
  var strDate;
  var strDateArray;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var booFound = false;
  var datefield = objName;
  var strSeparatorArray = new Array("-"," ","/",".");
  var intElementNr;
  var err = 0;
  var strMonthArray = new Array(12);
  strMonthArray[0] = "Jan";
  strMonthArray[1] = "Feb";
  strMonthArray[2] = "Mar";
  strMonthArray[3] = "Apr";
  strMonthArray[4] = "May";
  strMonthArray[5] = "Jun";
  strMonthArray[6] = "Jul";
  strMonthArray[7] = "Aug";
  strMonthArray[8] = "Sep";
  strMonthArray[9] = "Oct";
  strMonthArray[10] = "Nov";
  strMonthArray[11] = "Dec";
  strDate = datefield.value;
  // alert("chkdate's strDate=" + strDate);
  if (strDate.length < 6) {
      return false;
  }
  for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
    if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      if (strDateArray.length != 3) {
      err = 1;
      return false;
      }
      else {
        strDay = strDateArray[2];
        strMonth = strDateArray[1];
        strYear = strDateArray[0];
      }
      booFound = true;
      }
  }
  if (booFound == false) {
    if (strDate.length>5) {
      strDay = strDate.substr(0, 4);
      strMonth = strDate.substr(4, 2);
      strYear = strDate.substr(6);
      }
  }
  if (strYear.length == 2) {
    strYear = '20' + strYear;
  }
  // US style
  if (strDatestyle == "US") {
    strTemp = strDay;
    strDay = strMonth;
    strMonth = strTemp;
  }
  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    err = 2;
    return false;
  }
  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i = 0;i<12;i++) {
      if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
        intMonth = i+1;
        strMonth = strMonthArray[i];
        i = 12;
        }
    }
    if (isNaN(intMonth)) {
      err = 3;
      return false;
      }
  }
  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    err = 4;
    return false;
  }
  if (intMonth>12 || intMonth<1) {
    err = 5;
    return false;
  }
  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
    err = 6;
    return false;
  }
  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
    err = 7;
    return false;
  }
  if (intMonth == 2) {
    if (intday < 1) {
      err = 8;
      return false;
    }
    if (LeapYear(intYear) == true) {
      if (intday > 29) {
        err = 9;
        return false;
      }
    }
    else {
      if (intday > 28) {
        err = 10;
        return false;
      }
    }
  }
  if (strDatestyle == "US") {
    datefield.value = intMonth + "-" + intday + "-" + strYear;
  }
  else {
    datefield.value = strYear + "-" + intMonth + "-" + intday;
  }
  return true;
  }
  //
  function LeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) { return true; }
  }
  else {
    if ((intYear % 4) == 0) { return true; }
  }
  return false;
  }
  //
  function doDateCheck(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    alert("The dates are valid.");
  }
  else {
    if (from.value == "" || to.value == "")
      alert("Both dates must be entered.");
    else
      alert("To date must occur after the from date.");
    }
  }

function showImageInNewWindow(href) {
  popupWindow(href,'name','600','400','yes');
}

var win = null;
function popupWindow(myurl,myname,w,h,scroll){
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings =
  'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
  win = window.open(myurl,myname,settings)
}
//
function stringToDate(strDate, strDatestyle) {
  var strDateArray;
  var strDay;
  var strMonth;
  var strYear;
  var intDay;
  var intMonth;
  var intYear;
  var strSeparatorArray = new Array("-"," ","/",".");
  var intElementNr;
 
  if (strDate.length < 1) {
      return new Date(1900,1,1);
  }
  for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
    if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      if (strDateArray.length != 3) {
      	 return new Date(1900,1,1);	  
      }
      else {
        if(strDatestyle == "EU") {
		strDay = strDateArray[2];
	        strMonth = strDateArray[1];
	        strYear = strDateArray[0];
        } else {
	        strDay = strDateArray[0];
	        strMonth = strDateArray[1];
	        strYear = strDateArray[2];
        }
      }
      booFound = true;
    }
  }
  if (booFound == false) {
    if (strDate.length>5) {
      strDay = strDate.substr(0, 2);
      strMonth = strDate.substr(2, 2);
      strYear = strDate.substr(4);
      }
  }
  if (strYear.length == 2) {
    strYear = '20' + strYear;
  }
  // US style
  if (strDatestyle == "US") {
    strTemp = strDay;
    strDay = strMonth;
    strMonth = strTemp;
  }
  intDay = parseInt(strDay, 10);
  if (isNaN(intDay)) {
    return new Date(1900,1,1);
  }
  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    return new Date(1900,1,1);
  }
  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return new Date(1900,1,1);
  }
  return new Date(intYear, intMonth -1, intDay);
}
//
function isDateGreater(date1, date2){
	var daydif = date1 - date2;
	if (daydif > 0){
	   return true;
	}
	else{
	   return false;
	}
}
//
function isDateGreaterOrEqual(date1, date2){
	var daydif = date1 - date2;
	if (daydif >= 0){
	   return true;
	}
	else{
	   return false;
	}
}
//
function iscDateStrGreater(cstrDate1, cstrDate2, dStyle){
   var strDate1 = cstrDate1.value;
   var strDate2 = cstrDate2.value;
		 // alert(strDate1 + ", " + strDate2);
   var d1 = stringToDate(strDate1, dStyle);
   var d2 = stringToDate(strDate2, dStyle);
   // var isg = isDateGreater(d1, d2);
   //if (isg){
   //	  alert(strDate1 + " is greater than " + strDate2);
	//}
	//else{
	//	 alert(strDate1 + " is not greater than " + strDate2);
	//}
   return isDateGreater(d1, d2);
   //return isg;

}
//
function isDateStrGreater(strDate1, strDate2, dStyle){

   var d1 = stringToDate(strDate1, dStyle);
   var d2 = stringToDate(strDate2, dStyle);
   
   return isDateGreater(d1, d2);

}
//
//
function isDateStrGreaterOrEqual(strDate1, strDate2, dStyle){

   var d1 = stringToDate(strDate1, dStyle);
   var d2 = stringToDate(strDate2, dStyle);
   
   return isDateGreaterOrEqual(d1, d2);

}
//
function dateStrCompare(strDate1, strDate2, dStyle){
   var cd1 = stringToDate(strDate1, dStyle);
   var cd2 = stringToDate(strDate2, dStyle);
   //
   var daysdif = cd1 - cd2;
   if (daysdif = 0){
	return "=";
   }
   if (daysdif > 0){
	return ">";
   }
   if (daysdif < 0){
	return "<";
   }
   // shit
   return "";
}
function dateStrDiff(strDate1, strDate2, dStyle){
	//alert("dateStrDiff(" + strDate1 + ", " + strDate2 + ", " + dStype + ")");
   var dd1 = stringToDate(strDate1, dStyle);
   var dd2 = stringToDate(strDate2, dStyle);
   //
   var daysdiff = dd1 - dd2;
   
   return daysdiff;
}
function strDateAdd(strDate, numDay, dStyle){
	var dcd = stringToDate(strDate, dStyle);
	var ndcd = new Date(dcd.getTime() + numDay*24*60*60*1000); 
	return ndcd;
}
function dayDiffStrDate(strdbig, strdsmaller, dStyle){
	var dbig = stringToDate(strdbig, dStyle);
	var dsmall = stringToDate(strdsmaller, dStyle);
	var ddf = ((dbig - dsmall) / (24*60*60*1000));
	return RoundToNdp(ddf,0);
}
function RoundToNdp(X, N) { 
	var T = Number('1e'+N);
    	return Math.round(X*T)/T 
}
function maxMe(){
	self.moveTo(0,0);
	self.resizeTo(screen.availWidth,screen.availHeight); 
}



/*********************************************************************/
/* Without RegExps */
function isEmail(who) {
	function isEmpty(who) {
		var testArr=who.split("");
		if(testArr.length==0)
			return true;
		var toggle=0;
		for(var i=0; i<testArr.length; i++) {
			if(testArr[i]==" ") {
				toggle=1;
				break;
			}
		}
		if(toggle)
			return true;
		return false;
	}

	function isValid(who) {
		var invalidChars=new Array("~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?");
		var testArr=who.split("");
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}

	function isfl(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		which=0;
		for(var i=0; i<2; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[which]==invalidChars[j]) {
					return false;
				}
			}
			which=testArr.length-1;
		}
		return true;
	}

	function isDomain(who) {
		var invalidChars=new Array("-","_",".");
		var testArr=who.split("");
		if(testArr.length<2||testArr.length>4) {
			return false;
		}
		for(var i=0; i<testArr.length; i++) {
			for(var j=0; j<invalidChars.length; j++) {
				if(testArr[i]==invalidChars[j]) {
					return false;
				}
			}
		}
		return true;
	}


	var testArr=who.split("@");
	if(testArr.length<=1||testArr.length>2) {
		return false;
	}
	else {
		if(isValid(testArr[0])&&isfl(testArr[0])&&isValid(testArr[1])) {
			if(!isEmpty(testArr[testArr.length-1])&&!isEmpty(testArr[0])) {
				var testArr2=testArr[testArr.length-1].split(".");
				if(testArr2.length>=2) {
					var toggle=1;
					for(var i=0; i<testArr2.length; i++) {
						if(isEmpty(testArr2[i])||!isfl(testArr2[i])) {
							toggle=0;
							break;
						}
					}
					if(toggle&&isDomain(testArr2[testArr2.length-1]))
						return true;
					return false;
				}
				return false;
			}
		}
	}
}

/* With RegExp */
function isEmail2(who) {
	var email=/^[A-Za-z0-9][\w-.]+[A-Za-z0-9]@[A-Za-z0-9]([\w-.]+[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i;
	return(email.test(who));
}


//only allow (, ), space, - and  0-9 for phone field
function validPhone(thisKeyCode)
{
	if(thisKeyCode==40 || thisKeyCode==41 || thisKeyCode==32 || thisKeyCode==45 || (thisKeyCode>=48 && thisKeyCode<=57))
		return true;
	return false;
}

//only allow 0-9, -, /, A-Z and a-z for base data code
function validBaseCode(thisKeyCode)
{
	if(thisKeyCode==45 || thisKeyCode==47 || (thisKeyCode>=48 && thisKeyCode<=57) || (thisKeyCode>=65 && thisKeyCode<=90) || (thisKeyCode>=97 && thisKeyCode<=122))
		return true;
	return false;
}

//only allow 0-9, -, /, A-Z and a-z and . for path
function validPath(thisKeyCode)
{
	if(thisKeyCode==95 || thisKeyCode==45 || thisKeyCode==46 || thisKeyCode==47 || (thisKeyCode>=48 && thisKeyCode<=57) || (thisKeyCode>=65 && thisKeyCode<=90) || (thisKeyCode>=97 && thisKeyCode<=122))
		return true;
	return false;
}

//only allow 0-9, -, /, A-Z and a-z PLUS single quote
function validBaseCodeWithSingleQuote(thisKeyCode)
{
	if(validBaseCode(thisKeyCode) || thisKeyCode==39)
		return true;
	return false;
}

//only allow 0-9, -, A-Z and a-z for those job, booking, BL number ...
function validDocNumber(thisKeyCode)
{
	if(thisKeyCode==45 || (thisKeyCode>=48 && thisKeyCode<=57) || (thisKeyCode>=65 && thisKeyCode<=90) || (thisKeyCode>=97 && thisKeyCode<=122))
		return true;
	return false;
}

//only allow 0-9, -, A-Z and a-z plus AMP for those job, booking, BL number ...
function validDocNumberWithPercent(thisKeyCode)
{
	if(thisKeyCode==37 || thisKeyCode==45 || (thisKeyCode>=48 && thisKeyCode<=57) || (thisKeyCode>=65 && thisKeyCode<=90) || (thisKeyCode>=97 && thisKeyCode<=122))
		return true;
	return false;
}

//only allow 0-9, - for date input
function validDateChars(thisKeyCode)
{
	if(thisKeyCode==45 || (thisKeyCode>=48 && thisKeyCode<=57))
		return true;
	return false;
}

//only allow 0-9 for digit only
function validDigit(thisKeyCode)
{
	if(thisKeyCode>=48 && thisKeyCode<=57)
		return true;
	return false;
}


//only allow 0-9, A-Z for digitDot only
function validDigitDotPlusAlpha(thisKeyCode)
{
	if(thisKeyCode==46 || (thisKeyCode>=48 && thisKeyCode<=57) || (thisKeyCode>=65 && thisKeyCode<=90))
		return true;
	return false;
}

//only allow 0-9 for digitDot only
function validDigitDot(thisKeyCode)
{
	if(thisKeyCode==46 || (thisKeyCode>=48 && thisKeyCode<=57))
		return true;
	return false;
}

//only allow A-Z and a-z for alpha only
function validAlpha(thisKeyCode)
{
	if((thisKeyCode>=65 && thisKeyCode<=90) || (thisKeyCode>=97 && thisKeyCode<=122))
		return true;
	return false;
}

//only allow +, -
function validDebitCredit(thisKeyCode)
{
	if(thisKeyCode==43 || thisKeyCode==45)
		return true;
	return false;
}

//only allow +, -, And 0-9
function validSignInteger(thisKeyCode)
{
	if(thisKeyCode==43 || thisKeyCode==45 || (thisKeyCode>=48 && thisKeyCode<=57))
		return true;
	return false;
}



//currency formatting
function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1)
			aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

//trim all spaces for all text fields
function trimAllTextInputBox()
{
	var numForms = document.forms.length;
	var numElms = null;
	var formJumper = null;
	var elmJumper = null;
	//Loop through all the forms
	for(formJumper=0; formJumper<numForms; formJumper++) {
		numElms = document.forms[formJumper].length;
		for(elmJumper=0; elmJumper<numElms; elmJumper++) {
			if(document.forms[formJumper].elements[elmJumper].type == "text") {
				document.forms[formJumper].elements[elmJumper].value=document.forms[formJumper].elements[elmJumper].value.replace(/^\s+/g, '');
				document.forms[formJumper].elements[elmJumper].value=document.forms[formJumper].elements[elmJumper].value.replace(/\s+$/g, '');
			}
		}//End of all elements of a form
	}//End of all forms
}




function changeCurrency(aform)
{
	if(aform.change_currency.value=="$") {
		return "";
	}
	if(aform.change_currency.value=="") {
		alert("Invalid currency code, cannot change currency");
		return;
	}

	if(confirm("Are you sure you would like to change currency?")) {
		aform.a.value = "999";
		aform.submit();
	}
}