/*
* updated 2/9/2007 abosch - added function validate_date2 - has input_format as a parameter
* updated 2/12/2007 abosch - added function validate_datetime2 - has input_format as a parameter
* updated 3/7/2007 abosch - fixed date error with month
* updated 3/23/2007 abosch - added function copyToBuffer
* updated 09/08/2007 (pwexler) removed "ALERT" statement which was used for troubleshooting
* updated 4/23/2008 fix validname function bug by george
*/
function valid(ctrl) {
	strtest=ctrl.value;
	ctr=0; warn= "0"; per=""; out ="";
	for (u=0;u<strtest.length;u++) {
		idx=strtest.substr(u,1);
		if ( idx == "-" && out.length == 0 ) { out=out + idx; }
		else if (  idx <= "9"  && idx >= "0" ) { out=out + idx; }
		else if ( idx == "." && per != "done" ) { out=out  + idx; per="done"; }
		else { warn="1"; }
		}
	if ( warn == "1" ) { alert("The number entered has invalid characters.\nIt will be changed to " + out); }
	ctrl.value=out;
	return ;
	}

function validname(ctrl) {
	strtest=ctrl.value;
	ctr=0; warn= "0"; per=""; out ="";
	for (u=0;u<strtest.length;u++) {
		idx=strtest.substr(u,1);
		if ( idx == "-"  ) { out=out + idx; }
		else if ( idx == "="  ) { out=out + idx; }
		else if ( idx == "+"  ) { out=out + idx; }
		else if ( idx == "."  ) { out=out + idx; }
		else if ( idx == ","  ) { out=out + idx; }
		else if ( idx == "$"  ) { out=out + idx; }
		else if ( idx == "&"  ) { out=out + idx; }
		else if ( idx == "*"  ) { out=out + idx; }
		else if ( idx == "/"  ) { out=out + idx; }
		else if ( idx == "_"  ) { out=out + idx; }
		else if ( idx == "("  ) { out=out + idx; }
		else if ( idx == ")"  ) { out=out + idx; }
		else if ( idx == "#"  ) { out=out + idx; }
		else if ( idx == "@"  ) { out=out + idx; }
		else if ( idx == "!"  ) { out=out + idx; }
		else if ( idx == ":"  ) { out=out + idx; }
		else if ( idx == " "  ) { out=out + idx; }
		else if ( idx == "<"  ) { out=out + idx; }
		else if ( idx == ">"  ) { out=out + idx; }
		else if (  idx <= "9"  && idx >= "0" ) { out=out + idx; }
		else if (  idx <= "Z"  && idx >= "A" ) { out=out + idx; }
		else if (  idx <= "z"  && idx >= "a" ) { out=out + idx; }
		else { warn="1"; }
		}
	if ( warn == "1" ) { alert("The string entered has invalid characters.\nIt will be changed to " + out); }
	ctrl.value=out;
	return ;
	}

function valid_int_nosign(ctrl) {
	strtest=ctrl.value;
	ctr=0; warn= "0"; per=""; out ="";
	for (u=0;u<strtest.length;u++) {
		idx=strtest.substr(u,1);
		if (  idx <= "9"  && idx >= "0" ) { out=out + idx; }
		else { warn="1"; }
		}
	if ( warn == "1" ) { alert("The number entered has invalid characters.\nIt will be changed to " + out); }
	ctrl.value=out;
	return ;
	}
function valid_choice(ctrl) {
	strtest=ctrl.value;
	ctr=0; warn= "0"; per=""; out ="";
	for (u=0;u<strtest.length;u++) {
		idx=strtest.substr(u,1);
		if ( ( idx >= "a"  && idx <= "z" ) || ( idx >= "A"  && idx <= "Z" ) || ( idx <= "9"  && idx >= "0" ) || $idx == "/" || $idx == "+"  || $idx == "."  || $idx == "," ) { out=out + idx; }
		else { warn="1"; }
		}
	if ( warn == "1" ) { alert("The choice entered has invalid characters.\nIt will be changed to " + out); }
	ctrl.value=out;
	return ;
	}
