//
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;
}
//
  function move(fbox,tbox,sortitems) {
    for(var i=0; i<fbox.options.length; i++) {
      if(fbox.options[i].selected && fbox.options[i].value != "") {
        //alert("selected=" + fbox.options[i].value );
        var no = new Option();
        no.value = fbox.options[i].value;
        no.text = fbox.options[i].text;
        tbox.options[tbox.options.length] = no;
        fbox.options[i].value = "";
        fbox.options[i].text = "";
         }
    }
    packUp(fbox);
    if (sortitems){
       SortD(tbox);
    }
  }
//
  function moveSourceStay(fbox,tbox,sortitems) {
    for(var i=0; i<fbox.options.length; i++) {
      if(fbox.options[i].selected && fbox.options[i].value != "") {
        //alert("selected=" + fbox.options[i].value );
        if (!itemExists(tbox, fbox.options[i].value)){
          var no = new Option();
          no.value = fbox.options[i].value;
          no.text = fbox.options[i].text;
          tbox.options[tbox.options.length] = no;
        }
        else{
          alert("Item [" + fbox.options[i].text + "] exist");
        }
         }
    }
    if (sortitems){
       SortD(tbox);
    }
  }
  function itemExists(tbox, fvalue){
    // alert("check for " + fvalue);
    var ret = false;
    for(var i=0; i<tbox.options.length; i++){
      var tvalue = tbox.options[i].value;
      // alert("tbox[" + i + "] value = " + tvalue);
      if (tvalue == fvalue){
        // alert("tbox=" + tvalue + ", fvalue=" + fvalue);
        ret = true;
        break;
      }
    }
    return ret;
  }
  function removeSource(box, sortitems){
    for(var i=0; i<box.options.length; i++) {
      if(box.options[i].selected && box.options[i].value != "") {
        box.options[i].value = "";
        box.options[i].text = "";
         }
    }
    packUp(box);
    if (sortitems){
       SortD(tbox);
    }

  }
  //

  function packUp(box)  {
    for(var i=0; i<box.options.length; i++) {
      if(box.options[i].value == "" && box.options[i].text == "")  {
        for(var j=i; j<box.options.length-1; j++)  {
          box.options[j].value = box.options[j+1].value;
          box.options[j].text = box.options[j+1].text;
        }
        var ln = i;
        break;
        }
    }
    if(ln < box.options.length)  {
      box.options.length -= 1;
      packUp(box);
      }
  }

  function SortD(box)  {
    var temp_opts = new Array();
    var temp = new Object();
    for(var i=0; i<box.options.length; i++)  {
      temp_opts[i] = box.options[i];
    }
    for(var x=0; x<temp_opts.length-1; x++)  {
      for(var y=(x+1); y<temp_opts.length; y++)  {
        if(temp_opts[x].text > temp_opts[y].text)  {
          temp = temp_opts[x].text;
          temp_opts[x].text = temp_opts[y].text;
          temp_opts[y].text = temp;
          temp = temp_opts[x].value;
          temp_opts[x].value = temp_opts[y].value;
          temp_opts[y].value = temp;
              }
        }
    }
    for(var i=0; i<box.options.length; i++)  {
      box.options[i].value = temp_opts[i].value;
      box.options[i].text = temp_opts[i].text;
      }
  }
  function selectAll(box){
    for(var i=0; i<box.options.length; i++) {
      box.options[i].selected = true;
    }
  }
  // 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[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)) {
    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 = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
    datefield.value = intMonth + "/" + intday + "/" + strYear;
  }
  else {
    //datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
    datefield.value = intday + "/" + intMonth + "/" + strYear;
  }
  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 {
        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 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 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); 
}
//
function ltrim(str){

   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}
//
function rtrim(str){

   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}
//
function trim(str){
   return rtrim(ltrim(str));
}
