//-----------------------------------------------------------------
function clearsearch(){
	var el = document.getElementById('searchField');
	if(el.value == "Site search..."){
		el.value = "";
	}
	else if(el.value == ""){
		el.value = "Site search...";
	}
}

//-----------------------------------------------------------------
function mainmenu(){
	$(" #hm_nav ul.horisontalNav ").css({display: "none"}); // Opera Fix
	$(" #hm_nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(200);
		},function(){ $(this).find('ul:first').css({visibility: "hidden"});
	});
}

//-----------------------------------------------------------------
function updatePaper2Pages( el, totalPages ){
	var paper1Pages = el.value;
	var paper2value = "";
	
	if( isNaN( paper1Pages) ) {
		paper2value = "Numeric value only please on Paper 1 Pages field.";
	}else{
		if( Number( paper1Pages ) > Number(totalPages) ){
			paper2value = "Invalid Paper 1 pages. Max " + totalPages + " pages please.";
		}else{
			paper2value = totalPages - paper1Pages;
		}
	}
	document.getElementById( "Paper 2 pages" ).value = paper2value;
}

//-----------------------------------------------------------------
function updateOption( response ){
	var xmlDoc = response.responseXML;
	var optionMarkup; 
	
	var option = xmlDoc.getElementsByTagName('option');
	for(i=0;i<option.length;i++){
		var val = option[i].getAttribute('value');
		var text = option[i].getAttribute('text');
		optionMarkup = optionMarkup + "<option value='" + val + "'>" + text + "</option>"
	}

	var root = xmlDoc.getElementsByTagName('root');
	var target = root[0].getAttribute('target');
	if( (target.toLowerCase()).indexOf( "paper 1" ) > -1 ){
		var country = root[0].getAttribute('country');
		var gradeTarget = "Paper 2 grade"; //hardcoded not ideal, but i am running out of time...
		document.getElementById( target + " wrapper" ).innerHTML = "<select id='" + target + "' name='" + target + "' onchange=\"getGradeOptions('" + country + "', this, '" + gradeTarget + "');\">" + optionMarkup + "</select>";
		return;
	}
	document.getElementById( target + " wrapper" ).innerHTML = "<select id='" + target + "' name='" + target + "'>" + optionMarkup + "</select>";
}

//-----------------------------------------------------------------
function updateWePrintOptions( response ) {
	var xmlDoc = response.responseXML;
	var optionMarkup; 
	
	var option = xmlDoc.getElementsByTagName('option');
	for(i=0;i<option.length;i++){
		var val = option[i].getAttribute('value');
		var text = option[i].getAttribute('text');
		optionMarkup = optionMarkup + "<option value='" + val + "'>" + text + "</option>"
	}

	var root = xmlDoc.getElementsByTagName('root');
	var target = root[0].getAttribute('target');
	var heightElId = root[0].getAttribute('heightElId');
	//.....hmmmmmmmmm
	var selectElId;	
	if( target == "wePrintStandSizeWrapper" ||  target == "StandSizeWrapper" ){ 
		selectElId = "We Print Standard Size";
	}
	else{
		selectElId = "We Print Width"; 
	}
	document.getElementById( target ).innerHTML = "<select id='" + selectElId + "' name='" + selectElId + "'>" + optionMarkup + "</select>";
}

//-----------------------------------------------------------------
function getWidthOptions( country, elGrade, targetId ) {
	if( elGrade.value != -1 ) {
		$.ajax( {
			type:'GET',
			complete: updateOption,
			url: 'http://www.horton.co.nz/modules/customForms/GetAQuote/PaperWidthXml.asp?country=' + country + '&grade=' + elGrade.value + '&target=' + targetId
			});
	}
}

//-----------------------------------------------------------------
function getGradeOptions( country, elWidth, targetId ) {
	if( elWidth.value != -1 ) {
		$.ajax( {
			type:'GET',
			complete: updateOption,
			url: 'http://www.horton.co.nz/modules/customForms/GetAQuote/PaperGradeXml.asp?country=' + country + '&width=' + elWidth.value + '&target=' + targetId
			});
	}
}

//-----------------------------------------------------------------
function getWePrintStandardSizeOptions( countryId, targetId ) {
	var country = document.getElementById( countryId ).value;
	if( country != "-1" ) {
		$.ajax( {
			type:'GET',
			complete: updateWePrintOptions,
			url: 'http://www.horton.co.nz/modules/customForms/GetAQuote/StandardSizeXml.asp?country=' + country + '&target=' + targetId
			});
	}
}

//-----------------------------------------------------------------
function getWePrintWidthOptions( heightEl, countryId, targetId ) {
	heightValue = heightEl.value;
	var country = document.getElementById( countryId ).value;
	if( country != "-1" ) {
		if( ( heightValue > 425 && heightValue != 578 ) || ( heightValue < 163 && heightValue.length == 3 ) || isNaN( heightValue ) ){
			heightEl.value = '';
			return;
		}
		$.ajax( {
			type:'GET',
			complete: updateWePrintOptions,
			url: 'http://www.horton.co.nz/modules/customForms/GetAQuote/WidthOptionsXml.asp?country=' + country + '&target=' + 
			targetId + '&height=' + heightValue 
			});
	}
	else{
		heightEl.value = "Select country first.";
	}
}

//-----------------------------------------------------------------
$(document).ready(
	function(){mainmenu();}
);
