// Javscript functions for www.jewson.co.uk

function clearSearch(state) {
	if (state == 'focus') {
		if (document.forms['searchform'].search.value == "search this site") {
			document.forms['searchform'].search.value = "";
		}
	} else if (state == 'blur') {
		if (document.forms['searchform'].search.value == "") {
			document.forms['searchform'].search.value = "search this site";
		}
	}
}

function clearLocator(state) {
    if (state == 'focus') {
        if (document.getElementById('branchLocator').value == "Town or postcode") {
            document.getElementById('branchLocator').value = "";
        }
    } else if (state == 'blur') {
        if (document.getElementById('branchLocator').value == "") {
            document.getElementById('branchLocator').value = "Town or postcode";
        }
    }
}

function clearRegisterLocator(state) {
    if (state == 'focus') {
        if (document.forms['homelocatorRegister'].searchstring.value == "Town or postcode") {
            document.forms['homelocatorRegister'].searchstring.value = "";
        }
    } else if (state == 'blur') {
        if (document.forms['homelocatorRegister'].searchstring.value == "") {
            document.forms['homelocatorRegister'].searchstring.value = "Town or postcode";
        }
    }
}

function locatorSwitch(form) {
    if (form.type[0].checked == true) {
        form.addr3.value = form.searchstring.value;
        form.pc.value = '';
    } else if (form.type[1].checked == true) {
        form.addr3.value = '';  
        form.pc.value = form.searchstring.value;
    } else {
        alert('Please select if you are searching for town or postcode');
    }
}

function locatorSwitchMultimap(form) {
    if (form.type[0].checked == true) {
        form.addr2.value = form.searchstring.value;
        form.addr3.value = '';  
        form.pc.value = '';
    } else if (form.type[1].checked == true) {
        form.addr2.value = '';
        form.addr3.value = form.searchstring.value;
        form.pc.value = '';
    } else if (form.type[2].checked == true) {
        form.addr2.value = '';
        form.addr3.value = '';  
        form.pc.value = form.searchstring.value;
    } else {
        alert('Please select if you are searching for street, town or postcode');
    }
    
}

function submitRangeSearch(start, end) {
	var today = new Date();
	var startDate = "1/" + start + "/" + today.getFullYear();
	var endDate = daysInMonth(end, today.getFullYear()) + "/" + end + "/" + today.getFullYear();
	
	var form = document.forms["search"];
	
	form.startDate.value = startDate;
	form.endDate.value = endDate;
	
	form.submitSearch.click();
}

function daysInMonth(month, year){
	return 32 - new Date(year, month - 1, 32).getDate();
}


/** This method resizes the div as per the window height
* It leaves the dimensions as is, if null values provided for percent change required
*
*/
function resizeDivWithGivenId(divId, windowHeight, pctHeight, windowWidth, pctWidth) {
	//alert("windowHeight" + windowHeight);
	//alert("windowWidth" + windowWidth);

	if(divId != null){
		//alert("divId" + divId);
		var divToResize = document.getElementById(divId);

		if(divToResize != null ) {
			//alert("divToResize not null" + divToResize);
			var divHeight = divToResize.style.height;
			var divWidth = divToResize.style.width;

			//alert("original div height " + divHeight);
			//alert("original div width " + divWidth);

			divToResize.height = getResizedDimension(windowHeight, divHeight, pctHeight);
			divToResize.width = getResizedDimension(windowWidth, divWidth, pctWidth);

			//alert("new height " + divToResize.height);
			//alert("new width " + divToResize.width);
		}

	}
}

/**
* This method returns the new dimension of the element as a percentage of the container dimension
* If the container dimension or the percent change are not provided, it leaves the dimension unchanged
*
*/
function getResizedDimension(containerDimension, originalDimension, pctChangeRequired){
	//alert("containerDimension" + containerDimension)
	//alert("original dimension" + originalDimension);
	var newDimension = originalDimension;
	if(containerDimension != null || containerDimension!=''){
		if(pctChangeRequired != null || pctChangeRequired != '') {
			newDimension = containerDimension *(pctChangeRequired);
		}
	}
	//alert("newDimension" + newDimension);
	return newDimension;
}

/**
* This method gets the window height for different browsers
*
*/
function getWindowXY(param) {
	  if(param == 'Y'){
	  	  var myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		     myHeight = window.innerHeight;

		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		     myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		     myHeight = document.body.clientHeight;
		  }
		 //window.alert( 'Height = ' + myHeight );
		 return myHeight;
	 }
	  else if(param == 'X'){
		  var myWidth = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
			  myWidth = window.innerWidth;

		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
			  myWidth = document.documentElement.clientWidth;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
			  myWidth = document.body.clientWidth;
		  }
		 //window.alert( 'Width = ' + myWidth );
		 return myWidth;
	 }
}


/*function getRegSelectBoxWidth(){
	var divContainer = document.getElementById('menuBoxBodyReg');
	if(divContainer != ''){
		return divContainer.style.length * 0.33;
	}	
} */