function valid_datetime(inp) {
	var err = "" ;
	var input_date = inp.value ;
	if ( input_date.length > 0 ) {
		// break the string into two parts based upon a blank in the middle
		list = explode(input_date," ") ;
		if ( list[0].length < 1 ) { alert("No date. Please fix.") ; return false; }
		if ( list[1]==null ) { alert("No time. Please fix.") ; return false; }
		if ( list[1].length < 1 ) { alert("No time. Please fix.") ; return false; }
		//check the date, they put something in
		if ( list[0].indexOf("/") > 0 ) { listd=explode(list[0],"/",false) ; }
		if ( list[0].indexOf("-") > 0 ) { listd=explode(list[0],"-",false) ; }
		if ( list[0].indexOf(".") > 0 ) { listd=explode(list[0],".",false) ; }
		if ( list[0].indexOf(",") > 0 ) { listd=explode(list[0],",",false) ; }
		var yy = listd[2] ; var dy = listd[1] ; var mo = listd[0] ;
		cent = Math.round(cur_date.getFullYear() /100) ;
		if ( yy < 20  ) { yy =Math.round(yy*100)/100 + Math.round(cent*100) ; }
		if ( yy >= 20 && yy < 100 ) { yy =Math.round(yy*100)/100 + Math.round(cent*100)-100 ; }
		var test = new Date(yy,mo-1,dy);
		if (  yy == test.getFullYear() && mo-1 == test.getMonth() && dy == test.getDate() ) {
			}
		      else {
			err += "Valid format but an invalid date\n";
			}
		listt=explode(list[1],":",false) ;
		var hh = listt[0] ; var mm = listt[1] ; var ss = listt[2] ;
		if (hh==null ) { hh = 0 ; alert("No time. Please fix.") ; return false; }
		if (ss==null ) { ss = 0 ; }
		if (mm==null ) { mm = 0 ; }
		if ( hh<0 || hh > 23 ) { err +="bad hours\n"; }
		if ( mm < 0  || mm > 59 ) { err +="bad minutes\n"; }
		if (  ss > 59 || ss < 0 ) { err +="bad seconds\n"; }

		if ( err.length < 1  ) {
			// prep for output
			inp.value= formatNumber(mo,"00")+"/"+formatNumber(dy,"00")+"/"+formatNumber(year,"0000")+" "+formatNumber(hh,"00")+":"+formatNumber(mm,"00")+":"+formatNumber(ss,"00") ;
			return true ;
			}
		    else {
			alert(err+"/nProper format is 'mm/dd/yyyy hh:mm:ss'") ;
			return false ;
			}
		}
	}

function validate_date( input_date )  {
	inp =input_date.value ;
	if ( inp.charAt(0) =="+" || inp.charAt(0) == "-" ) {
		op = inp.charAt(0) ;
		//decode number part
		gg=inp.substr(1) ;
		ii =  parseInt(gg) ;
		if ( gg.length < 1 ) {
			alert ( " invalid offset, date blanked" ) ;
			input_date.value = "" ;
			return false;
			}
		if ( inp.charAt(0) == "-" ) { ii = 0 - ii ; }
		var myDate = new Date();
		dat = new Date(myDate.getTime() + ii*24*60*60*1000) ;
		var d  = dat.getDate();
		var day = (d < 10) ? '0' + d : d;
		var m = dat.getMonth() + 1;
		var month = (m < 10) ? '0' + m : m;
		var yy = dat.getYear();
		//var year = (yy < 1000) ? yy + 2000 : yy;
		var year = (yy < 1000) ? yy + 1900 : yy;
		input_date.value = month+"/"+day+"/"+year ;
		return true ;
		}
	if ( inp.length < 5 && inp.length > 0 ) {
		alert ("You have a badly formatted date!");
		input_date.value = "" ;
		return false ;
		}
	if ( inp.length > 0 ) {
		//check the date, they put something in
		// find separator, then explode on it
		if ( inp.indexOf("/") > 0 ) { list=explode(inp,"/",false) ; }
		if ( inp.indexOf(".") > 0 ) { list=explode(inp,".",false) ; }
		if ( inp.indexOf(",") > 0 ) { list=explode(inp,",",false) ; }
		if ( inp.indexOf("-") > 0 ) { list=explode(inp,"-",false) ; }
		var yy = list[2] ; var dy = list[1] ; var mo = list[0] ;
		cur_date=new Date() ;
		cent = Math.round(cur_date.getFullYear() /100) ;
		if ( yy < 20  ) { yy =Math.round(yy*100)/100 + Math.round(cent*100) ; }
		if ( yy >= 20 && yy < 100 ) { yy =Math.round(yy*100)/100 + Math.round(cent*100)-100 ; }
		var aa = (Math.round(cent*100)/100)-100 ;
		var test = new Date(yy,mo-1,dy);
		if ( yy == test.getFullYear() && mo-1 == test.getMonth() && dy == test.getDate() ) {
			input_date.value = formatNumber(mo,"00")+"/"+formatNumber(dy,"00")+"/"+yy ;
			return true ;
		}
		else {
			alert ("valid format but an invalid date");
			input_date.value = "" ;
		    return false;
		}
	}
}

