/*----------------------
  Open_PopUp('home.htm','455','271','1');
  args: URL,X-dimension,Y-Dimension,scrolling(1,0)
----------------------*/

function Open_Window(page,x,y,scroll){
    scroll = (isNaN(scroll)) ? 0:scroll;
	x = (isNaN(x))? 480:x;
	y = (isNaN(y))? 288:y;
	Open_Window = window.open(page,'w2','width=' + x + ',height=' + y + ',toolbar=0,menubar=0,resizable=no,status=0,scrollbars=' + scroll + ',left=100,top=50');
	setTimeout("Open_Window.focus()",200);
}

var popUpWin
function Open_PopUp(page,x,y,scroll){
        scroll = (isNaN(scroll)) ? 0:scroll;
	x = (isNaN(x))? 480:x;
	y = (isNaN(y))? 288:y;
	if(popUpWin!=true){popUpWin = window.open(page,'w2','width=' + x + ',height=' + y + ',toolbar=0,menubar=0,resizable=no,status=0,scrollbars=' + scroll + ',left=100,top=50');}
	setTimeout("popUpWin.focus()",200);
}

function checkTop() {
  if(window.parent != self) {
    window.top.location = self.location;
  }
}

function getRadioValue(formName,elementName) {
	radioArray = eval("document." + formName + "." + elementName );
	for(i=0; i<radioArray.length; i++) {
		if(radioArray[i].checked) {
			return radioArray[i].value
		}
	}
	return "";
}

function getSelectValue(formName,elementName) {
	selectObj = eval("document." + formName + "." + elementName );
	i = selectObj.selectedIndex
	return selectObj.options[i].value
}

// checkMaxSelected
// takes a select element object as the first argument and the maximum 
// number of options to be selected as the second argument. The function 
// is to be called in the onchange handler and then fires an alert if the 
// maximum number of selections is exceeded and discards that selection.

function checkMaxSelected (select, maxSelected) {
  if (!select.storeSelections) {
    select.storeSelections = new Array(select.options.length);
    select.selectedOptions = 0;
  }
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i].selected && 
        !select.storeSelections[i]) {
      if (select.selectedOptions < maxSelected) {
        select.storeSelections[i] = true;
        select.selectedOptions++;
      }
      else {
        alert('Selection will be discarded as limit of ' + maxSelected + ' is reached.');
        select.options[i].selected = false;
      }
    }
    else if (!select.options[i].selected &&
             select.storeSelections[i]) {
      select.storeSelections[i] = false;
      select.selectedOptions--;
    }
  }
} 


//-----------------------------------------------------------------------------------
// Form Validation routines.
// In you page create an array of validObj. One for each form element you want to validate.
// Currently all arguments are required
//

function validate(validArray) {
  var elemType;
  //alert("two")
  for(i=0;i<validArray.length;i++) {
    var elem = eval("document." + validArray[i].formName + "." + validArray[i].elementName);
    var elemType = elem.type;
    //alert(elemType + "'" + elem.value + "'");

    if (elemType == "text" || elemType == "textarea") {
      if(elem.value == validArray[i].notEqualVal) {
        alert("You must enter a value for '" + validArray[i].niceName + "'.");
        elem.focus();
        return false;
      }
    }

    if (elemType == "select-one") {
      if(getSelectValue(validArray[i].formName,validArray[i].elementName) == validArray[i].notEqualVal) {
        alert("You must enter a value for '" + validArray[i].niceName + "'.");
        elem.focus();
        return false;
      }
    }

  }
  return true;
}

function validObj(formName,elementName,niceName,notEqualVal) {
  this.formName = formName;
  this.elementName = elementName;
  this.niceName = niceName;
  this.notEqualVal = notEqualVal;
}

