﻿// Fire off page specific init method

$j(document).ready(function() {

	if (typeof initPage == "function"){			                        										
		initPage();
	}
	
	/*Loop through the address finder controls (as there can be more than one) and call their InitControl method */	
	if (typeof addressFinderControls != "undefined") {
	    if (addressFinderControls.length > 0) {
	        for (i=0; i < addressFinderControls.length; i++) {
	            if (typeof eval(addressFinderControls[i] + "_InitControl") == "function") {
	                eval(addressFinderControls[i] + "_InitControl();");
	            }
	        }
	    }
	}
	
	externalLinksInit();
	resize();
	
	setupConfirmationForm();

});

$j(window).resize(function(){
	resize();});

/* Run these things last otherwise they can't get bound to functions exicuted during initPage
----------------------------------------------- */
function externalLinksInit() {
	$j("a.external").each(function(){
		$j(this).click(function(){return !window.open($j(this).attr("href"));});
	});
};

/* Ajax methods */
function ajaxReplace(href, containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	$j.ajax({
		url: href + "&" + isAjaxParamName + "=true",
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);	}	});}
			
function ajaxEmailSignup(containerId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	email = $j('#Master_uiEmailSignup_txtEmail').val();
	$j.ajax({
		url: "?email="+ email +"&" + isAjaxParamName + "=true",
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);
		},
		error: function(response){
		    $j(containerId).html(response);	}	});}
		    
/* Postcode ajax method */
function ajaxPostcodeSearch(containerId, controlId, isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";	
	$j("#"+controlId+"_loading").show();	
	var urlToUse = '';	
	
	if( jQuery.browser.msie && (jQuery.browser.version < 7) )	{
		//IE6 acts differently and takes the client val div if present into the url so we need to remove it.		
		urlToUse = location.href.replace('#ClientValidationSummary', '').replace('http://www.hillarys.co.uk', '');								
		var postcode = $j('#'+controlId+'_Postcode').val();					
                
		if(urlToUse.substr(urlToUse.length-4,4) == ".htm")		{				    
		    $j.ajax({ 
	            url: urlToUse + '?type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true&controlid=" + controlId,
		        dataType: "html",
		        success: function(response){
		            $j(containerId).html(response);
		            ajaxPostcodeListApplyClick(controlId);
		            ajaxPostcodeFindBtnApplyClick(controlId);
		            ajaxPostcodeManualLinkApplyClick(controlId);
		        }	
		    });		
		}
		else {//we need to amend the url if it already has a query string  
		    $j.ajax({ 
	            url: urlToUse + '&type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true&controlid=" + controlId,
		        dataType: "html",
		        success: function(response){
		            $j(containerId).html(response);
		            ajaxPostcodeListApplyClick(controlId);
		            ajaxPostcodeFindBtnApplyClick(controlId);
		            ajaxPostcodeManualLinkApplyClick(controlId);		            		            
		        }	
		     });		
		} 
	}	    
	else {
	    var postcode = $j('#'+controlId+'_Postcode').val();				    
		$j.ajax({ 
	        url: urlToUse + '?type=search&postcode='+ postcode +"&" + isAjaxParamName + "=true&controlid=" + controlId,
		    dataType: "html",
		    success: function(response){
		        $j(containerId).html(response);	
		        ajaxPostcodeListApplyClick(controlId);
	            ajaxPostcodeFindBtnApplyClick(controlId);
	            ajaxPostcodeManualLinkApplyClick(controlId);	        
		    }	
		});	
	}
}

function ajaxPostcodeListApplyClick(controlId)
{
	$j("."+controlId+"_item").click(function(){
		ajaxPostcodeSelect(this.href, '#'+controlId+'_result-container', controlId);
		return false;
	});
}  

function ajaxPostcodeFindBtnApplyClick(controlId){
	$j("#"+controlId+"_FindAddressBtn").validationAideDisableOnClick("#aspnetForm");
	$j("#"+controlId+"_FindAddressBtn").click(function(){
		ajaxPostcodeSearch('#'+controlId+'_result-container',controlId);	
		return false;
	});
}

function ajaxPostcodeManualLinkApplyClick(controlId){
	$j("#"+controlId+"_manuallink").click(function(){
		ajaxPostcodeSelect(this.href, '#'+controlId+'_result-container', controlId);		
		return false;
	});	
}	        
		        
/* Ajax postcode select */
function ajaxPostcodeSelect(href, containerId, controlId ,isAjaxParamName){
	isAjaxParamName = isAjaxParamName ? isAjaxParamName : "isajaxrequest";
	$j("#"+controlId+"_loading").show();
	$j.ajax({
		url: href + "&" + isAjaxParamName + "=true&controlid=" + controlId,
		dataType: "html",
		success: function(response){
			$j(containerId).html(response);			
			ajaxPostcodeFindBtnApplyClick(controlId);
			ajaxPostcodeManualLinkApplyClick(controlId);
			$j('#'+controlId+'_fullAddress').show();
		}	
	});
}
			
function showTab(divToShow, tabToActivate){
	//deactivate all tabs
	$j("#Tabs > ul > li").removeClass("active");

	//hide all tab bodys
	$j("#Tabs > div").hide();

	//show and activate the tab
	$j("#"+ tabToActivate).addClass("active");
	$j("#"+ divToShow).show();}
/* resize detection fuinction */
function resize() { 
	var documentWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		documentWidth = window.innerWidth; /* Non-IE */
	} else if ( document.documentElement && ( document.documentElement.clientWidth ) ) {
		documentWidth = document.documentElement.clientWidth; /* IE 6+ in 'standards compliant mode' */
	} else if( document.body && ( document.body.clientWidth ) ) {
		documentWidth = document.body.clientWidth; /* IE 4 compatible */
	}
	if(documentWidth <= 980){
		$j("body").addClass("lores");
		toggleWords("off");
	}else if (documentWidth > 980){
		$j("body").removeClass("lores");
		toggleWords("on");	}}
function toggleWords(state){
	if(state == "off"){
		$j("#NavigationSecondary ul li a span").hide();
	}else if (state == "on"){
		$j("#NavigationSecondary ul li a span").show();	}}
		
function setupConfirmationForm()
{	
	$j("#BuildingTypeOtherContainter").hide();
	
	$j("#Master_MainContentPlaceHolder_drpPropertyType").change(function () {
		var selectedValue = $j("#Master_MainContentPlaceHolder_drpPropertyType").val();
		toggleOtherBox(selectedValue);
	});
}

function toggleOtherBox(selectedValue)
{
	if(selectedValue =="Other")
	{
		$j("#BuildingTypeOtherContainter").show();
	}
	else
	{
		$j("#BuildingTypeOtherContainter").hide();
	}
}

function isNumeric(strString)
{
   //  check for valid numeric strings	
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function getAppointmentVaildationRules()
{
	var myRules = jQuery.validationAide.getDefaultValidationRules();
	myRules.add('validator-home-or-mobile-required', 'Please enter either a home number or a mobile number', function(fieldValue, fieldObj) {
		if (fieldValue == "" && jQuery('input.js-mobile-number').val() == "" ) {
				return false;
		}
		return true;
	});
  myRules.add('validator-must-be-numeric', 'Please enter an approximate amount as a number instead of a word', function(fieldValue, fieldObj) {
      if (isNumeric(fieldValue) == false) return false;
      return true;
  });
  myRules.add('validator-must-be-greater-than-zero', 'Please enter the approximate numbers - this must be greater than 0', function(fieldValue, fieldObj) {
      if (isNumeric(fieldValue) && fieldValue == "0") return false;
      return true; 
  });
  
  return myRules;
}