////////////////////////////////////
// function: parseSimpleDate(d,input_format)
// ORIG: abosch 2/7/2007
function parseSimpleDate(d,input_format)
{
	//var input_format = '%d-%m-%Y';
	var myformat = input_format.replace(/[- \.]/g, '/'); 
	//alert(myformat);
	myformat.replace('/y/g', 'Y'); 
	
	var mydate = d.replace(/[- \.]/g, '/'); 
	var a = d.split("/");

	if (myformat == '%d/%m/%Y')
		 var newdate= new Date(a[2],a[1]-1,a[0]);
	else if (myformat == '%d/%Y/%m')
		 var newdate= new Date(a[1],a[2]-1,a[0]);
	else if (myformat == '%m/%d/%Y')
		 var newdate= new Date(a[2],a[0]-1,a[1]);
	else if (myformat == '%m/%Y/%d')
		 var newdate= new Date(a[1],a[0]-1,a[2]);
	else if (myformat == '%Y/%m/%d')
		 var newdate= new Date(a[0],a[1]-1,a[2]);
	else if (myformat == '%Y/%d/%m')
		 var newdate= new Date(a[0],a[2]-1,a[1]);
	else {
		 var newdate= new Date();
		 newdate.setTime(Date.parse(d));
	}
	//return newdate.format('%m/%d/%Y');
	//return (newdate.getMonth()+1)+'/'+newdate.getDate()+'/'+newdate.getFullYear();
	return newdate;
}

