// ------------------------------------ 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 validateTitleBarKeywordSearch(f){
	if ((f.keyword.value == '') || (f.keyword.value == 'Product Code / Keyword')) {
		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 chkWebLinks(f) {
	if ((f.keyword.value == '') || (f.keyword.value == 'Enter Code')) {
			alert('You must enter a code to search for');
		return false;
	} else {
		return true;
	}
}

function emailSignup(f)
{

	if (!isEmail(f.email.value)) {
		alert("You must enter a valid email address");
		f.email.focus();
		return false;
	}
	else {
		window.open('', 'EmailSignUp', 'menu=no, toolbar=no, scroll=no, width=340, height=500, left=210, top=210');
		return true;
	}
}

var showProducts

function toggleDisplay() {
	if (showProducts == "TRUE") {
		showProducts = "FALSE";
	}
	else {
		showProducts = "TRUE";
	}
	switchDisplay(getElement("detailsBrief"),getElement("detailsExpanded"));
}

function switchDisplay(el1,el2){ 
	if(el1!=null){
		if(el1.style.display=="block" || el1.style.display==""){
			el1.style.display='none';
			el2.style.display='block';
			document.toggle.src=strRoot+'/images/buttons/basket_toggle_up.gif';
		} else {
			el1.style.display='block';
			el2.style.display='none';
			document.toggle.src=strRoot+'/images/buttons/basket_toggle.gif';
		}
	}
}

function getElement(id){
	if(document.all){
		el = document.all(id)
	} else if(document.getElementById){
		el = document.getElementById(id)
	} else {
		el = null
	}
	return el
}


function SendSearchRequest(strLayerId, strGroupCode) {	
	if (strGroupCode == ' : '){
		strResponse = '<select name="mainSearch2"><option value="">--- Model ---<option></select>'
	}
	else{
		var strURL, strResponse;
		strURL = strRoot + "/common/SendSearchRequest.asp";
		strURL += "?groupCode=" + strGroupCode;
		strResponse = sendXmlHttpRequest(strURL);

		strResponse = '<select name="mainSearch2" id="mainSearch2"><option value="">--- Model ---</option>'+strResponse+'</select>'
		
		// Write response to layer
	}
	MM_setTextOfLayer('test','',strResponse);

}

function chkDropdownSearch(f){
	if(f.mainSearch1.value ==" : " && isBlank(f.mainSearch2.value) && isBlank(f.mainSearch3.value))
	{	
		alert("Please enter your search criteria");
		return false;
	}
}

function chkSearch(f){

	if(f.keyword.value =="-- Enter Keyword --" || isBlank(f.keyword.value))
	{	
		alert("Please enter your search");
		return false;
	}
	f.newKeyword.value = f.keyword.value
}

function remove_noise(phrase)
{
//dimensionalizing variables
noisearray=new Array();
array_phrase=new Array();
array_pruned=new Array();

var noisearray=new Array ("0","1","2","3","4","5","6","7","8","9","$","a","about","after","all","also","an","and","another","any","are","as","at","b","be","because","been","before","being","between","both","but","by","c","came","can","come","could","d","did","do","does","e","each","else","f","for","from","g","get","got","h","had","has","have","he","her","here","him","himself","his","how","i","if","in","into","is","it","its","j","just","k","l","like","m","make","many","me","might","more","most","much","must","my","n","never","now","o","of","on","only","or","other","our","out","over","p","q","r","re","s","said","same","see","should","since","so","some","still","such","t","take","Test","than","that","the","their","them","then","there","these","they","this","those","through","to","too","u","under","up","use","v","very","w","want","was","way","we","well","were","what","when","where","which","while","who","will","with","would","x","y","you","your","z");
var array_pruned_length=0;
var counter=0;
var counter1=0;
var counter2=0;
var phrase1;
var length=0;
var noise_array_counter=0;
var search_phrase="";

//spliting the search phrase into an array
array_pruned=phrase.split(" ");
array_pruned_length=array_pruned.length;

for (counter=0;counter <array_pruned_length;counter++)
{
	counter2=0;
	counter1=0;
	for (counter1=0;counter1<noisearray.length;counter1++)
		{
			if(array_pruned[counter]==noisearray[counter1])
				{
	   				search_phrase= " " + array_pruned[counter] + "";
    					search_phrase_first_position= array_pruned[counter] + "";
					counter2 = phrase.indexOf(search_phrase,0);
					counter3 = phrase.indexOf(search_phrase_first_position,0);
					if(counter3<counter2)
						counter2=counter3;
					while (counter2>= 0)
						{
							temp_string=array_pruned[counter];
							length=temp_string.length;
							phrase = phrase.substr(0,counter2) + phrase.substr(counter2+length+1);
							counter2=phrase.indexOf(search_phrase,0);
  						}
				}
		}
}

return phrase;
}

function validateMediaCode(f,name) {
	if (isBlank(f.mediaCode.value)) {
		alert("Please enter a valid code");
		return false;
	}
	if (!isBlank(name)) {
		if(confirm('You have alreay entered a promotion code ' + name + ' If you continue you will overide this media code'))
		return true;
		else
		return false;
	}
}

function inputTextToUpper(obj) {
	var txtUpper 
	txtUpper = obj.value.toUpperCase();
	obj.value = txtUpper;
}

