function clickButton(e, buttonid){ 
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt){ 
          if (evt.keyCode == 13){ 
                bt.click(); 
                return false; 
          } 
      } 
}
function goToNextBox(e, thisboxid, maxlength, nextboxid){ 
      var thisbox = document.getElementById(thisboxid);
      var nextbox = document.getElementById(nextboxid);
      if (thisbox){ 
          if (thisbox.value.length == maxlength -1){ 
                //get value entered
                var keynum
                if(window.event) // IE
                  {keynum = e.keyCode}
                else if(e.which) // Netscape/Firefox/Opera
                  {keynum = e.which}
                //add value to textbox and reset focus
                thisbox.value = thisbox.value + String.fromCharCode(keynum)
                if (nextbox) {nextbox.focus();} 
                return false; 
          }
      } 
}
function showUploadingMessage(browseboxid, messagecontrolid){
      var browsebox = document.getElementById(browseboxid);
      var messagecontrol = document.getElementById(messagecontrolid);
      if (browsebox){
         if (browsebox.value!=""){
            messagecontrol.innerHtml="Please standby while we upload your image...";
          }else{
            messagecontrol.innerHtml="";
          }
      }
      return true;
}
function setID(itemcontrolid, itemid, resultcontrolid, resultid){
      var itemcontrol = document.getElementById(itemcontrolid);
      var resultcontrol = document.getElementById(resultcontrolid);
      if (itemcontrol && resultcontrol){
        itemcontrol.value = itemid;
        resultcontrol.value = resultid;
        return true;
      }else{return false;}
}
function confirmMessage(message) {
  return confirm(message);
}
function submitonce(submitbuttonid){
    var submitbutton = document.getElementById(submitbuttonid);
    if (submitbutton){
      submitbutton.disabled = true;
      __doPostBack(submitbutton,"")
    }else{return false;}
}
function stripHTML(){
  var numberForms = document.forms.length;
  var formIndex =0;
  var myvalue="";
  for (formIndex=0; formIndex<numberForms; formIndex++){
    for(x=0;x<document.forms[formIndex].length;x++){
      if(document.forms[formIndex].elements[x].type=='textarea'||document.forms[formIndex].elements[x].type=='text'){
        myvalue = document.forms[formIndex].elements[x].value;
        document.forms[formIndex].elements[x].value=myvalue.replace(/(<([^>]+)>)/ig,""); 
      }
    }
  }
}
function leftTrim(myStr){
  while (myStr.substring(0,1) == ' '){
    myStr = myStr.substring(1, myStr.length);
  }
  return myStr;
}
function rightTrim(myStr){
  while (myStr.substring(myStr.length-1, myStr.length) == ' '){
    myStr = myStr.substring(0,myStr.length-1);
  }
  return myStr;
}
function allTrim(myStr){
  myStr = leftTrim(myStr);
  myStr = rightTrim(myStr);
  return myStr;
}
function stripCharacter(words,character) {
	 var spaces = words.length;
	 for(var x = 1; x<spaces; ++x){
	   words = words.replace(character, "");   
	 }
	 return words;
}
function changeCss(theClass,element,value) {
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	  }
	 }	
}
function checkUncheckAll(theElement) {
   var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
   }
}
function checkUncheckSome(controller,theElements) {
	//Programmed by Shawn Olson
	//Copyright (c) 2006
	//Permission to use this function provided that it always includes this credit text
	//  http://www.shawnolson.net
	//Find more JavaScripts at http://www.shawnolson.net/topics/Javascript/
	//theElements is an array of objects designated as a comma separated list of their IDs
	//If an element in theElements is not a checkbox, then it is assumed
	//that the function is recursive for that object and will check/uncheck
	//all checkboxes contained in that element
	 var formElements = theElements.split(',');
	 var theController = document.getElementById(controller);
	 for(var z=0; z<formElements.length;z++){
	  theItem = document.getElementById(formElements[z]);
	  if(theItem){
	  if(theItem.type){
        if(theItem.type == 'checkbox' && theItem.id != theController.id){
	     theItem.checked = theController.checked;
	    }
	  } else {

	    var nextArray = '';
	     for(var x=0;x <theItem.childNodes.length;x++){
	      if(theItem.childNodes[x]){
	        if (theItem.childNodes[x].id){
	          nextArray += theItem.childNodes[x].id+',';
		    }
	      }
	     }
	     checkUncheckSome(controller,nextArray);
	   
	   }
	  }
 }
}