/////////////////////////////////////////////////////////////////
// function: validate_date2( input_date, input_format )  
// ORIG: abosch 2/7/2007
function validate_date2( input_date, input_format )  
{
	var myformat = input_format.replace(/[-\.,]/g, '/'); 

	inp =input_date.value ;


	if ( inp.charAt(0) =="+" || inp.charAt(0) == "-" ) 
	{
		op = inp.charAt(0) ;
		//decode number part
		gg=inp.substr(1) ;
		ii =  parseInt(gg) ;
		if ( gg.length < 1 ) {
			alert ( " invalid offset, date blanked" ) ;
			input_date.value = "" ;
			return false;
		}
		if ( inp.charAt(0) == "-" ) { ii = 0 - ii ; }
		var myDate = new Date();
		dat = new Date(myDate.getTime() + ii*24*60*60*1000) ;
		/*
		var d  = dat.getDate();
		var day = (d < 10) ? '0' + d : d;
		var m = dat.getMonth() + 1;
		var month = (m < 10) ? '0' + m : m;
		var yy = dat.getYear();
		//var year = (yy < 1000) ? yy + 2000 : yy;
		var year = (yy < 1000) ? yy + 1900 : yy;
		input_date.value = month+"/"+day+"/"+year ;
		*/
		input_date.value = dat.format(input_format);
		return true ;
	}
	if ( inp.length < 5 && inp.length > 0 ) {
		alert ("You have a badly formatted date!");
		input_date.value = "" ;
		return false ;
	}
	if ( inp.length > 0 ) 
	{
		//check the date, they put something in
		// find separator, then explode on it
		if ( inp.indexOf("/") > 0 ) { list=explode(inp,"/",false) ; }
		if ( inp.indexOf(".") > 0 ) { list=explode(inp,".",false) ; }
		if ( inp.indexOf(",") > 0 ) { list=explode(inp,",",false) ; }
		if ( inp.indexOf("-") > 0 ) { list=explode(inp,"-",false) ; }

		if (myformat == '%d/%m/%Y')			{ yy = list[2]; mo = list[1]; dy = list[0]; }
		else if (myformat == '%d/%Y/%m')	{ yy = list[1]; mo = list[2]; dy = list[0]; }
		else if (myformat == '%m/%d/%Y')	{ yy = list[2]; mo = list[0]; dy = list[1]; }
		else if (myformat == '%m/%Y/%d')	{ yy = list[1]; mo = list[0]; dy = list[2]; }
		else if (myformat == '%Y/%m/%d')	{ yy = list[0]; mo = list[1]; dy = list[2]; }
		else if (myformat == '%Y/%d/%m')	{ yy = list[0]; mo = list[2]; dy = list[1]; }
		else {
			var mydate= new Date();
			mydate.setTime(Date.parse(input_date));
			yy = mydate.getFullYear(); mo = mydate.getMonth()+1; dy = mydate.getDate();
		}

		var newdate= new Date(yy,mo-1,dy);
		//var yy = list[2] ; var dy = list[1] ; var mo = list[0] ;

		cur_date=new Date() ;
		cent = Math.round(cur_date.getFullYear() /100) ;
		if ( yy < 20  ) { yy =Math.round(yy*100)/100 + Math.round(cent*100) ; }
		if ( yy >= 20 && yy < 100 ) { yy =Math.round(yy*100)/100 + Math.round(cent*100)-100 ; }
		var aa = (Math.round(cent*100)/100)-100 ;
		var test = new Date(yy,mo-1,dy);

		if ( yy == test.getFullYear() && mo-1 == test.getMonth() && dy == test.getDate() ) {
			
			//input_date.value = formatNumber(mo,"00")+"/"+formatNumber(dy,"00")+"/"+yy ;
			//alert ("Date is valid :)");
			input_date.value =  newdate.format(input_format);
			return true ;
		}
		else {
			alert ("valid format but an invalid date");
			input_date.value = "" ;
		    return false;
		}
	}
}	// END: function validate_date2( input_date, input_format )  


