var baseurl='https://www.energyaverage.co.uk/';var baseurlht='http://www.energyaverage.co.uk/';$(document).ready(function(){$("#UserAddForm").submit(function(){if($(this).validation()==true){if(checkEmail(document.getElementById("E-mail_Address").value)){if(document.getElementById("E-mail_Address").value==document.getElementById("Confirm_E-mail_Address").value){var pass=document.getElementById("Password_").value;if(pass.length<4){$('#Password_').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be minimum 4 alphanumeric characters in length.</b></div> ");return false}if(check_alphanumeric(pass)){}else{$('#Password_').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be alphanumeric characters.</b></div> ");return false}if(document.getElementById("Password_").value==document.getElementById("Confirm_Password").value){if((document.getElementById("terms").checked)==true){if(document.getElementById("a_flag").value==0){checkAvailability(document.getElementById("E-mail_Address").value,baseurl);return false}return true}$('#err_dis').html("<div class='error_div'><b>Please agree to the terms of use and privacy policy.</b></div> ");return false}$('#Password').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password Mismatch.</b></div> ");return false}$('#E-mail_Address').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Email Address Mismatch.</b></div> ");return false}$('#E-mail_Address').addClass("error-highlight");return false}return false});$("#UserAddFormContinue").submit(function(){if($(this).validation()==true){if(checkPostCode(document.getElementById("Post_Code").value)){var err="";if(document.getElementById("house_type_1").checked==false&&document.getElementById("house_type_2").checked==false&&document.getElementById("house_type_3").checked==false){err+=" House Type,"}if(document.getElementById("number_people_1").checked==false&&document.getElementById("number_people_2").checked==false&&document.getElementById("number_people_3").checked==false&&document.getElementById("number_people_4").checked==false&&document.getElementById("number_people_5").checked==false){err+=" Number of People,"}if(document.getElementById("occupied_1").checked==false&&document.getElementById("occupied_2").checked==false){err+=" Occupied,"}if(document.getElementById("gas_1").checked==false&&document.getElementById("gas_2").checked==false){err+=" Use Gas,"}if(document.getElementById("heat_1").checked==false&&document.getElementById("heat_2").checked==false&&document.getElementById("heat_3").checked==false){err+=" Heating,"}if(err==""){if((document.getElementById("gas_2").checked==true)&&(document.getElementById("heat_1").checked==true)){$('#err_dis').html("<div class='error_div'><b>Fuel Used For Heating</b> cannot be gas if you don't use Gas.</div> ");return false}else{return true}}else{$('#err_dis').html("<div class='error_div'><b>"+err.substr(0,err.length-1)+"</b> fields cannot be blank.</div> ");return false}}else{$('#Post_Code').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Post Code</b> has invalid format.</div> ");return false}}return false});$("#UserEditForm").submit(function(){if($(this).validation()==true){if(checkEmail(document.getElementById("E-mail_Address").value)){if(checkPostCode(document.getElementById("Post_Code").value)){if(document.getElementById("a_flag").value==0){checkEmailAvailability(document.getElementById("E-mail_Address").value,baseurl);return false}if((document.getElementById("UserGasNo").checked==true)&&(document.getElementById("UserHeatGas").checked==true)){$('#err_dis').html("<div class='error_div'><b>Fuel Used For Heating</b> cannot be gas if you don't use Gas.</div> ");return false}else{return true}}else{$('#Post_Code').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Post Code</b> has invalid format.</div> ");return false}}$('#E-mail_Address').addClass("error-highlight");return false}return false});$("#passwordform").submit(function(){if($(this).validation()==true){var pass=document.getElementById("New_Password").value;if(pass.length<4){$('#New_Password').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be minimum 4 alphanumeric characters in length.</b></div> ");return false}if(check_alphanumeric(pass)){}else{$('#New_Password').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be alphanumeric characters.</b></div> ");return false}if(document.getElementById("New_Password").value==document.getElementById("Confirm_New_Password").value){return true}else{$('#err_dis').html("<div class='error_div'><b>Password mismatch.</b></div> ");return false}}return false});$("#passwordforce").submit(function(){if($(this).validation()==true){var pass=document.getElementById("New_Password").value;if(pass.length<4){$('#New_Password').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be minimum 4 alphanumeric characters in length.</b></div> ");return false}if(check_alphanumeric(pass)){}else{$('#New_Password').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Password should be alphanumeric characters.</b></div> ");return false}if(document.getElementById("New_Password").value==document.getElementById("Confirm_New_Password").value){return true}else{$('#err_dis').html("<div class='error_div'><b>Password mismatch.</b></div> ");return false}}return false});$("#UserRecordAddForm").submit(function(){if($(this).validation()==true){if(document.getElementById("product_1").checked==false&&document.getElementById("product_2").checked==false){$('#err_dis').html("<div class='error_div'><b>Gas Or Electricity Record </b>field cannot be blank.</div> ");return false}if(document.getElementById("s_flag").value==""){$('#UserRecordSupplierId1').addClass("error-highlight");$('#UserRecordSupplierId2').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Supplier Name </b>field cannot be blank.</div> ");return false}if(check_number(document.getElementById("Units_Used").value)){if(check_numeric(document.getElementById("Bill_Amount").value)){document.getElementById("Bill_Amount").value=Math.round(document.getElementById("Bill_Amount").value*100)/100;dt=document.getElementById('Bill_Start_Date');if(isDate(dt.value)==false){$('#Bill_Start_Date').addClass("error-highlight");return false}dt=document.getElementById('Bill_End_Date');if(isDate(dt.value)==false){$('#Bill_End_Date').addClass("error-highlight");return false}if(datecomptoday()){}else{$('#Bill_End_Date').addClass("error-highlight");$('#Bill_Start_Date').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Bill Start, End Date should not be greater than today.</b></div> ");return false}if(datecomp()){return true}else{$('#Bill_End_Date').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Bill End Date should be greater than Bill Start Date.</b></div> ");return false}}else{$('#Bill_Amount').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Bill Amount</b> should be non negative numeric value.</div> ");return false}}else{$('#Units_Used').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Units Used</b> should be non negative numeric value.</div> ");return false}}return false});$("#referFriend").submit(function(){if($(this).validation()==true){if(checkEmail(document.getElementById("E-mail_Address").value)){return true}else{$('#E-mail_Address').addClass("error-highlight");return false}}return false});$("#ContactUs").submit(function(){if($(this).validation()==true){if(checkEmail(document.getElementById("E-mail_Address").value)){if(document.getElementById("a_flag").value==0){checkCapContact(document.getElementById("Security_Code").value,baseurlht)}else{return true}}else{$('#err_dis').html("<div class='error_div' style='width:890px;'>   Please Enter A Valid Email Address     </div>  ");$('#E-mail_Address').addClass("error-highlight");return false}}return false});$("#compare").submit(function(){if($(this).validation()==true){var from=document.getElementById("from_year").value+document.getElementById("from_month").value;var to=document.getElementById("to_year").value+document.getElementById("to_month").value;if(parseInt(from)<=parseInt(to)){if(parseInt(document.getElementById("from_year").value)!=parseInt(document.getElementById("to_year").value)){if(parseInt(document.getElementById("to_year").value)-parseInt(document.getElementById("from_year").value)>1){$('#from_year').addClass("error-highlight");$('#from_month').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Date interval should not be more than 12 months.</b></div>");return false}if(parseInt(document.getElementById("from_month").value,10)>parseInt(document.getElementById("to_month").value,10)){return true}else{$('#from_year').addClass("error-highlight");$('#from_month').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Date interval should not be more than 12 months.</b></div>");return false}}return true}else{$('#from_year').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>To date should not be lesser than From date.</b></div>");return false}}return false})});function datecomptoday(){var today=document.getElementById("Bill_Start_Date").value;var start=document.getElementById("Bill_End_Date").value;var current=document.getElementById("Cur_Date").value;var today_val=today.split("-",3);var start_val=start.split("-",3);var cur_val=current.split("-",3);var msg=0;var t_val=today_val[2]+today_val[1]+today_val[0];var s_val=start_val[2]+start_val[1]+start_val[0];var c_val=cur_val[2]+cur_val[1]+cur_val[0];if(parseInt(c_val,10)<parseInt(t_val,10)){return false}if(parseInt(c_val,10)<parseInt(s_val,10)){return false}return true}function datecomp(){var today=document.getElementById("Bill_Start_Date").value;var start=document.getElementById("Bill_End_Date").value;var today_val=today.split("-",3);var start_val=start.split("-",3);var msg=0;var t_val=today_val[2]+today_val[1]+today_val[0];var s_val=start_val[2]+start_val[1]+start_val[0];if(parseInt(s_val,10)<=parseInt(t_val,10)){return false}else{return true}}function checkAvailability(email,url){$.get(url+"users/checkEmailAvailability/"+email,function(data){var val=data.split("<");if(val[0]=="0"){checkCap(document.getElementById("Security_Code").value,url)}else{$('#E-mail_Address').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>E-mail Address already exists. Please use Forgot Password function if signed up already.</b></div> ")}})}function checkEmailAvailability(email,url){var id=document.getElementById("user_id").value;$.get(url+"users/checkEmailDuplicate/"+id+"/"+email,function(data){var val=data.split("<");if(val[0]=="0"){document.getElementById("a_flag").value=1;$("form:UserEditForm").submit()}else{$('#E-mail_Address').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>E-mail Address already exists.</b></div> ")}})}function checkCap(cap,url){$.get(url+"users/checkCap/"+cap,function(data){var val=data.split("<");if(val[0]=="0"){document.getElementById("a_flag").value=1;$("form:UserAddForm").submit()}else{$('#Security_Code').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Security Code does not match. Please try again.</b></div> ")}})}function checkCapContact(cap,url){$.get(url+"users/checkCap/"+cap,function(data){var val=data.split("<");if(val[0]=="0"){document.getElementById("a_flag").value=1;$("form:ContactUs").submit()}else{$('#Security_Code').addClass("error-highlight");$('#err_dis').html("<div class='error_div'><b>Security Code does not match. Please try again.</b></div> ")}})}function refreshCap(){$.get("../users/captchas/",function(data){$('#imagecaptch').src=data})}(function($){$.fn.validation=function(){var error=0;var error_text='';$('.required',this).each(function(){var input=$(':input',this).attr("value");var display_name=$(':input',this).attr("id");input=jQuery.trim(input);if(input==""){$(':input',this).addClass("error-highlight");display_name=((display_name.replace('_',' ')).replace('_',' ')).replace('_',' ');error++;$(':input',this).val('');$(':input',this).focus();error_text+=display_name+', '}else{$('span.error-message',this).remove();$(':input',this).removeClass("error-highlight")}});if(error==0){$('#err_dis').html("");return true}else{error_text=error_text.substring(0,(error_text.length-2));$('#err_dis').html("<div class='error_div' > <b>"+error_text+"</b> fields cannot be blank. </div>  ");return false}}})(jQuery);function checkEmail(emailString){splitVal=emailString.split('@');if(splitVal.length<=1){$('#err_dis').html("<div class='error_div' > <b>Please Enter A Valid Email Address.</b> </div>  ");return false}if(splitVal[0].length<=0||splitVal[1].length<=0){$('#err_dis').html("<div class='error_div' > <b>Please Enter A Valid Email Address.</b> </div>  ");return false}splitDomain=splitVal[1].split('.');if(splitDomain.length<=1){$('#err_dis').html("<div class='error_div' > <b>Please Enter A Valid Email Address.</b> </div>  ");return false}if(splitDomain[0].length<=0||splitDomain[1].length<=1){$('#err_dis').html("<div class='error_div' > <b>Please Enter A Valid Email Address.</b> </div>  ");return false}return true}function check_number(sText){var ValidChars="0123456789";var IsNumber=true;var Char;for(i=0;i<sText.length&&IsNumber==true;i++){Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1){IsNumber=false}}return IsNumber}function check_alphanumeric(sText){var ValidChars="0123456789ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";var IsNumber=true;var Char;for(i=0;i<sText.length&&IsNumber==true;i++){Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1){IsNumber=false}}return IsNumber}function check_numeric(sText){var ValidChars="0123456789.";var IsNumber=true;var Char;for(i=0;i<sText.length&&IsNumber==true;i++){Char=sText.charAt(i);if(ValidChars.indexOf(Char)==-1){IsNumber=false}}return IsNumber}function specialCharCheck(strng){var iChars="\\";var a1=new Array();a1=strng.split("\\");a_count=(a1.length)-1;strng=a1[a_count];var iChars="!@#$%^&*()+=-[]\'';,/{}|\":<>?~`";var iCharsspace=" ";for(var i=0;i<strng.length;i++){if(iChars.indexOf(strng.charAt(i))!=-1){alert("Filename contains special characters. \n These are not allowed.\n Please remove them and try again.");return false}if(iCharsspace.indexOf(strng.charAt(i))!=-1){alert("Filename contains special characters. \n These are not allowed.\n Please remove them and try again.");return false}}return true}function usernamenull(){if(document.userlogin.email_t.value=="E-Mail Address"){document.userlogin.email_t.value=""}}function passwordnull(){if(document.userlogin.password_t.value=="password"){document.userlogin.password_t.value=""}}function chngepass(){window.open("../../users/changePassword","Window1","menubar=no,width=440,height=250,toolbar=no")}function lostpass(){window.open(baseurlht+"users/lostPassword","Window1","menubar=no,width=440,height=250,toolbar=no")}function occupied(){window.open(baseurl+"users/occupied","Window1","menubar=no,width=450,height=250,toolbar=no")}function howreadbill(){window.open(baseurl+"users/howReadBill","Window1","menubar=no,width=450,height=450,toolbar=no")}function occupied(){window.open(baseurl+"users/occupied","Window1","menubar=no,width=450,height=250,toolbar=no")}
	 
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtdq = new Date();
var dtCh= "-";
//var minYear = dtdq.getFullYear();
var minYear = 2005;
var maxYear = 2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
		if (pos1==-1 || pos2==-1){
		$('#err_dis').html("<div class='error_div'><b>The date format should be : dd-mm-yyyy</b></div> ");
		//alert("The date format should be : dd-mm-yyyy")
		return false
	}
	
	if (strMonth.length<2)
	{
	 $('#err_dis').html("<div class='error_div'><b>The month format should be : mm</b></div> ");
		//alert("The month format should be : mm")
		return false
	}
	
	if (strDay.length<2)
	{
	 $('#err_dis').html("<div class='error_div'><b>The day format should be : dd</b></div> ");
		//alert("The day format should be : dd")
		return false
	}
	
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth,10)
	day=parseInt(strDay,10)
	year=parseInt(strYr,10)
	
	

	if (strMonth.length<1 || month<1 || month>12){
		$('#err_dis').html("<div class='error_div'><b>Please enter a valid month</b></div> ");
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		$('#err_dis').html("<div class='error_div'><b>Please enter a valid day</b></div> ");
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		$('#err_dis').html("<div class='error_div'><b>Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"</b></div> ");
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		$('#err_dis').html("<div class='error_div'><b>Please enter a valid date</b></div> ");
		//alert("Please enter a valid date")
		return false
	}
return true
}





function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "{1}" + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}