function changeImgSize(objectId,newWidth,newHeight) {
	  imgString = 'theImg = document.getElementById("'+objectId+'")';
	  eval(imgString);
	  oldWidth = theImg.width;
	  oldHeight = theImg.height;
	  if(newWidth>0){
	   theImg.width = newWidth;
	  } 
	  if(newHeight>0){
	   theImg.height = newHeight;
	  } 	
}
function changeColor(theObj,newColor){
	  eval('var theObject = document.getElementById("'+theObj+'")');
	  if(theObject.style.backgroundColor==null){theBG='white';}else{theBG=theObject.style.backgroundColor;}
	  if(theObject.style.color==null){theColor='black';}else{theColor=theObject.style.color;}
	  //alert(theObject.style.color+' '+theObject.style.backgroundColor);
      switch(theColor){
	    case newColor:
		  switch(theBG){
			case 'white':
		      theObject.style.color = 'black';
		    break;
			case 'black':
			  theObject.style.color = 'white';
			  break;
			default:
			  theObject.style.color = 'black';
			  break;
		  }
		  break;
	    default:
		  theObject.style.color = newColor;
		  break;
	  }
}
// Replaces all instances of the given substring.
String.prototype.replaceAll = function( 
                                        strTarget, // The substring you want to replace
                                        strSubString // The string you want to replace in.
                                        ){
      var strText = this;
      var intIndexOfMatch = strText.indexOf( strTarget );
      // Keep looping while an instance of the target string
      // still exists in the string.
      while (intIndexOfMatch != -1){
      // Relace out the current instance.
      strText = strText.replace( strTarget, strSubString )
      // Get the index of any next matching substring.
      intIndexOfMatch = strText.indexOf( strTarget );
      }
      // Return the updated string with ALL the target strings
      // replaced out with the new substring.
      return( strText );
}
//Adds value and its name to querystring
function AddToQuery(q, valname, val){
      if (q.indexOf('?')<0) {
        q += '?'
      } else {
      var pv = q.indexOf(valname+'=');
      if (pv >= 0){
        var amp = q.indexOf('&', pv);
        if (amp<0) {
        q = q.substr(0, pv) 
        } else {
        q = q.substr(0, pv) + q.substr(amp+1) + '&'
        }
      } else {
        if (q.substr(q.length-1)!='?') q += '&'
      };
      };
      q += valname + '=' + val
      return q
};
function pause(millis){
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); } 
    while(curDate-date < millis);
} 

function hide(id){
  var x = getObj(id);
  myObject.style.visibility=hidden;
}
function getObj(name)
{
  if (document.getElementById){
  	  this.obj = document.getElementById(name);
	    this.style = document.getElementById(name).style;
  }else if (document.all){
	    this.obj = document.all[name];
	    this.style = document.all[name].style;
  }else if (document.layers){
   	  this.obj = document.layers[name];
   	  this.style = document.layers[name];
  }
}

var DHTML = (document.getElementById || document.all || document.layers);
function invisible(what,flag){  
	if (!DHTML) return;
	var x = new getObj(what);
	x.style.visibility = (flag) ? 'hidden' : 'visible'
}
function changeColor(what,color){
	if (!DHTML) return;
	var x = new getObj(what);
	x.style.color = color;
}

function fileIsAvailable(file){
    var x = new getObj(file);
    var bExists = false;
    
    alert(x.readyState);
    
    if(x.readyState == 4) {
        if(x.status == 200)
        {
            // image exists
            bExists=false;
        }
	   else if(x.status==404)
	   {// image doesn't exist
	       bExists=true;
	   }
        else
        {
            // all other error codes will set this
            bExists=null;
        }
    return bExists;
    }
}
