////$Id: functions.js,v 1.4 2005/11/28 21:36:23 ge_allan Exp $////
//added by Allan
function openwindow(url, windowname, width, height) {
	if(arguments.length==4)
		extravariable=arguments[4]  //not used
	newwindow = window.open(url, windowname, "width="+width+",height="+height+",left=150,top=100,scrollbars=yes")
	newwindow.focus();
}

function getele(n, d){
  //source: MM_findObj(n, d) { //v4.01
  //argument 'n' is the name of the object you want to get
  //argument 'd' is the document object, it is not required
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function explodeArray(item,delimiter) {
  //function for explode a string into an array by the given seperator
  tempArray=new Array(1);
  var Count=0;
  var tempString=new String(item);
  while (tempString.indexOf(delimiter)>0) {
    tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
    tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1); 
    Count=Count+1
  }
  tempArray[Count]=tempString;
  return tempArray;
}

function transfer_standard_selection(transfer_from, transfer_to, form_name) {
	if (transfer_from.selectedIndex != 0) {  //first selection in dropdown is not a value
		transfer_value = transfer_from.options[transfer_from.selectedIndex].text;  //take label instead of value
	} else {
		transfer_value = '';
	}
	document.forms[form_name].elements[transfer_to].value = transfer_value;
	if (transfer_value != '') {
		document.forms[form_name].elements[transfer_to].disabled = true;
	} else {
		document.forms[form_name].elements[transfer_to].disabled = false;
	}
}

function set_opener_field(field_in_parent_window, myvalue, add_seperator) {
	/*
	NOTES:
	- copy a value to a field in the parent window
	INPUT:
	- add_seperator : whether or not the existing value should be overwritten or the new value should just be added
		- set to true if value should be added without any seperator
		- write a seperator string if value should be added with a seperator
		- omit parameter (or set to false) to have new value overwrite any current value
	*/
	obj = getele(field_in_parent_window, opener.document);  //get the field
	currvalue = obj.value;  //get current value
	if (currvalue.length > 0 && add_seperator) {
		if (add_seperator === true) add_seperator = '';
		newvalue  = currvalue + add_seperator + myvalue;
	} else {
		newvalue  = myvalue;
	}
	obj.value = newvalue;  //set new value
}

function refresh_parent_window(url) {
	if (url) {
		opener.document.location = url;
	} else {
		opener.document.location.reload();
	}
}

function change_parent_and_close(url) {
	//Change parent window to a certain URL and close this (popup) window
	opener.document.location = url;
	window.close();
}

function confirm_link(url, popup_question) {
	if (!popup_question) {
		popup_question = 'Are you sure?';
	}
	if (confirm(popup_question)) {
		window.location=url;
	}
}

function dropdown_add(obj, thevalue, thelabel, is_selected) {
	next_index = obj.options.length;
	obj.options[next_index] = new Option(thelabel, thevalue, is_selected, is_selected); //it will only be selected if both arguments are set to true!! (but IE 5.0 does not even correctly set the default value!)
	if (is_selected) {  //IE does not correctly display the selected value by setting it when the item is added to the dropdown, therefore also set the selected here (this is at least what I experienced)
		obj.options[next_index].selected = true
	}
	return next_index;  //return the index number of this new option
}

function dropdown_value(select_obj) {
	if (select_obj.options.length > 0) {  //check if the dropdown has any values
		if (typeof select_obj == 'object') {
			myvalue = select_obj.options[select_obj.selectedIndex].value;
			return myvalue;
		} else {
			alert("Error occured in getting value from dropdown. Please contact system administrator.");
		}
	} else {
		return "";
	}
}

function dropdown_value_label(select_obj) {
	if (select_obj.options.length > 0) {  //check if the dropdown has any values
		if (typeof select_obj == 'object') {
			mylabel = select_obj.options[select_obj.selectedIndex].text;
			return mylabel;
		} else {
			alert("Error occured in getting value label from dropdown. Please contact system administrator.");
		}
	} else {
		return "";
	}
}

var boolNS4 = navigator.appVersion.indexOf("Nav") > 0 && parseInt(navigator.appVersion) == 4;
function showhide_text(elementname) {
  //if(boolNS4) return false;	
   objCurrent = getele(elementname);
   objCurrent.style.display = (objCurrent.style.display=="none" ? "block" : "none");
}

function get_cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function set_cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function delete_cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function get_text(object) {
	// Get the text/HTML between for an object on the page
	return getele(object).innerHTML;
}

function set_text(object, text, append) {
	// Set the text/HTML between for an object on the page
	if (!append) {
		getele(object).innerHTML = text;
	} else {
		theobject = getele(object);
		theobject.innerHTML = theobject.innerHTML + text;
	}
}

function pause(milliseconds) {
	// Pause Javascript for a while (without eating CPU)
	// Source: http://www.faqts.com/knowledge_base/view.phtml/aid/1602
	var dialogScript = 
		'window.setTimeout(' +
		' function () { window.close(); }, ' + milliseconds + ');';
	var result = 
	// For IE5+
	window.showModalDialog(
		'javascript:document.writeln(' +
		'"<script>' + dialogScript + '<' + '/script>")');
	/* For NN6, but it requires a trusted script.
	openDialog(
		'javascript:document.writeln(' +
		'"<script>' + dialogScript + '<' + '/script>"',
		'pauseDialog', 'modal=1,width=10,height=10');
	*/
}