/*
----------------------------------------------------------
	Scion Solutions Ltd main JavaScript file.
	This file is included in all pages
	FileName:    generic.js
	Version:     1.00				07/10/2005
	Copyright Scion Solutions Ltd 1998 - 2005
	www.scion.co.uk
	Author: Ian Winstanley
----------------------------------------------------------
*/

/* Browser detection */
var NS = (document.layers) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var NEW = (document.getElementById) ? 1 : 0;
var IE4 = (IE && !NEW) ? 1 : 0;
var IE5 = (IE && NEW) ? 1 : 0;
var MAC = (navigator.appVersion.indexOf("Mac") != -1) ? 1 : 0;
var Opera = (navigator.userAgent.indexOf("Opera") != -1) ? 1 : 0;
var Firefox = (navigator.userAgent.indexOf("Firefox") != -1) ? 1 : 0;
var NS6 = (NEW && !IE && !Opera & !Firefox) ? 1 : 0;
//alert('NS:' + NS + '\nIE:' + IE + '\nNEW:' + NEW + '\nIE4:' + IE4 + '\nIE5:' + IE5 + '\nNS6:' + NS6 + '\nNS60:' + NS60 + '\nMAC:' + MAC + '\nOpera: ' + Opera + 'navigator.userAgent:' + navigator.userAgent);

window.name = 'scionMain'; // Generic window name

/* Element identification. Makes allowances for earlier browser that do not support getElementById */
function getElementById(id, windowName) {
	if (NEW) {
		return windowName ? windowName.document.getElementById(id) : document.getElementById(id);
	} else {
		return windowName ? windowName.document.all[id] : document.all[id];
	}
}

// Preload images
var myImages = new Array(); 
function preloadImages(imageName, imagePath) {
	for (var i in imageName) {
		myImages[imageName[i] + '_on'] = new Image();
		myImages[imageName[i] + '_on'].src  = imagePath + imageName[i] + '_on.gif';
		myImages[imageName[i] + '_off'] = new Image();
		myImages[imageName[i] + '_off'].src = imagePath + imageName[i] + '_off.gif';
	}
}

// Generic /images replace function
function imageChange(imgOb, imgReplace) {
	imgOb.src = myImages[imgReplace].src;
}

/* Loops through each of the stylesheets and then through each of the styles
	until the specified (by the array name) style object is returned as an array of arrays  */
function getStyleClass(className) {
	if (IE4 || IE5) {
		for (var s = 0; s < document.styleSheets.length; s++) {
			for (var r = 0; r < document.styleSheets[s].rules.length; r++) {
				if (document.styleSheets[s].rules[r].selectorText == '.' + className) {
          			return document.styleSheets[s].rules[r];
				}
			}
		}
	} else if (NEW) {
		for (var s = 0; s < document.styleSheets.length; s++) {
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++) {
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className) {
					return document.styleSheets[s].cssRules[r];
				}
			}
		}
	}
  return null;
}