/////////////////////////////////////////////////////////////////
// function: validate_datetime2( input_date, input_format )  
// ORIG: abosch 2/12/2007
function validate_datetime2( input_date, input_format )  
{
	var err = "";
	var myformat = input_format.replace(/[-\.,]/g, '/'); 

	inp_datetime = input_date.value ;
	
	if ( inp_datetime.length <= 0 )  alert("No date. Please fix.") ; return false; 
	// break the string into two parts based upon a blank in the middle
	dt_list = explode(inp_datetime," ") ;
	if ( dt_list[0].length < 1 ) { alert("No date. Please fix.") ; return false; }
	if ( dt_list[1]==null ) { alert("No time. Please fix.") ; return false; }
	if ( dt_list[1].length < 1 ) { alert("No time. Please fix.") ; return false; }
	
	inp = dt_list[0];		// date
	imp_time = dt_list[1];	// time
	
	if ( inp.charAt(0) =="+" || inp.charAt(0) == "-" ) 
	{
		op = inp.charAt(0) ;
		//decode number part
		gg=inp.substr(1) ;
		ii =  parseInt(gg) ;
		if ( gg.length < 1 ) {
			alert ( " invalid offset, date blanked" ) ;
			input_date.value = "" ;
			return false;
		}
		if ( inp.charAt(0) == "-" ) { ii = 0 - ii ; }
		var myDate = new Date();
		dat = new Date(myDate.getTime() + ii*24*60*60*1000) ;
		/*
		var d  = dat.getDate();
		var day = (d < 10) ? '0' + d : d;
		var m = dat.getMonth() + 1;
		var month = (m < 10) ? '0' + m : m;
		var yy = dat.getYear();
		//var year = (yy < 1000) ? yy + 2000 : yy;
		var year = (yy < 1000) ? yy + 1900 : yy;
		input_date.value = month+"/"+day+"/"+year ;
		*/
		input_date.value = dat.format(input_format)+" 00:00:00";
		return true ;
	}
	if ( inp.length < 5 && inp.length > 0 ) {
		alert ("You have a badly formatted date!");
		input_date.value = "" ;
		return false ;
	}
	
	listt=explode(imp_time,":",false) ;
	var hh = listt[0] ; var mm = listt[1] ; var ss = listt[2] ;
	if (hh==null ) { hh = 0 ; alert("No time. Please fix.") ; return false; }
	if (ss==null ) { ss = 0 ; }
	if (mm==null ) { mm = 0 ; }
	if ( hh<0 || hh > 23 ) { err +="bad hours\n"; }
	if ( mm < 0  || mm > 59 ) { err +="bad minutes\n"; }
	if (  ss > 59 || ss < 0 ) { err +="bad seconds\n"; }
	if (err.length > 0) {alert(err); return false;}
	
	
	//check the date, they put something in
	// find separator, then explode on it
	if ( inp.indexOf("/") > 0 ) { list=explode(inp,"/",false) ; }
	if ( inp.indexOf(".") > 0 ) { list=explode(inp,".",false) ; }
	if ( inp.indexOf(",") > 0 ) { list=explode(inp,",",false) ; }
	if ( inp.indexOf("-") > 0 ) { list=explode(inp,"-",false) ; }

	if (myformat == '%d/%m/%Y')			{ yy = list[2]; mo = list[1]; dy = list[0]; }
	else if (myformat == '%d/%Y/%m')	{ yy = list[1]; mo = list[2]; dy = list[0]; }
	else if (myformat == '%m/%d/%Y')	{ yy = list[2]; mo = list[0]; dy = list[1]; }
	else if (myformat == '%m/%Y/%d')	{ yy = list[1]; mo = list[0]; dy = list[2]; }
	else if (myformat == '%Y/%m/%d')	{ yy = list[0]; mo = list[1]; dy = list[2]; }
	else if (myformat == '%Y/%d/%m')	{ yy = list[0]; mo = list[2]; dy = list[1]; }
	else {
		var mydate= new Date();
		mydate.setTime(Date.parse(input_date));
		yy = mydate.getFullYear(); mo = mydate.getMonth()+1; dy = mydate.getDate();
	}

	var newdate= new Date(yy,mo-1,dy);
	//var yy = list[2] ; var dy = list[1] ; var mo = list[0] ;

	cur_date=new Date() ;
	cent = Math.round(cur_date.getFullYear() /100) ;
	if ( yy < 20  ) { yy =Math.round(yy*100)/100 + Math.round(cent*100) ; }
	if ( yy >= 20 && yy < 100 ) { yy =Math.round(yy*100)/100 + Math.round(cent*100)-100 ; }
	var aa = (Math.round(cent*100)/100)-100 ;
	var test = new Date(yy,mo-1,dy);
	if ( yy == test.getFullYear() && mo-1 == test.getMonth() && dy == test.getDate() ) {
		
		//input_date.value = formatNumber(mo,"00")+"/"+formatNumber(dy,"00")+"/"+yy ;
		//alert ("Date is valid :)");
		input_date.value =  newdate.format(input_format)+" "+formatNumber(hh,"00")+":"+formatNumber(mm,"00")+":"+formatNumber(ss,"00");
		return true ;
	}
	else {
		alert ("valid format but an invalid date");
		//input_date.value = "" ;
		return false;
	}
	
}	// END: function validate_date2( input_date, input_format )  








//document.form1.sdate
//document.form1.edate
//document.form1.doc_date
//document.form1.gl_date
//document.form1.due_date

function explode(inputstring, separators, includeEmpties) {
	inputstring = new String(inputstring);
	separators = new String(separators);
	if(separators == "undefined") {
		separators = " :;";
		}

	fixedExplode = new Array(1);
	currentElement = "";
	count = 0;

	for(x=0; x < inputstring.length; x++) {
		chard = inputstring.charAt(x);
		if(separators.indexOf(chard) != -1) {
			if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) { }
			          else {
				fixedExplode[count] = currentElement;
				count++;
				currentElement = "";
				}
			 }
		       else { currentElement += chard; }
		}

	if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
		fixedExplode[count] = currentElement;
		}
	return fixedExplode;
	}

