// copyright Intelligent Sites Ltd 2003
/*

Date selector

<script>dateselect('$value','$prompt');</script>

*/
function isSetDate(inp,typ,fld){
	 thedate = new String(inp.form[fld].value); //current (hidden) date
	 bits = thedate.split('-');
	 s1 = new String(inp.value);
	 if (s1.length<2) s1='0'+s1;
	 if (typ=='y') thedate = selyear(inp)+'-'+bits[1]+'-'+bits[2];
	 if (typ=='m') thedate = bits[0]+'-'+s1+'-'+bits[2];
	 if (typ=='d') thedate = bits[0]+'-'+bits[1]+'-'+s1;
	 if (typ=='x')  { //clear
      thedate = '0000-00-00';
   //   getElementById('dv'+fld).style.
      inp.parentNode.style.display='none';
      }
   inp.form[fld].value=thedate;
   } //end MP

function monthdd(m,nm)  {
	 fmonths = new Array('--','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	 document.writeln("<SELECT ");
	 document.writeln( "onchange='isSetDate(this,",'"m","',nm,'")',"'>");
   for (i=0; i<=12; i++) {
      document.write('<OPTION VALUE="',i,'"');
			if (i==m) document.write(' SELECTED');
			document.write(">",fmonths[i],"</OPTION>");
      } //end of for loop
	 document.write('</SELECT>');
   } //end monthdd

function yearddold(y,nm) {
	 document.writeln("<SELECT ");
	 document.writeln( "onchange='isSetDate(this,",'"y","',nm,'")',"'>");

 	 document.writeln('<OPTION>earlier');
   for(i=1; i<6; i++) {
 		 document.writeln('<OPTION');
		 if (i==3)document.writeln(' SELECTED');
		 document.writeln('>',1*y + i - 3);
		 }
 	 document.writeln('<OPTION>later');
	 document.writeln('</SELECT>');
   } //end yeardd
function yeardd(y,nm) {
   yy = y;
   if (yy=='0000') {yy = '2006'; }
   document.writeln("<SELECT ");
	 document.writeln( " onchange='isSetDate(this,",'"y","',nm,'")',"'>");
   document.writeln('<OPTION>earlier');
   for(i=1; i<6; i++) {
 		 document.writeln('<OPTION');
		 if ((y!='0000') & (i==3))document.writeln(' SELECTED');
		 document.writeln('>',1*y + i - 3);
		 }
 	 document.writeln('<OPTION>later');
   document.writeln('<OPTION>this year');
   if (y=='0000') document.writeln('<OPTION SELECTED>0000');
    else document.writeln('<OPTION>0000');
   document.writeln('</SELECT>');
   } //end yeardd

function choptold(selector,d){
	 for(i=1; i<6; i++)
	 selector.options[i].text = 1*selector.options[i].text + d;
   }

function selyearold(thing) {
	 if (thing.selectedIndex==0) {
	     chopt(thing,-5); thing.selectedIndex = 1; }
	 if (thing.selectedIndex==6) {
	     chopt(thing,5); thing.selectedIndex = 1; }
	 return thing[(thing.selectedIndex)].text;
   }
function chopt(selector,d,sel){
   if (d==99) {
     today= new Date();
  	 for(i=1; i<6; i++)
	    selector.options[i].text = today.getFullYear() + i -3;
      } //if 99
   else
     {  if (d==98) {
         // do whatever
         }//if 98
        else {
           for(i=1; i<6; i++)
	         selector.options[i].text = 1*selector.options[i].text + d;
           } //else 2
	   }//else1
   selector.selectedIndex=sel;
   }//fun
function selyear(thing) {
   // choose 'earlier'
	 if (thing.selectedIndex==0) {
	     chopt(thing,-5,1); }
   // choose 'later'
	 if (thing.selectedIndex==6) {
	     chopt(thing,5,1); }
   // choose 'this year' - shift value of 99 flags 'this year';
	 if (thing.selectedIndex==7) {
	     chopt(thing,99,3); }
   // choose 'blank' - shift value of 98 flags 'blank';
	 if (thing.selectedIndex==8) {
	     chopt(thing,98,8); }
	 return thing[(thing.selectedIndex)].text;
   }

 function dateselect(dt, nm){
   // dt = default date eg 2001-10-29
	 // nm = name for output date
	 bits = dt.split('-');
	 document.write("<input type='text' value='",bits[2],"' size=1 ");
	 document.writeln( "onchange='isSetDate(this,",'"d","',nm,'")',"'>");
	 monthdd(bits[1],nm);
	 yeardd(bits[0],nm);
	 document.write("<input type='hidden' value='",dt,"' name='",nm,"' >");
   }

  function dateselectornull(dt, nm){
   // dt = default date eg 2001-10-29
	 // nm = name for output date
   s2 = new String(dt);
	 if (s2.length<9) s2 = '0000-00-00';
   bits = s2.split('-');
   document.write("<div id='dv_",nm,"'>");
	 document.write("<input type='text' value='",bits[2],"' size=1 ");
	 document.writeln( "onchange='isSetDate(this,",'"d","',nm,'")',"'>");
	 monthdd(bits[1],nm);
	 yeardd(bits[0],nm);
	 document.write(" <input type='button' value='clear date'");
	 document.writeln( "onclick='isSetDate(this,",'"x","',nm,'")',"'>");
   document.write("</div>");
	 document.write("<input type='hidden' value='",dt,"' name='",nm,"' >");
   }
/*

Checkbox:
to show a check box to accept boolean
database field should be an int(1) /TINYINT(1)
value is 0(false) or 1(true)
call with:  <script>checkbox('$value','$field');</script>

*/
	function isSetCheck(inp,fld) {
	  //inp is 'this' ie the form's checkbox.
		//alert ('checkbox='+ inp.checked);
	  checked = 0;
	  if (inp.checked) checked=1; //ie toggle it
	 // alert(checked);
	  inp.form[fld].value = checked;
    }

  function checkbox(vl, nm) {
	 // write visible element
	 document.write("<input type='checkbox'");
	 if (vl==1) document.write(" checked ");
	 //document.writeln( "onchange='alert(",nm,")'>");
	 document.writeln( "onchange='isSetCheck(this,",'"',nm,'"',")'>");
	 //write hidden element
	 document.write("<input type='hidden' value='",vl,"' name='",nm,"' >");
	 }

/*

Numeric Field
to show a numeric field. If entered with commas, they will be stripped out.
call with:  <script>numfield('$value','$field');</script>

*/
function stripcommas(inp,fld) {
    s = inp.value;  //string including commas
		rExp=/,/g
		s = s.replace(rExp,'');
	  //inp is 'this' ie the calling textbox.
		inp.form[fld].value = s;
    }

function numfield(vl, nm) {
	 // write visible element
	 document.write("<input type='text' class='numeric' value='",vl,"'");
	 document.writeln( "onchange='stripcommas(this,",'"',nm,'"',")'>");

	 //write hidden element
   s = vl;
   rExp=/,/g
	 s = s.replace(rExp,'');
	 document.write("<input type='hidden' value='",s,"' name='",nm,"' >");
	 }

