/* $Id: SCRIPTtwd.js 358 2007-10-01 21:47:54Z timw $ */

/**
 * Javascript -> Main Functions used in APP_BASE
 *
 * @author $Author: timw $
 * @version $Rev: 358 $ $Date: 2007-10-02 10:47:54 +1300 (Tue, 02 Oct 2007) $
 * @url $HeadURL: http://elibcsystems.dyndns.org/svn/skink/tags/1.5.0/scripts/SCRIPTtwd.js $
 * @package appbase
 */

/**
 * TWD_JS_findObj()
 *
 * Finds an unique element in the page
 *
 * @version 1.00.0
 * @param string ELEMENT_NAME
 * @param string DOCUMENT
 * @return dom NODE
 */
function TWD_JS_findObj(n, d) {
  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=TWD_JS_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
/**
 * TWD_JS_showHideObjects()
 *
 * Finds an unique element in the page and changes the style display attribute thus hiding the element from view
 *
 * @version 2.00.0
 * @param string ELEMENT_NAME
 * @param string STYLE_DISPLAY_OPTION
 * @return bool
 */
function TWD_JS_showHideObjects() {
  var i,p,v,obj,args=TWD_JS_showHideObjects.arguments;
  for (i=0; i<(args.length-1); i+=2) if ((obj=TWD_JS_findObj(args[i]))!=null) { v=args[i+1]; 
    if (obj.style) { obj=obj.style; }
    obj.display=v;}
    return true;
}
/**
 * TWD_JS_confirmAndGo()
 *
 * Popup a javasript confirm window then either go to link or execute a javasript command
 *
 * @version 2.01.0
 * @param string CONFIRM_MESSAGE
 * @param string URL_OR_JAVASCRIPT
 * @return bool
 */
function TWD_JS_confirmAndGo(theMessage, theURL, theTarget) {
  theTarget = (theTarget) ? theTarget : 'self';
	if (confirm(theMessage)) {
	  if (theURL.substring(0,11).toLowerCase() == 'javascript:') {
	    eval(theURL.substring(11,theURL.length));
	    return true;
	  } else { 
    	eval(theTarget + ".location='" + theURL + "'")
    	return true;
	  }
	} else {
		return false;
	}
}
/**
 * TWD_JS_goToURL()
 *
 * Go to go to a location
 *
 * @version 20070921
 * @return bool
 */
function TWD_JS_goToURL() {
  var i, args=TWD_JS_goToURL.arguments; document.TWD_JS_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(((args[i+1]) ? args[i+1] : 'self') +".location='"+args[i]+"'");
}
/**
 * TWD_JS_openWindow()
 *
 * Open a popup window 
 *
 * @version 20070921
 * @param string URL
 * @param string WINDOW_NAME
 * @param string WINDOW_FEATURES
 * @return bool
 */
function TWD_JS_openWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}
/**
 * TWD_JS_form_menu_populate_menu()
 *
 * Transfer selected options from one menu to another
 *
 * @version 20070629
 * @param string FROM_MENU
 * @param string TO_MENU
 * @return bool
 */
function TWD_JS_form_menu_populate_menu(fbox, tbox,sortFlag) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		} else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}
	if (sortFlag) {
		arrTbox.sort();
	}
	fbox.length = 0;
	tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}
	for(c = 0; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
	}
}
/**
 * TWD_JS_form_menu_insert_option()
 *
 * Insert the option into the menu
 *
 * @version 20070629
 * @param string MENU
 * @param string OPTION_VALUE
 * @param string OPTION_HTML
 * @return bool
 */
function TWD_JS_form_menu_insert_option(theMenu, optionValue, optionHTML) {
	var no = document.createElement('option');
		no.value = optionValue;
		no.text = optionHTML;
	theMenu.appendChild(no);
}
/**
 * TWD_JS_array_search()
 *
 * Search the array for needle
 *
 * @version 20070727
 * @param string NEEDLE
 * @param array SEARCH_ARRAY
 * @return bool
 */
function TWD_JS_array_search(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
	  if (haystack[i] === needle) {
	    return true;
	  }
	}
	return false;
}
/**
 * TWD_JS_array_search()
 *
 * Search the array for needle
 *
 * @version 20071001
 */
function TWD_JS_validateForm() {
  var i,p,q,nm,test,num,min,max,errors='',args=TWD_JS_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=TWD_JS_findObj(args[i]); nm=args[i+1];
    if (val) { if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must be an email address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must be a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must be a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following errors occured:\n'+errors);
  document.TWD_JS_returnValue = (errors == '');
}




















































function TWD_JS_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//TWDform_dropmenu_remote_reload(theSplash, theForm, theDropmenuselector, theRemotevalue)
//LAST MODIFIED: 09-11-03
function TWDform_dropmenu_remote_reload(theSplash, theForm, theDropmenuselector, theRemotevalue) { 
	//Variables
	var DIVreloading = MM_findObj(theSplash, opener.document);
	var FORMname = MM_findObj(theForm, opener.document);
	var FORMid = MM_findObj('FORMid', opener.document);
	var FORMselected = MM_findObj(theDropmenuselector, opener.document);
	//Do reload
	FORMid.value = "reloading";
	FORMselected.value = theRemotevalue;
	FORMname.submit();
	DIVreloading.style.visibility = "visible";
	opener.focus();
	self.close();
}
//TWDform_focusvalue
function TWDform_focusvalue(textbox, currentValue, setValue) {
	var textbox = ((typeof(textbox) != 'string') ? textbox : TWD_JS_findObj(textbox));
	if (textbox.value == setValue) {
		textbox.value = currentValue;
	} else if (textbox.value == currentValue) {
		textbox.value = setValue;
	}
}

function TWD_showHideObjects() { //v1.0
  alert('invalid function: TWD_showHideObjects');
}
//TWDform_selectall
//Creation Date: 24-06-04
//Last Modified: 25-06-04
var checkflag = "false";
function TWD_JS_form_selectall(fieldName) {
	if (checkflag == "false") {
		for (i = 0; i < fieldName.length; i++) {
			fieldName[i].checked = true;
		}
		checkflag = "true";
		//return "Uncheck All"; 
	} else {
		for (i = 0; i < fieldName.length; i++) {
			fieldName[i].checked = false; 
		}
		checkflag = "false";
		//return "Check All"; 
	}
}

//TWDtoggledisplay();
//Creation Date: 05-10-04
//Last Modified: 05-10-04
function TWDtoggledisplay(targetId){
	ie4 = (document.all) ? true : false;
	ns6 = (! document.all && document.getElementById) ? true : false;
	if (ie4) {
		target = document.all[targetId];
		if (target.style.display == "none"){
			//document.all["interacting"].style.display = "none";
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	} else if (ns6) {
		target = document.getElementById(targetId);
		if (target.style.display == "none"){
			//document.getElementById("interacting").style.display = "none";
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}
}
function TWD_JS_number_format(a, b, c, d) { 
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}