function format_number(pnumber,decimals) {
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};

	var IsNegative=(parseInt(pnumber)<0);
	if(IsNegative)pnumber=-pnumber;

	var snum=new String(pnumber);
	var sec=snum.split('.');
	var whole=parseInt(sec[0]);
	var result='';
	if(sec.length > 1){  var dec=new String(sec[1]);  }
	     else { var dec = "" ; }
	var last = parseInt(decimals)+parseInt(1) ;
	while ( dec.length < last ) { dec = dec + "0" ; }
	var out = "" ;
	var carry = 0 ; var tt ;
	if(parseInt(dec.charAt(decimals)) > 4 ) { carry = 1; }
	for ( k=parseInt(decimals)-1; k> -1; k-- ) {
		if ( carry > 1 ) {
			if(parseInt(dec.charAt(k)) > 8 ) {
				out = "0" + out ;
				carry = 1 ;
				}
			        else {
				tt = parseInt(dec.charAt(k)) + parseInt(1)  ;
				out = tt + out ;
				carry = 0 ;
				}
			}
		       else {
			carry = 0 ;
			out = dec.charAt(k) + out ;
			}
		}
	if ( carry > 0 ) { whole++ ; }
	out = whole+"."+out ;
	if(IsNegative) { out ="-"+out ; }
	return out ;

	}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i=getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

 function validdigs(ctrl) {
	strtest=ctrl.value;
	var out="";
	var idx;
	warn=0 ;
	for (var u=0;u<strtest.length;u++) {
		idx=strtest.substr(u,1);
		if (  idx <= "9"  && idx >= "0" ) { out=out + idx; } else { warn=1 ;}
		}
	if ( warn == "1" ) { alert("The number entered has invalid characters.\nIt will be changed to " + out); }
	ctrl.value=out;
	return ;
	}
function URLEncode(ctl )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = ctl.value;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

  var separator = ",";  // use comma as 000's separator
  var decpoint = ".";  // use period as decimal point
  var percent = "%";
  var currency = "$";  // use dollar sign for currency


function NumberFormat(expr, decplaces) {
	if (isNaN(decplaces)) decplaces = 2;
	var num = parseFloat(expr);
	return num.toFixed(decplaces);
} //End function NumberFormat


function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
	//formatNumber(3.14159265, "##0.####")
	//3.1416
	//formatNumber(3.14, "0.0###%")
	//314.0%
	//formatNumber(314159, ",##0.####")
	//314,159
	//formatNumber(31415962, "$,##0.00")
	//members.ozemail.com.au/~dcrombie/format.html
    if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");
//var number1 = number - 0 ;
//alert(number+'--**********--'+number1) ;
    if (number - 0 != number) return null;  // if number is NaN return null
    var useSeparator = format.indexOf(separator) != -1;  // use separators in number
    var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
    var useCurrency = format.indexOf(currency) != -1;  // use currency format
    var isNegative = (number < 0);
    number = Math.abs (number);
    if (usePercent) number *= 100;
    format = strip(format, separator + percent + currency);  // remove key characters
    number = "" + number;  // convert number input to string

     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(decpoint) != -1;
    var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
    var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

     // split format string into LHS and RHS using decpoint as divider
    dec = format.indexOf(decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

     // adjust decimal places by cropping or adding zeros to LHS of number
    if (srightEnd.length < nrightEnd.length) {
      var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
      nrightEnd = nrightEnd.substring(0, srightEnd.length);
      if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

 // patch provided by Patti Marcoux 1999/08/06
      while (srightEnd.length > nrightEnd.length) {
        nrightEnd = "0" + nrightEnd;
      }

      if (srightEnd.length < nrightEnd.length) {
        nrightEnd = nrightEnd.substring(1);
        nleftEnd = (nleftEnd - 0) + 1;
      }
    } else {
      for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
        if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
        else break;
      }
    }

     // adjust leading zeros
    sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
    output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
    return output;
  }

  function strip(input, chars) {  // strip all characters in 'chars' from input
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++)
      if (chars.indexOf(input.charAt(i)) == -1)
        output += input.charAt(i);
    return output;
  }

  function separate(input, separator) {  // format input using 'separator' to mark 000's
    input = "" + input;
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++) {
      if (i != 0 && (input.length - i) % 3 == 0) output += separator;
      output += input.charAt(i);
    }
    return output;
  }
function openwin(url1,heit,widt){
	var winname='editrep'+Math.round(Math.random()*10000);
	var opstr='width='+widt+',height='+heit+',toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,screenx=10,screeny=10,left=10,top=10,copyhistory=no,resizable=yes' ;
	window.open(url1,winname,opstr);
	return false;
}

