var itsConfirmType = "";
var confirmed = false;
var _oldColor='#FFFFFF';
var _oldButtonColor='';

var _currentSelected = false;

function setSelected(aSpan){
  aSpan.style.fontWeight = 'bold';
  if (_currentSelected != false){
    if (aSpan.id != _currentSelected.id){
      _currentSelected.style.fontWeight = 'normal';
    }
  }
  _currentSelected = aSpan;
}

function setConfirmType(type){
  itsConfirmType = type;
}

function askConfirm(){
  confirmed = confirm("are you sure you want to "+itsConfirmType+" the selection?");
}
function getConfirm(){
  return confirmed;
}

function setFocus(formname){
  curform = MM_findObj(formname);
    curform.focus();
}

function selectAll(formname){
  curform = MM_findObj(formname);
    iArrayLength = curform.elements.length;
    for(i=0;i<iArrayLength;i++) {
         if(curform.elements[i].type =="checkbox"){
              curform.elements[i].checked=true;
         }
  }
}



/*
dropdown filter adapted from:

  Author: Justin Whitford
  Source: www.evolt.org

*/

function filtery(pattern, list){
 for (n=0;n<list.length;n++){
    if(list[n].text.toLowerCase().indexOf(pattern.toLowerCase())==0){
      list.selectedIndex=n;
      found = true;
      break;
    }else
      found = false;
  }


  /*
  if (!list.bak){

    list.bak = new Array();
    for (n=0;n<list.length;n++){
      list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
    }
  }


  match = new Array();
  nomatch = new Array();
  for (n=0;n<list.bak.length;n++){
    if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())==0){
      match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }else{
      nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
    }
  }


  for (n=0;n<match.length;n++){
    list[n].value = match[n][0];
    list[n].text = match[n][1];
  }
  for (n=0;n<nomatch.length;n++){
    list[n+match.length].value = nomatch[n][0];
    list[n+match.length].text = nomatch[n][1];
  }

  list.selectedIndex=0;
  */
}



function printit(){
  if (window.print) {
      window.print() ;
  } else {
      var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
      WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
  }
}

function deselectAll(formname){
  curform = MM_findObj(formname);
    iArrayLength = curform.elements.length;
    for(i=0;i<iArrayLength;i++){
         if(curform.elements[i].type =="checkbox"){
              curform.elements[i].checked=false;
         }
  }
}

function enter(nextfield) {
  if(window.event && window.event.keyCode == 13) {
      nextfield.focus();
      return false;
  } else {
      return true;
    }
}

function setOldColor(row){
  row.style.backgroundColor=_oldColor;
}
function setOverColor(row){
  _oldColor = row.style.backgroundColor;
  row.style.backgroundColor='#D7D7D7';
}


function openwindow(par){
  window.open(par);
}

function changeImage(imgName,imgObj) {
  document.images[imgName].src = eval(imgObj+".src");
}

function preload(imgObj,imgSrc) {
    if (document.images) {
      eval(imgObj+' = new Image()');
        eval(imgObj+'.src = "'+imgSrc+'"');
    }
}

function scrollup(){
  window.scroll(0, -(document.body.scrollHeight - document.body.offsetHeight));
}

function scrolldown(y_pos){
  if (window.y_pos){
    window.scroll(0, y_pos);
  } else {
    window.scroll(0, (document.body.scrollHeight - document.body.offsetHeight));
  }
}

function trim(str){
  str = str.replace(/ /g,"");
  return str;
}

function toggleBool(aElem){
    if (aElem.value == "N" || aElem.value == "") {
      aElem.value = "Y";
    } else {
      aElem.value = "N";
    }
}

function toggleForeign(Div, cOne, cTwo){
  if (trim(Div.innerHTML)==trim(cOne)){
    Div.innerHTML= cTwo;
  } else {
    Div.innerHTML= cOne;
  }
}

function toggleDetail(ddSpan){
  if (ddSpan.style.visibility=="hidden" || ddSpan.style.visibility==""){
    ddSpan.style.visibility="visible";
    ddSpan.style.display="inline";
    ddSpan.style.width=100;
  } else {
    ddSpan.style.visibility="hidden";
    ddSpan.style.display="none";
    ddSpan.style.width=0;
  }
}

function toggleElement(ddSpan){
  if (ddSpan.style.visibility=="hidden" || ddSpan.style.visibility==""){
    ddSpan.style.visibility="visible";
    ddSpan.style.display="inline";
  } else {
    ddSpan.style.visibility="hidden";
    ddSpan.style.display="none";
  }
}

function toggleDetailSmall(ddSpan,ddMain,ddButton){
  if (ddSpan.style.visibility=="hidden" || ddSpan.style.visibility==""){
    ddSpan.style.visibility="visible";
    ddSpan.style.width=40;
    ddMain.style.left="47px";
    ddButton.innerHTML="&gt;&gt;";
    ddMain.style.width=document.body.clientWidth-55;

  } else {
    ddSpan.style.visibility="hidden";
    ddSpan.style.width=0;
    ddMain.style.left="5px";
    ddButton.innerHTML="&lt;&lt;";
    ddMain.style.width=document.body.clientWidth-10;

  }
}