//-----------------------------------------------------------------------------------
// Form Submission for on_keydown event
// called with: onkeydown=" return on_keydown(event);" in the text form element.
// use "return" so that the return keydown doesn't auto submit the form.
// This calls the doSubmit() function of the particular page.
//-----------------------------------------------------------------------------------

function on_keydown(formInfo,e,action)
{
	var keyCode = document.layers ? e.which : e.keyCode;
	if (keyCode == 13)
	{
		validatelogin (formInfo);
		return false;
	}

	return true;
}


function replaceStr(sourceStr, searchStr, replaceStr) {
  while (sourceStr.indexOf(searchStr)!= -1){
    firstStr = sourceStr.slice(0,sourceStr.indexOf(searchStr));
    //alert("firstStr=" + firstStr + "!");
    secondStr = sourceStr.slice(firstStr.length + searchStr.length, sourceStr.length);
    //alert("secondStr=" + secondStr + "!");
    sourceStr = firstStr + replaceStr + secondStr;
  }
    return sourceStr;
}


// BrowserCheck Object
function BrowserCheck() {
	var b = navigator.appName

	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)

	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.ie6 = (this.version.indexOf('MSIE 6')>0)
	this.min = (this.ns||this.ie)
}

is = new BrowserCheck()

function hideObject(id) {
  if (is.ns4) {
     eval ("document." + id + ".visibility = 'hide'");
  }
  else if (is.ie4 || is.ie5 || is.ie6 ) {
     document.all[id].style.visibility = "hidden";
  }
  else if (is.ns5 || is.ns6) {
     document.getElementById(id).style.visibility = "hidden";
  }
}


function showObject(id) {

  if (is.ns4) {

     eval ("document." + id + ".visibility = 'show'");
  }
  else if (is.ie4 || is.ie5 || is.ie6 ) {

     document.all[id].style.visibility = "visible";
  }
   else if (is.ns5 || is.ns6) {

     document.getElementById(id).style.visibility = "visible";
  }
}



//AJAX stuff

var request;

function createRequestObject(){
	try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
	
/*	var request_;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
 		request_ = new ActiveXObject("Microsoft.XMLHTTP");
	} else{
 		request_ = new XMLHttpRequest();
	}
	return request_; */
}

var http = createRequestObject();

function getInfo(strUrl){
	http.open('get', strUrl);
	http.onreadystatechange = handleInfo;
	http.send(null);
}

function handleInfo(){
	if(http.readyState == 1){
 		//document.getElementById(updatedDiv).innerHTML = 'Loading...';
	}
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById(updatedDiv).innerHTML = response;
	}
}

function TableRowDisplay(bShow, id)
{
  id = xGetElementById(id);
  id.style.display = bShow ? '' : 'none';
}

function xGetElementById(e)
{
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function WriteLayer(ID,parentID,sText) {
	if (document.layers) {
   		var oLayer;
   		if(parentID){
     		oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
   		}else{
     		oLayer = document.layers[ID].document;
   		}
   		oLayer.open();
   		oLayer.write(sText);
   		oLayer.close();
 	} else if (parseInt(navigator.appVersion)>=5 && navigator.appName=="Netscape") {
   		document.getElementById(ID).innerHTML = sText;
 	} else if (document.all)  {
		document.all[ID].innerHTML = sText
	}
} 
function ReadLayer(ID,parentID) {
	returnValue = "";
	if (document.layers) {
   		var oLayer;
   		if(parentID){
     		oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
   		}else{
     		oLayer = document.layers[ID].document;
   		}
   		oLayer.open();
   		//oLayer.write(sText);
		returnValue = oLayer.text;
   		oLayer.close();
 	} else if (parseInt(navigator.appVersion)>=5&&navigator.appName=="Netscape") {
   		//document.getElementById(ID).innerHTML = sText;
		returnValue = document.getElementById(ID).innerHTML
 	} else if (document.all) 
		returnValue = document.all[ID].innerHTML
		//document.all[ID].innerHTML = sText
	return returnValue;
} 



