/* $Id: SCRIPTtwd.js 162 2006-12-07 00:49:56Z timw $ */

/**
 * Javascript -> Main Functions used in APP_BASE
 *
 * @author $Author: timw $
 * @version $Rev: 162 $ $Date: 2006-12-07 13:49:56 +1300 (Thu, 07 Dec 2006) $
 * @url $HeadURL: file:///var/lib/svn/malaqerevillas.com/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) {
	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;
	}
}































































function TWD_JS_validateForm() { //v4.0
  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
  var i,p,v,obj,args=TWD_showHideObjects.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=document.getElementById(args[i]))!=null) { v=args[i+2]; //if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; }
    obj.display=v; }
}
//TWDform_selectall
//Creation Date: 24-06-04
//Last Modified: 25-06-04
var checkflag = "false";
function TWDform_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_openWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}










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];
}