function toggleDetail2(expanded,key,expand){
  if (expand.value=='true' ){
    key.style.visibility="visible";
    key.style.display="inline"
    expanded.style.visibility="hidden";
    expanded.style.display="none";
    expand.value='false';
  } else {
    key.style.visibility="hidden";
    key.style.display="none"
    expanded.style.visibility="visible";
    expanded.style.display="inline";
    expand.value='true';
  }
}

/*
 *  For date selector
 */
function isValidDate(year, month, day){
  if ( isInteger(year) && isInteger(month) && isInteger(day) ){
    if (year.length == 4){
      if (month >= 0 && month <= 12 && month.charAt(0) >= 0 && month > "00"){
        if (day >= 0 && day <= 31 && day.charAt(0) >= 0 && day > "00"){
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    } else {
      return false;
    }
  } else {
    return false;
  }
}

function dateFormatJS(aDate, oldDate, aFormat, aMessage){
  if (aDate == ""){
    return aDate;
  }
  var c = aDate.charAt(2);
  if (c == "-"){
    var year = aDate.charAt(6) + aDate.charAt(7) + aDate.charAt(8) + aDate.charAt(9);
    var month = aDate.charAt(3) + aDate.charAt(4);
    var day = aDate.charAt(0) + aDate.charAt(1);
    if (!isValidDate(year, month, day) ){
      if (aMessage == 1){
        alert(aDate + " is not in the valid date format. (dd-mm-yyyy)!");
      }
      var year = oldDate.charAt(0) + oldDate.charAt(1) + oldDate.charAt(2) + oldDate.charAt(3);
      var month = oldDate.charAt(5) + oldDate.charAt(6);
      var day = oldDate.charAt(8) + oldDate.charAt(9);
    }
    if (aFormat == 1){
      return year + "-" + month + "-" + day;
    } else {
      return day + "-" + month + "-" + year;
    }
  } else if ( isDigit(c) ) {
    var year = aDate.charAt(0) + aDate.charAt(1) + aDate.charAt(2) + aDate.charAt(3);
    var month = aDate.charAt(5) + aDate.charAt(6);
    var day = aDate.charAt(8) + aDate.charAt(9);
    if (!isValidDate(year, month, day)){
      if (aMessage == 1){
        alert(aDate + " is not in the valid date format. (dd-mm-yyyy)!");
      }
      var year = oldDate.charAt(0) + oldDate.charAt(1) + oldDate.charAt(2) + oldDate.charAt(3);
      var month = oldDate.charAt(5) + oldDate.charAt(6);
      var day = oldDate.charAt(8) + oldDate.charAt(9);
    }
    if (aFormat == 1){
      return year + "-" + month + "-" + day;
    } else {
      return day + "-" + month + "-" + year;
    }
  } else {
    if (aMessage == 1){
      alert(aDate + " is not in the valid date format. (dd-mm-yyyy)!");
    }
    return dateFormatJS(oldDate, oldDate, aFormat, 0);
  }
}

/*
 *  For intControl
 */
function validateInt(aFormElement, oldValue){
  if (!isInteger(aFormElement.value)){
    alert(aFormElement.value + " is not an integer!");
    aFormElement.value = oldValue;
  }
}

function isInteger (s){
  var i;
  if (isEmpty(s))
  if (isInteger.arguments.length == 1){
    return 0;
  } else {
    return (isInteger.arguments[1] == true);
  }
  for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
    if (!isDigit(c) && !(i == 0 && c == '-')){
      return false;
    }
  }
  return true;
}




function isEmpty(s) {
  return ((s == null) || (s.length == 0))
}

function isDigit (c){
  return ((c >= "0") && (c <= "9"))
}

/*
  For Percentage Control
*/


function validatePercentage(aFormElement, oldValue){
  if(!isNumber(aFormElement.value)){
    alert(aFormElement.value + " is not a valid pecentage!");
    aFormElement.value = oldValue;
  }
  if (aFormElement.value < 0 || aFormElement.value > 100){
    alert(aFormElement.value + " is not a valid pecentage!");
    aFormElement.value = oldValue;
  }
}

function isNumber(f){
  var decimal_seperator = false;
  for (i = 0; i < f.length; i++){
    var c = f.charAt(i);
    if (!isDigit(c)){
      if (c == '.' || c == ',') {
        if (decimal_seperator == true) {
          return false;
        }
        decimal_seperator = true;
      } else {
        return false;
      }
    }
  }
  return true;
}


/*
  For EOLNonn.php
*/
function NONNDeSelectAll(menuFrom){
  for (var i=0; i<menuFrom.options.length; i++) {
    menuFrom.options[i].selected = false;
  }
}

function NONNSelectAll(menuFrom){
  for (var i=0; i<menuFrom.options.length; i++) {
    menuFrom.options[i].selected = true;
  }
}

function changeCol(menuFrom, menuTo, boolFrom){
  for (var i=0; i<menuFrom.options.length; i++) {
    if(menuFrom.options[i].selected) {
      var newValue = menuFrom.options[i].value;
      var newLabel = menuFrom.options[i].text;
      menuFrom.remove(i);
      menuTo.options[menuTo.options.length]= new Option(newLabel, newValue);
      i--;
    }
  }
}
