// ------------------------------------ sets up css for cross browser code

isMac = (navigator.appVersion.indexOf("Mac") != -1);
isWindows = (navigator.appVersion.indexOf("Windows") != -1);
isIE = (navigator.appName == "Microsoft Internet Explorer");
isNN = (navigator.appName == "Netscape");
isIE5 = (navigator.appVersion.indexOf("MSIE 5") != -1);
isSafari = (navigator.appVersion.indexOf("Safari") != -1);

if (isIE) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/ie.css">');
}
if (isNN) {
	//document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/nn.css">');
}
if (isMac && isIE) {
	//document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/mac.css">');
}
if (isIE5) {
	//document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/ie5.css">');
}
if (isSafari) {
	document.write('<link rel="stylesheet" type="text/css" href="' + strRoot + '/style/common/safari.css">');
}

// ------------------------------------ form validation functions

function isBlank(v) {
	if ((v.length>0) && (v!=" ")) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(v) {
    	if (isNaN(v.replace(/ /g,""))) {
    		return false;
    	}
    	else return true;
}

function isEmail(v) {
	at=v.indexOf('@');
	dot=v.lastIndexOf('.');
	end=v.length;
	subone=v.substring(at+1,dot);
	domainbeforedot=subone.length;
	subtwo=v.substring(dot+1,end);
	domainafterdot=subtwo.length;
   	if (v.indexOf ('@',0) == -1 || v.indexOf ('.',0) == -1  || domainbeforedot < 1 || domainafterdot < 2 || subone.indexOf ('.',0) == 0) {
      		return false;
	}
   	else {
   		return true;
   	}
}

function isPostcode(v) {
 	size = v.length;
  	//Strip leading spaces
 	while (v.slice(0,1) == " ") {
 		v = v.substr(1,size-1);
 		size = v.length;
 	}
  	//Strip trailing spaces
	while(v.slice(size-1,size)== " ") {
		v = v.substr(0,size-1);
		size = v.length;
	}
 	if (size < 6 || size > 8){
 		//Code length rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(0)))){
 		//leftmost character must be alpha character rule
   		return false;
  	}
 	if (isNaN(v.charAt(size-3))){
 		//first character of inward code must be numeric rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-2)))){
 		//second character of inward code must be alpha rule
   		return false;
  	}
 	if (!(isNaN(v.charAt(size-1)))){
 		//third character of inward code must be alpha rule
   		return false;
  	}
 	count1 = v.indexOf(" ");
 	count2 = v.lastIndexOf(" ");
 	if (count1 != count2){
 		// only one space rule
  		return false;
  	}
	return true;
}

function chkRadio(f, btnName) {
	var btn = f[btnName]
	var valid

	valid = f[btnName].checked;  // checks the status of a single radio button

	for (var x = 0;x < btn.length; x++)
	{
		valid = btn[x].checked
		if (valid) { break }
	}
	if(!valid)
	{
		return false;
	}
	else
	{
		return true;
	}
}


function chkFieldDefaultText(v, defaultValue, action) {
	if (action == "blur") {
		if (isBlank(v.value)) {
			v.value = defaultValue
		}
	}
	if (action == "click") {
		if (v.value == defaultValue) {
			v.value = ""
		}
	}
}

// ------------------------------------ popup functions

function QAS_Popup(fname) {
	QAS_PRO = window.open("/qas/popup.asp?CountryCodeName=GBRUnited+Kingdom&fname="+fname,"QAS_PRO", "scrollbars=yes,resizable=yes,width=600,height=450");
}

function openEmailFriend(url) {
	window.open("/sendemail/sendemail_form.asp?page=" + url,'sendemail','top=140,left=140,width=390,height=460,resizable=0,scrollbars=0,toolbar=0,directories=0,status=0,menubar=0')
}

function openDetail(url) {
	window.open(url,'detailImages','top=140,left=140,width=390,height=460,resizable=0,scrollbars=0,toolbar=0,directories=0,status=0,menubar=0')
}

function openZoom(url,image) {
	image = image.substring(image.length,image.lastIndexOf("/")+1)
	window.open(url+image,'imageZoom','width=700,height=400,resizable=0,scrollbars=0,toolbar=0,directories=0,status=0,menubar=0')
}


// ------------------------------------



function validateTitleBarKeywordSearch(f){
	if ((f.keyword.value == '') || (f.keyword.value == 'Keyword/Product Ref.')) {
		alert('You must enter a keyword to search for');

		return false;
	}
}

function validateKeywordSearch(f){

}

function openContentPopup(url) {

	window.open(url,'content','top=100,left=100,width=600,height=450,resizable=0,scrollbars=1,toolbar=0,directories=0,status=0,menubar=0');

}

function validateSearch(f) {
	if ((f.keyword.value == "Enter Keyword") || isBlank(f.keyword.value) ) {
		alert("Please enter a keyword or a product code");
		return false;
	}
	return true;
}

function ValidateFPForm(f) {
	if (isBlank(f.EmailAddress.value)) {
		alert("You must enter your email address");
		return false;
	}
	if (!isEmail(f.EmailAddress.value)) {
		alert("The email address entered is not valid")
		return false;
	}
}


function showHide(obj, visibility) {
	if (document.getElementById){
    		document.getElementById(obj).style.display = visibility;
	}
	else if (document.layers) {
		document.obj.visibility = visibility;
	}
}

function validate(f) {
	if(f.BrandCode){
		if ((f.BrandCode.value == '#') && (isBlank(f.PriceLow.value)) && (isBlank(f.PriceHigh.value)) && (f.ListOrder.value == '')) {
			alert('No criteria selected');
			return false;
		}
	}
	if ((!isBlank(f.PriceLow.value)) && (!isNumber(f.PriceLow.value))) {
		alert('The low price range must be numeric');
		f.PriceLow.focus();
		return false;
	}
	if ((!isBlank(f.PriceHigh.value)) && (!isNumber(f.PriceHigh.value))) {
		alert('The high price range must be numeric');
		f.PriceHigh.focus();
		return false;
	}
}