/* Checks the length of the search phrase in the search box */
function checkLogin(frm) {
	var Msg = '';
	if (frm.username.value.length < 3) {
		Msg += 'You must enter a username of 3 characters or more\n';
	}
	if (frm.password.value.length < 6) {
		Msg += 'You must enter a password of 6 characters or more';
	}
	
	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

/* Clears the prompt text when a user focuses. Re-enters it if the search box is left empty */
function clearSearchText(fld, fldFocus) {
	if (fld.value == 'Enter search here' && fldFocus == 1) {
		fld.value = '';
	} else if (fld.value == '' && fldFocus == 0) {
		fld.value = 'Enter search here';
	}
}

/* Clears the prompt text when a user focuses. Re-enters it if the email box is left empty */
function clearEmailText(fld, fldFocus) {
	if (fld.value == 'Enter email here' && fldFocus == 1) {
		fld.value = '';
	} else if (fld.value == '' && fldFocus == 0) {
		fld.value = 'Enter email here';
	}
}

// Strips out non number characters
function checkForTelephone(fld) {
	var invalid = false, string = fld.value; 
	//Stripping out any invalid characters.
	for (var i=0, output='', valid="0123456789"; i<string.length; i++) {
		if (valid.indexOf(string.charAt(i)) != -1) {
			output += string.charAt(i);
		} else if (string.charAt(i) != ' ') {
			invalid = true;
		}
	}
	fld.value = output
	if (invalid == true) {
		return false;
	} else {
		return true;
	}
}

// Checks the email address
function checkForEmail(fld) {
	if (fld.value.search(/[\w\-_]+\@[\w\-_]+\.[\w\-_]+/) == -1) {
		return false;
	} else {
		return true;
	}
}

function checkNewsSubscription(fld) {
	if (!checkForEmail(fld)) {
		alert('Please a valid email address\n');
		return false;
	}
}


/*
	Checks the contact form form values
*/
function checkContactForm(frm) {
	Msg = '';
	for (i = 0; i < frm.mandatory.length; i++) {
		fieldLabelArray = frm.mandatory[i].value.split(/_/);
		fieldLabel = '';
		for (var j in fieldLabelArray) {
			fieldLabelArray[j] = fieldLabelArray[j].substring(0,1).toUpperCase() + fieldLabelArray[j].substring(1, fieldLabelArray[j].length);
			fieldLabel += fieldLabelArray[j] + ' ';
		}

		if (frm[frm.mandatory[i].value].value == '') {
			Msg += 'Please enter some text for "' + fieldLabel + '"\n';
		} else if (frm.mandatory[i].value.match(/name/) && frm[frm.mandatory[i].value].value.match(/[^a-zA-Z0-9-' ]+/) > 0) {
			Msg += 'Please enter only letters, space, commas or dashes for "' + fieldLabel + '"\n';
		}
	}
	if (Msg) {
		alert(Msg);
		return false;
	}
}


/* Checks that the values within the quotation form are suitable for submission */
function checkQuotationForm(frm) {
	var Msg = ''
	if (frm.name.value == '') {
		Msg += 'Please enter your name\n';
	}
	if (frm.email.value == '') {
		Msg += 'Please enter your email address';
	}

	if (Msg == '') {
		return true;
	} else {
		alert(Msg);
		return false;	
	}
}


// Check that the address details have been submitted correctly (for submission to WorldPay)
function checkAddressSubmit(frm) {
	var Msg = ''
	if  (frm.name.value == '') {
		Msg += 'Please enter your name\n';  
	}
	if (frm.email.value == '') {
		Msg += 'Please enter your email address\n';  
	} else if (!checkForEmail(frm.email)) {
		Msg += 'Please a valid email address\n';
	}
	if  (frm.address.value == '') {
		Msg += 'Please enter your address\n';  
	}
	if  (frm.town.value == '') {
		Msg += 'Please enter your town\n';  
	}
	if  (frm.county.value == '') {
		Msg += 'Please enter your county\n';  
	}
	if  (frm.postcode.value == '') {
		Msg += 'Please enter your postcode\n';  
	}

	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}


// opens a popup window based on either default or supplied arguments
var myWin, focusWin, genericWin;
function openWindow(url, winWidth, winHeight, windowName) {
var winWidth = winWidth ? winWidth : Opera ? 666 : 650;
var winHeight = winHeight ? winHeight : Opera ? 570 : 570;
var parms = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, height=' + winHeight + ',width=' + winWidth + ', left=0, top=0';
    if (windowName) {
    	genericWin = window.open(url, 'myWin', parms);    
    } else {
    	focusWin = window.open(url, 'myWin', parms);
    }
    if (focusWin) {
        focusWin.focus();
    }
}

/* Gets the current date and writes it to wherever the script is called from */
function getCurrentDate() {
	var mydate=new Date();
	var hours=mydate.getHours();
	var yr=mydate.getFullYear();
	var dy=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	if (daym<10) {
		daym="0"+daym;
	}
	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

	document.write(dayarray[dy]+ ' ' + daym + ' ' + montharray[month] + ' ' + yr);
}

/*
	Used for the Trust Logo generation
*/
var result = null;
var result2 = null;
function click2() {
	if (navigator.appName.indexOf("Explorer") > -1) {
		document.getElementById('imgShow').innerHTML = '<img src=\"'+result2+'\">';
		document.getElementById('st2').innerText = '<script language=\"JavaScript\">COT(\"'+result2+'\", \"SC2\", \"none\");<\/script>';
	} else {
		document.getElementById('imgShow').innerHTML = '<img src=\"'+result2+'\">';
		document.getElementById('st2').textContent = '<script language=\"JavaScript\">COT(\"'+result2+'\", \"SC2\", \"none\");<\/script>';
	}
}