function numbersonly(myfield, e, len, nextfld, decpoint)
{
var key;
var keychar;
             //alert("numbersonly " + len + " " + recnum);  
	        // alert("numbersonly ");  
if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;
   
if (keychar == '.') { // allow decimal point ?
   if (decpoint) {
       return true;  // allowed
   } 
   alert("Please round to the nearest whole number");
   return false;    // reject
}

// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
        if (nextfld && myfield.value.length == len-1) { // move on to next field if enough chars entered
            myfield.value += keychar; 
            nextfld.focus();
           
            return false;
        }
        return true;
     }
 	 alert("Please enter digits 0-9 only");

return false;
}

function checkMPRlength(strngToCheck, minlen, maxlen, field, alertnow) {

  var error = "";
  var errchar=-1;

 //alert("checkMPRlength "+strngToCheck);
  if (strngToCheck.length < minlen) {  // too short
      errchar = strngToCheck.length;
  }       
  if (errchar >= 0) {
      error = "This field should be an " + minlen + " to " + maxlen +" digit number\n\n";
      if (alertnow == true) {
	      alert(error);
          field.focus();
          field.blur(); 
          field.select();
	  }
      return false;
  }	  
  return true;
}

function validateMPR(theForm, alertnow) {

// alert("valitadeMPR");
    if (!checkMPRlength( theForm.MPR.value,7,10, theForm.MPR, alertnow)) {
       return false;
    }
    return true;
}

function validateGasUsage(theForm) {

    if (theForm.gasusage.value.length == 0 ) {
	    alert("Please enter gas consumption");
        return false;
    }
    return true;
}
function ApplyClick(etid, button, commid)
{
//   document.all.quoteform.etid.value = etid; also works
    button.form.etid.value = etid;      // elec tariff id
  // button.form.mqstate.value = "order";
  button.form.commid.value = commid;  // commission id

   return true;
}


function isFieldOk(strng, len, fieldDesc) {

  if (!checkNumber(strng,len,fieldDesc)) {
      return false;  
  }
  return true;	  
}


function checkNumber(strngToCheck, len, field) {

  var error = "";
  var errchar=-1;

  if (strngToCheck.length != len) {  // too short
      errchar = strngToCheck.length;
      field.value = "";
      for (i = 0; i < errchar; i++)  {
         field.value += strngToCheck.charAt(i); 
      }
      field.value += " ";
  }      
  else {
      errchar = checkNumeric(strngToCheck);
      if (errchar >= 0) {
          field.value = "";
          for (i = 0; i < errchar+1; i++)  {
             field.value += strngToCheck.charAt(i); 
          }
      }
  }
  if (errchar >= 0) {
     error = "This field should be a " + len + " digit number\n\n";
     alert(error);
     field.focus();
     field.blur(); 
     field.select();
     return false;
  }	  
  return true;
}


function checkNumeric(sText) {

   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
                     
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         return i;
      }
   }
   return -1;  
}

function checkDropdown(theForm, select) {

    if (select.value == "--") {
        alert("Please select a number from the drop-down list");
        select.focus();
        return false;        
    }
    return true;
}


function isUsageOK(len)
{
   
    if (len <= 0) {
        alert("Please enter a value");
		return false;
    }
	return true;
}

function isCapacityOK(strng, field)
{
   
    if (strng.length <= 0) {
        alert("Please enter a value");
        field.focus();
        field.blur(); 
        field.select();
		return false;
    }
	return true;
}

function validateMPAN(theForm) {

    if (! checkDropdown(theForm, theForm.profile)) {
        return false;
    }
    if (! checkDropdown(theForm, theForm.REC)) {
        return false;
    }    
    if (! isFieldOk( theForm.MTC.value,3, theForm.MTC)) {
        return false;
    }
    if (!isFieldOk( theForm.LLF.value,3, theForm.LLF)) {
       return false;
    }
    if (!isFieldOk( theForm.mpanleft.value,4, theForm.mpanleft)) {
       return false;
    }
    if (!isFieldOk( theForm.mpanmid.value,4, theForm.mpanmid)) {
       return false;
    }
    if (!isFieldOk( theForm.mpanright.value,3, theForm.mpanright)) {
       return false;
    }
    return true;
}


function validateCapacity(theForm) {

    if (!isCapacityOK(theForm.capacity.value,theForm.capacity)) {
  	     return false;
    }
    return true;
}

function validateUsage(theForm,tariff) {
    // alert("tariff type "+ tariff);
	switch (tariff) {
    case 1:
	     if (!isUsageOK(theForm.allunits.value.length)) {
		      return false;
	     }
		 break;
	case 2:
	     if (!isUsageOK(theForm.dayunits.value.length)) {
             return false;		 
		 }
		 if (!isUsageOK(theForm.nightunits.value.length)) {
             return false;
	     }
		 break;
	case 3:
	     if (!isUsageOK(theForm.wddunits.value.length)) {
             return false; 
		 }
		 if (!isUsageOK(theForm.ewunits.value.length)) {
             return false;
	     }
		 break;
	case 4:
	     if (!isUsageOK(theForm.wddunits.value.length)) {
             return false;
         }
		 if (!isUsageOK(theForm.ewunits.value.length)) {
             return false;	 
		 }
		 if (!isUsageOK(theForm.nightunits.value.length)) {
             return false;
	     }
		 break;
    case 5:      // STOD
         break;
    case 6:      // OFF PEAK
         break;
    case 7:      // WHITE METER
         break;
	default:
	     alert('system error, tariff not found');
		 break;
	}
    return true;
}

