function numbersonly(myfield, e, len, nextfld, decpoint, space)
{
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
}
if (keychar == ' ') { 
   if (space) {
       return true;  // allowed
   } 
   alert("Please enter number only without spaces");
   return false;    // reject
}
if (keychar == '-') { 
//   if (neg) {
       return true;  // allowed
 //  } 
//   alert("Negative numbers are not allowed");
//   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;
        }
   //     if  (myfield.value.length >= len) {
   //         alert("Max. number of digits is "+len);
   //         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 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;
var linkargs="";

function getbid() { 
   
  return brid; 
}

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

function passLinkVars(linkvarstr) { 
   
  linkargs = linkvarstr;
  return 0;                                                                       
}

function getSessionLinks()
{
    return linkargs;
}


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 submitMenu( menu, event)
{  
    menu.form.barbut1.value = event;  
    menu.form.submit();
}   

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; 
}

function supplierMenu(menu) {
   //   if (form.CurElecContract[0].checked ) {   // not under contract button
   //      document.all.contractRow.className = "show";
     if (menu.value == -1) {
  //       menu.form.TXT_OTHER_SUPPLIER_NAME.className = "show";
         document.all.TXT_OTHER_SUPPLIER_NAME_TITLE.className = "show";
         document.all.TXT_OTHER_SUPPLIER_NAME.className = "show";
         document.all.TXT_OTHER_SUPPLIER_NAME_INPCELL.className = "show";
     }
     else {
         document.all.TXT_OTHER_SUPPLIER_NAME_TITLE.className = "hide";
         document.all.TXT_OTHER_SUPPLIER_NAME.className = "hide";
         document.all.TXT_OTHER_SUPPLIER_NAME_INPCELL.className = "hide";
     }
     return true;      
}

function otherSupplier(inp) {

     inp.form.MNU_SUPPLIERS.value = '-1';
     return true;      
}

function noSupplyContract(check) {
 
     if(check.form.CHK_NO_CONTRACT.checked) {
         check.form.DT_SUPPLY_RENEWAL.value = '';
     }
     return true;      
}

function billingToSupply(check)
{                                          
    if(check.form.CHK_NO_CONTRACT.checked) {
        check.form.TXA_SUPPLYADDRESS.value = check.form.HID_TXA_BILL_ADDRESS.value;
    }    

}

function inputDateFocus(dt) {
  
    if ( dt.form.DT_SUPPLY_RENEWAL.value == 'DD/MM/YYYY') {
        dt.form.DT_SUPPLY_RENEWAL.value ='';
    }
    return true;
}

function supplyRenewalDate(dt) {
  
 if(dt.form.CHK_NO_CONTRACT.checked) {
     dt.form.CHK_NO_CONTRACT.checked = false;
 }
 return true;
}

function ToggleCheckAll(frm) {

    var checkall=false;
    
    if (frm.form.CHK_TOGGLE_CHECK_ALL.checked == true) {
          checkall = false;
    }
    else {
          checkall = true;
    }
    for (var i=0; i < frm.form.elements.length; i++) {
        var e = frm.form.elements[i];
        if ((e.name != 'CHK_TOGGLE_CHECK_ALL' && e.type=='checkbox')) {        
            e.checked = frm.form.CHK_TOGGLE_CHECK_ALL.checked;
        }
    }
    return true;
}

function selectSite(menu) {
  var menutext;

  if (menu.value > 0) {
      menutext = mytrim(menu.options[menu.selectedIndex].text)
      splitaddr = extractPostcode(menutext);
      if (splitaddr != null) {
        address = splitaddr[1];
        postcode = splitaddr[2];
      }
      menu.form.TXA_SUPPLYADDRESS.value = address;
      menu.form.TXTINP_CUST_SUPPLY_POSTCODE.value = postcode;
  } 
  else {      // Please Select = 0, New address = -1 
    //  menu.form.TXTINP_CUST_SUPPLY_POSTCODE.value = '';
    //  menu.form.TXTINP_CUST_SUPPLY_POSTCODE.disabled = false;    
  }
  return true;  
}


/* tests to see if string is in correct UK style postcode: AL1 1AB, BM1 5YZ etc. */
function isValidPostcode(p) {
    var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
    return postcodeRegEx.test(p);
}

/**  * CALLBACK - determine if the provided postcode is valid.
  *  * @param string $postcode  
  * @return bool TRUE if valid, FALSE otherwise  * @author George Edwards  */

function is_valid_uk_postcode($postcode) { 
    $pattern = "/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/" 
    if (preg_match($pattern, $postcode)) { 
            return TRUE;  
    }          
    return FALSE; 
} 

function mytrim(str) {
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}
/* valid postcodes 
AN NAA    M1 1AA 
ANN NAA   M60 1NW 
AAN NAA   CR2 6XH 
AANN NAA  DN55 1PT 
ANA NAA   W1A 1HQ 
AANA NAA  EC1A 1BB 
*/


function extractPostcode(str) {

    var pattern = /(.*)\.\.\. ([A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2})/i;

    match = str.match(pattern);
    return match;
}
















