function showWindow(file) {      
      var url = location.protocol+"//"+location.host+'/'+file;      
      var myRef = window.open(''+url,'popup',
          'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,status=0,menubar=0,directories=0,location=0');                
}

function countDigitsInID(id) {
    var number = document.getElementById(id).value;
    return countDigits(number);
}

function countDigits(number) {
    var str = '';
    str = str + number;
    return str.length;
}

function isDigits(id) {
    var str = document.getElementById(id).value;
    return str.match(/[0-9]+/) == str;
}

function isEmpty(id) {
    var val = document.getElementById(id).value;
    if ( val.length <= 0 ) {
        return true;        
    }
    return false;
}

function isMatchPattern(id,pattern) {
    var val = document.getElementById(id).value;
    return val.match(pattern) == val;
}

function isValidMonth(id) {
    var val = document.getElementById(id).value;
    var num = parseInt(val, 10);
    return num <= 12 && num > 0;
}

function isValidDay(yearid, monthid,id) {
    var month = document.getElementById(monthid).value;
    var year = document.getElementById(yearid).value;
    var mnum = 1;
    if ( month != null ) {
        mnum = parseInt(month, 10);
    }
    var myear = 2009;
    if ( year != null ) {
        myear = parseInt(year, 10);
    }
    var val = document.getElementById(id).value;
    var num = parseInt(val, 10);
    if ( mnum == 1 || mnum == 3 || mnum == 5 || mnum == 7 || mnum == 8
            || mnum == 10 || mnum == 12 ) {
        return num <= 31 && num >= 1;
    }
    if ( mnum == 4 || mnum == 6 || mnum == 9 || mnum == 11 ) {
        return num <= 30 && num >= 1;
    }
    if ( mnum == 2 ) { // feb
        var days = (((myear % 4 == 0) && ( (!(myear % 100 == 0)) || (myear % 400 == 0))) ? 29 : 28 );
        return num <= days && num >= 1;
    }
    return true;

}

function hide(id) {
    document.getElementById(id).style.visibility='hidden';
}

function show(id) {
    document.getElementById(id).style.visibility='visible';
}

function setError(helpid,text) {
    document.getElementById(helpid).className = 'helpcell_highlight';
    document.getElementById(helpid).innerHTML = text;    
}

function clearError(helpid,str) {
    document.getElementById(helpid).className = 'helpcell';
    if ( str != null ) {
        document.getElementById(helpid).innerHTML = str;
    }
    else {
        document.getElementById(helpid).innerHTML = "";
    }
}

function getHttp(url) {
    var http = false;  
    var res = '';
    if(navigator.appName == "Microsoft Internet Explorer") {
      http = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      http = new XMLHttpRequest();
    }

    http.open("GET", url);
    http.onreadystatechange= function() {
            if(http.readyState == 4) {
                    res = http.responseText;                        
            }            
    }        
    return res;

}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}


function loadPage(id,source,rand) {
        var http = false;                
        var rand_no = Math.random()
        var appendix = "&rand="+rand_no+"&";
        if ( rand == null ) {
            appendix  = "";
        }        
        if(navigator.appName == "Microsoft Internet Explorer") {
          http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
          http = new XMLHttpRequest();
        }        
        http.open("GET", source+appendix);
        http.onreadystatechange=function() {
                if(http.readyState == 4) {   
//                    alert("loaded:"+source);
//		    alert(http.responseText);
//		    
                    var text = 	http.responseText;
		    var len = text.length;
		      document.getElementById(id).innerHTML = http.responseText;
//                      alert("set done to true");
//                      http.send(null);
                      return true;
                }
                
        }        
        http.send(null);                
        return false;
}	


       

function replaceFrame(iframid,selected,source,rand) {
    var rand_no = Math.random();
    var appendix = "rand="+rand_no+"&";
    if ( rand == null ) {
        appendix  = "";
    }
    document.getElementById(iframid).src=source+appendix;    
    var selectedb = document.getElementById(selected);
    selectedb.className='selected'; 
    if ( currentSelection != selected ) {
            document.getElementById(currentSelection).className=null; 
    }            
    currentSelection = selected;
}
       

	
function checkLoginState(link) {
        var http = false;

        // use rand number to make sure the page is not cached
        var rand_no = Math.random()
        link = link+"?rand="+rand_no+"&";

        if(navigator.appName == "Microsoft Internet Explorer") {
          http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
          http = new XMLHttpRequest();
        }

        http.open("GET",link);
        http.onreadystatechange=function() {
                if(http.readyState == 4) {
                        var str = http.responseText;
                        var index = str.indexOf("loggedin", 0);
                        if ( index <= 0 ) {
                            // handle logged out                                    
                            window.location.reload( false );
                        }                                   
                }
        }
        http.send(null);	
        window.setTimeout("checkLoginState('checkLogin.jsp')", 20*60*1000 );
}

function alterTextForCheckBox(texttagid,checkboxtagid,onclass,ontext,offclass,offtext) {
    var texttag = document.getElementById(texttagid);
    var checkbox = document.getElementById(checkboxtagid);
    if ( checkbox == null ) {
        return;
    }
    var value = checkbox.checked;
    
    if ( value == true )  {
        texttag.innerHTML = ontext;        
        texttag.className = onclass;
    }
    else {
        texttag.innerHTML = offtext;
        texttag.className = offclass;
    }
}