function OnProfileChange(selObj, next) 
{                            
    if (selObj.value == "--") {
        alert("Please select a number from the list");
    }
    else
        next.focus();
}

function OnRECChange(selObj, next) 
{
      if (selObj.value == "--") {
         alert("Please select a number from the list");
      }
      else
         next.focus();
}

function showMoreInfoRow(form, row) {
     
     element = document.getElementById(row);
     element.className = "show";
     return false;
}

function hideMoreInfoRow(form, row) {

     element = document.getElementById(row);
     element.className = "hide";
     return false;
}

function showContractDetailRow(form) {

    if (form.CurElecContract[0].checked ) {   // not under contract button
         document.all.contractRow.className = "show";
         document.all.nocontractRow.className = "hide";
         document.all.dontknowRow.className = "hide";
    }
    if (form.CurElecContract[1].checked ) {
	     document.all.contractRow.className = "hide";
	     document.all.nocontractRow.className = "show";
	     document.all.dontknowRow.className = "hide";
	     
	}
    if (form.CurElecContract[2].checked ) {
	     document.all.contractRow.className = "hide";
	     document.all.nocontractRow.className = "hide";
	     document.all.dontknowRow.className = "show";
	}

}
function elecOrderClick( button, qgid, eqid)
{  
// if (qgid == 12610)  {
//    alert("validate.js")
// }
 button.form.listIndex.value = eqid;
 button.form.eqid.value = eqid;
 button.form.qgid.value = qgid;  

   return true;
}
function OpenUtilWindow(ut)
{
    var wname='gewin';
    var childwin;
    
    window.name = "main";
    if (ut.substring(3,7)=='elec') {
        wname = 'electricity';
    }
    if (ut.substring(3,6)=='gas') {
        wname = 'gas';
    }
    if (ut.substring(3,7)=='tele') {
        wname = 'telecom';
    }
    if (ut.substring(3,10)=='enquiry') {
        wname = 'enquiry';
    }
    childwin = window.open(ut+'.htm',wname,'scrollbars=yes,width=720,height=420');
    childwin.focus();    
}

var brid=0;

function getbid() { 
   
  return brid; 
}

function putbid(brokid) { 
   
  brid = brokid;
  return 0; 
}

function showUnitUsageFields(form) {

 //  alert ("HELLO");

    selected_usage_type = eval( form.tariff.value );

    if( selected_usage_type == 1 ) {
        document.all.allunitsrow.className="show";
        document.all.dayunitsrow.className="hide";
        document.all.nightunitsrow.className="hide";
        document.all.wddunitsrow.className="hide";
        document.all.ewunitsrow.className="hide";
    }
    if( selected_usage_type == 2 ) {
        document.all.allunitsrow.className="hide";
        document.all.dayunitsrow.className="show";
        document.all.nightunitsrow.className="show";
        document.all.wddunitsrow.className="hide";
        document.all.ewunitsrow.className="hide";
    }
    if( selected_usage_type == 3 ) {
        document.all.allunitsrow.className="hide";
        document.all.dayunitsrow.className="hide";
        document.all.nightunitsrow.className="hide";
        document.all.wddunitsrow.className="show";
        document.all.ewunitsrow.className="show";
    }
    if( selected_usage_type == 4 ) {
        document.all.allunitsrow.className="hide";
        document.all.dayunitsrow.className="hide";
        document.all.nightunitsrow.className="show";
        document.all.wddunitsrow.className="show";
        document.all.ewunitsrow.className="show";
    }
}

function popupWindow() { 
   
// alert('POP UP'); 
  //  this.form.target='_preview'; 
  // window.open('',this.form.target,'dependent=no,toolbar=0,directories=0,location=0,status=1,menubar=1,scrollbars=1,resizable=1,width=700,height=400').focus();    
 
   childwin = window.open('', 'childwin','dependent=yes,toolbar=0,directories=0,location=0,status=1,menubar=1,scrollbars=1,resizable=1,width=800,height=400');
   childwin.focus();    
   return true; 
}

function MultiClick( button, act, postvar, id)
{  
   var conf;
   if (act == 'Delete') {
       conf = confirm('Are you sure you wish to delete this record ?' );
   }
   else {
       conf = true;
   }
  // alert ('Multiclick postvar='+postvar+' id= '+id);
   if (conf) { 

      if (postvar == 'listIndex') {
          button.form.listIndex.value = id;  
      }
      if (postvar == 'accountID') {
          button.form.accountID.value = id;  
      }
      if (postvar == 'siteID') {
          alert('siteid '+id);
          button.form.siteID.value = id;  
      }
      if (postvar == 'supplyID') {
          button.form.supplyID.value = id;  
      }
      if (postvar == 'oid') {
          button.form.oid.value = id;  
      }
      if (postvar == 'offerid') {
          button.form.offerid.value = id;  
      }
      if (postvar == 'commid') {
          button.form.commid.value = id;  
      }
      if (postvar == 'tariffID') {
          button.form.tariffID.value = id;  
      }
      if (postvar == 'etid') {
          button.form.etid.value = id;  
      }
      if (postvar == 'leadID') {
          button.form.leadID.value = id;  
      }
      if (postvar == 'contractID') {
          button.form.contractID.value = id;  
      }
      if (postvar == 'qgid') {
// alert('MultiClick');
          button.form.qgid.value = id;  
      }
      
   }
   return conf;
}
function chkRegister() { 
   
   alert('Register'); 
   return true; 
}