/**
 * Date.format()
 * string format ( string format )
 * Formatting rules according to http://php.net/strftime
 *
 * Copyright (C) 2006  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *   Herltestra�e 12
 *   D-01307, Germany
 *
 * @version  0.5
 * @todo     %g, %G, %U, %V, %W, %z, more/better localization
 * @url      http://design-noir.de/webdev/JS/Date.format/
 */

var _lang = (navigator.systemLanguage || navigator.userLanguage || navigator.language || navigator.browserLanguage || '').replace(/-.*/,'');
switch (_lang) {
	case 'de':
		Date._l10n = {
			days	: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
			months	: ['Januar', 'Februar', 'M�rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
			date	: '%e.%m.%Y',
			time	: '%H:%M:%S'};
		break;
	case 'fr':
		Date._l10n = {
			days	: ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'],
			months	: ['janvier', 'f�vrier', 'mars', 'avril', 'mai', 'juin', 'juillet', 'ao�t', 'septembre', 'octobre', 'novembre', 'decembre'],
			date	: '%e/%m/%Y',
			time	: '%H:%M:%S'};
		break;
	case 'it':
		Date._l10n = {
			days	: ['domenica', 'luned�', 'marted�', 'mercoled�', 'gioved�', 'venerd�', 'sabato'],
			months	: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
			date	: '%e/%m/%y',
			time	: '%H.%M.%S'};
		break;
	case 'en':
	default:
		Date._l10n = {
			days	: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
			months	: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
			date	: '%Y-%m-%e',
			time	: '%H:%M:%S'};
		break;
}
Date._pad = function (num, len) {
	for (var i = 1; i <= len; i++)
		if (num < Math.pow (10, i))
			return new Array(len-i+1).join(0) + num;
	return num;
};
Date.prototype.format = function (format) {
	if (format.indexOf('%%') > -1) {  // a literal '%' character
		format = format.split ('%%');
		for (var i = 0; i < format.length; i++)
			format[i] = this.format (format[i]);
		return format.join ('%');
	}
	format = format.replace (/%D/g, '%m/%d/%y');  // same as %m/%d/%y
	format = format.replace (/%r/g, '%I:%M:%S %p');  // time in a.m. and p.m. notation
	format = format.replace (/%R/g, '%H:%M:%S');  // time in 24 hour notation
	format = format.replace (/%T/g, '%H:%M:%S');  // current time, equal to %H:%M:%S
	format = format.replace (/%x/g, Date._l10n.date);  // preferred date representation for the current locale without the time
	format = format.replace (/%X/g, Date._l10n.time);  // preferred time representation for the current locale without the date
	var dateObj = this;
	return format.replace (/%([aAbhBcCdegGHIjmMnpStuUVWwyYzZ])/g, function (match0, match1) {
		return dateObj.format_callback (match0, match1);
	});
}	
Date.prototype.format_callback = function (match0, match1) {
	switch (match1) {
		case 'a': // abbreviated weekday name according to the current locale
			return Date._l10n.days[this.getDay()].substr(0,3);
		case 'A': // full weekday name according to the current locale
			return Date._l10n.days[this.getDay()];
		case 'b':
		case 'h': // abbreviated month name according to the current locale
			return Date._l10n.months[this.getMonth()].substr(0,3);
		case 'B': // full month name according to the current locale
			return Date._l10n.months[this.getMonth()];
		case 'c': // preferred date and time representation for the current locale
			return this.toLocaleString();
		case 'C': // century number (the year divided by 100 and truncated to an integer, range 00 to 99)
			return Math.floor (this.getFullYear() / 100);
		case 'd': // day of the month as a decimal number (range 01 to 31)
			return Date._pad (this.getDate(), 2);
		case 'e': // day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31')
			return Date._pad (this.getDate(), 2);
		/*case 'g': // like %G, but without the century
			return ;
		case 'G': // The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead
			return ;*/
		case 'H': // hour as a decimal number using a 24-hour clock (range 00 to 23)
			return Date._pad (this.getHours(), 2);
		case 'I': // hour as a decimal number using a 12-hour clock (range 01 to 12)
			return Date._pad (this.getHours() % 12, 2);
		case 'j': // day of the year as a decimal number (range 001 to 366)
			return Date._pad (this.getMonth() * 30 + Math.ceil (this.getMonth() / 2) + this.getDay() - 2 * (this.getMonth() > 1) + (!(this.getFullYear()%400) || (!(this.getFullYear()%4) && this.getFullYear()%100)), 3);
		case 'm': // month as a decimal number (range 01 to 12)
			return Date._pad (this.getMonth() + 1, 2);
		case 'M': // minute as a decimal number
			return Date._pad (this.getMinutes(), 2);
		case 'n': // newline character
			return '\n';
		case 'p': // either 'am' or 'pm' according to the given time value, or the corresponding strings for the current locale
			return this.getHours() < 12 ? 'am' : 'pm';
		case 'S': // second as a decimal number
			return Date._pad (this.getSeconds(), 2);
		case 't': // tab character
			return '\t';
		case 'u': // weekday as a decimal number [1,7], with 1 representing Monday
			return this.getDay() || 7;
		/*case 'U': // week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
			return ;
		case 'V': // The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
			return ;
		case 'W': // week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
			return ;*/
		case 'w': // day of the week as a decimal, Sunday being 0
			return this.getDay();
		case 'y': // year as a decimal number without a century (range 00 to 99)
			return this.getFullYear().toString().substr(2);
		case 'Y': // year as a decimal number including the century
			return this.getFullYear();
		/*case 'z':
		case 'Z': // time zone or name or abbreviation
			return ;*/
		default:
			return match0;
	}
}


// Copyright (C) krikkit - krikkit@gmx.net
// --> http://www.krikkit.net/
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
 
function copy_clip(meintext)
{
 if (window.clipboardData) 
   {
   
   // the IE-manier
   window.clipboardData.setData("Text", meintext);
   
   // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
   // het is mij echter onbekend vanaf welke versie dit precies werkt:
   }
   else if (window.netscape) 
   { 
   
   // dit is belangrijk maar staat nergens duidelijk vermeld:
   // you have to sign the code to enable this, or see notes below 
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   // maak een interface naar het clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // maak een transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specificeer wat voor soort data we op willen halen; text in dit geval
   trans.addDataFlavor('text/unicode');
   
   // om de data uit de transferable te halen hebben we 2 nieuwe objecten 
   // nodig om het in op te slaan
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*2);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   //alert("Following info was copied to your clipboard:\n\n" + meintext);
   return false;
}
function copyToBuffer(id)
{
	myObj = document.getElementById(id);
	//alert(myObj.value);
	copy_clip(myObj.value);
}

function copyToBufferOLD(id)
{
	//var isOpera = navigator.userAgent.indexOf(“Opera”) > -1;
	//var isIE = navigator.userAgent.indexOf(“MSIE”) > 1 && !isOpera;
	//var isMoz = navigator.userAgent.indexOf(“Mozilla/5.”) == 0 && !isOpera;

	myObj = document.getElementById(id);
	myObj.select();
	//myObj.focus();

	CopiedTxt = document.selection.createRange();
	CopiedTxt.execCommand("Copy");

	/*
	if (isIE)
	{
		textRange = myObj.createTextRange();
		textRange.execCommand("RemoveFormat");
		textRange.execCommand("Copy");
	}
	else {
		myObj.setSelectionRange(iStart, iEnd);
		
	}
	*/
	
	alert("code copied to your clipboard.\nopen your editor and \npaste it in.");
} 

function copyToBuffer2(myObj)
{
	myObj.select();
	//myObj.focus();
	textRange = myObj.createTextRange();
	textRange.execCommand("RemoveFormat");
	textRange.execCommand("Copy");
	//alert("code copied to your clipboard.\nopen your editor and \npaste it in.");
} 


function Trim(STRING){
STRING = LTrim(STRING);
return RTrim(STRING);
}

function RTrim(STRING){
while(STRING.charAt((STRING.length -1))==" "){
STRING = STRING.substring(0,STRING.length-1);
}
return STRING;
}


function LTrim(STRING){
while(STRING.charAt(0)==" "){
STRING = STRING.replace(STRING.charAt(0),"");
}
return STRING;
}

