// the onload event handler that starts the fading.
function initTPS() {
	document.getElementById('top-dynamic-2').style.display = 'none';
	document.getElementById('top-dynamic-3').style.display = 'none';
	document.getElementById('top-dynamic-4').style.display = 'none';
	setInterval('swapFade()',wait);
}

// this array consists of the id attributes of the divs we wish to alternate between
var divs_to_slide = new Array('dynamic', 'dynamic2', 'dynamic3', 'dynamic4', 'dynamic5');
var divs_to_fade  = new Array('top-dynamic-1', 'top-dynamic-2', 'top-dynamic-3', 'top-dynamic-4');
// the starting index in the divs_to_slide array.  It should be set to the value of the div which doesn't have the CSS Display property set to "none"
var i = 0;
var j = 0;
// the number of milliseconds between swaps.
var wait = 10000;
// var wait2 = 5000;

// the function that performs effects
function swapFade() {
	//sliding panel
	Effect.MoveBy(divs_to_slide[i], 0, -1180, {duration: 2,  transition: Effect.Transitions.sinoidal});
	Effect.Fade(divs_to_slide[i], { duration:1, from:1.0, to:0.0, transition: Effect.Transitions.sinoidal});
	i++;
	if(i==divs_to_slide.length){
		i=0;
	}
	Effect.Appear(divs_to_slide[i], { duration:3, from:0.0, to:1.0, transition: Effect.Transitions.sinoidal });
	Effect.MoveBy(divs_to_slide[i], 0, 1180, {duration: 2, transition: Effect.Transitions.sinoidal});
	
	// PDQubes fade
	Effect.Fade(divs_to_fade[j], {duration:2.5, from:1.0, to:0.0, transition: Effect.Transitions.sinoidal});
	j++;
	if(j==divs_to_fade.length){
		j=0;
	}
	Effect.Appear(divs_to_fade[j], {duration:2.5, from:0.0, to:1.0, transition: Effect.Transitions.sinoidal});
}

// pops a new dependant window with chrome removed
function popwin(surl) {
	window.open(surl, 'newwin', 'dependent=yes,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no'); 
	return false;	
}

//Form Validation
function validateContactForm() {
	if (trim(document.getElementById('frm-ContFName').value) == '') {
		return showError(document.getElementById('frm-ContFName'), 'Please enter your first name.');
	}
	if (trim(document.getElementById('frm-ContLName').value) == '') {
		return showError(document.getElementById('frm-ContLName'), 'Please enter your last name.');
	}
	if (trim(document.getElementById('frm-ContEmail').value) == '') {
		return showError(document.getElementById('frm-ContEmail'), 'Please enter your email address.');
	}
	if (!isEmailFormatCorrect(document.getElementById('frm-ContEmail').value)) {
		return showError(document.getElementById('frm-ContEmail'), 'Please enter a valid email address.');
	}
	if (trim(document.getElementById('frm-ContMessage').value) == '') {
		return showError(document.getElementById('frm-ContMessage'), 'Please enter your enquiry.');
	}	
	document.contactform.submit();
	return true;
}

function validateTellAFriendForm(){
	if (trim(document.getElementById('frm-ContFriendName').value) == '') {
		return showError(document.getElementById('frm-ContFriendName'), 'Please enter your friend\'s name.');
	}
	if (trim(document.getElementById('frm-ContFriendEmail').value) == '') {
		return showError(document.getElementById('frm-ContFriendEmail'), 'Please enter your friend\'s email.');
	}
	if (!isEmailFormatCorrect(document.getElementById('frm-ContFriendEmail').value)) {
		return showError(document.getElementById('frm-ContFriendEmail'), 'Please enter a valid email address.');
	}	
	if (trim(document.getElementById('frm-ContYourName').value) == '') {
		return showError(document.getElementById('frm-ContYourName'), 'Please enter your name.');
	}
	if (trim(document.getElementById('frm-ContMessage').value) == '') {
		return showError(document.getElementById('frm-ContMessage'), 'Please enter your message.');
	}	
	document.tellafriendform.submit();
	return true;
	
}

function submit_search() {
	if (trim(document.getElementById('frm-Search').value) == '') {
		return showError(document.getElementById('frm-Search'), 'Please enter a word or phrase');
	}
	document.getElementById('search-form').submit();
	return true;
}

function onClickSearch() {
	var searchField = document.getElementById('frm-Search');
	if (searchField) {
		if (searchField.value == '') {
			searchField.value = 'Enter word or phrase';
		} 
		else { 
			if (searchField.value == 'Enter word or phrase') {
				searchField.value = '';
			}
		}
	}
}



// email checker
function isEmailFormatCorrect(strAddr) { 
	var ptn = /^[\w\d._%-]+@[\w\d._%-]+\.[\w\d._%-]{2,4}$/;
	return ptn.test(strAddr);
}

function trim(sVal) {
	while(sVal.charAt(sVal.length-1)==' ')
		sVal=sVal.substring(0,sVal.length-1);

	while(sVal.charAt(0)==' ')
		sVal=sVal.substr(1,sVal.length);

	return sVal;
}

// generic error message display
function showError(fld, msg)
{
	fld.focus();
	alert(msg);
	return false;
}

function checkQty(frm_id)
{
	this.frm_id = document.getElementById(frm_id);
	var bAtLeastOne = false;
	var bIsValid = false;
	var vItemPrice;
	var vItemPrice2;

	//alert(this.frm_id['qty1'].value);

	for (var i = 1; i <= this.frm_id['rowCounter'].value; i++)	{
		bIsValid = false;
		if (this.frm_id['qty' + i].value != '0') {
			bIsValid = true;
			bAtLeastOne = true;
		}
				
		if (Number(this.frm_id['qty' + i].value) < Number(this.frm_id['iMinOrder'].value) && bIsValid)	{
			return showError(this.frm_id['qty' + i] , 'The minimum order for this product is ' + this.frm_id['iMinOrder'].value);
		}

		if (bIsValid)	{
			var vPrice = new Array();
			//alert(this.frm_id['ps' + i].value);
			var vPrice = this.frm_id['ps' + i].value.split(',');
			
			// Last element is an overhead from the join procedure in ASP
			vPrice.length = vPrice.length - 1;

			for (var x = 0;x < vPrice.length; x++)
			{
				var vItemPrice = new Array();
				vItemPrice = vPrice[x].split('_');

				var vItemPrice2 = new Array();
							
				if (x < vPrice.length - 1) {
					vItemPrice2 = vPrice[x + 1].split('_');
				}

				//alert(x + ' Item Price: ' + vItemPrice[1]);
				//alert(x + ' Item Price2: ' + vItemPrice2[1]);

				if (this.frm_id['qty' + i].value >= vItemPrice[0] && this.frm_id['qty' + i].value < vItemPrice2[0] && vItemPrice[1] == -1)	{
					return showError(this.frm_id['qty' + i] , 'Please contact us for a price based on this quantity');
				}
			
			}
		}
	}
	if (!bAtLeastOne)
		return showError(this.frm_id['qty1'] , 'You have not given a quantity for any of the products above');

	this.frm_id.submit();

	return true;				
}


//Style Switcher functions
function styleSwitcher() 
{
	// id of the stylesheet element
	this.styleid = 'css760';
	// name of the global stylesheet
	this.smallstylesheetname = 'css/css760.css';
	this.smallstylesheetcsname = 'css/tpscasestudies760.css';
	// trigger width of site - below this we use the small stylesheet
	this.triggerwidth = 984;
	// null stylesheet name - use this to prevent errors associated with a null link value
	this.nullstylesheet = 'tps_null.css';

	// ENTRY POINT: sets the stylesheet to the defaults (above) unless styleId and sheetname are specified
	this.setStyleSheet = function (styleId, sheetname)
	{
		if(!styleId){
			this.switchSheet(this.styleid, this.smallstylesheetname);
		}
		else{
			this.switchSheet(styleId, sheetname);
		}
	};

	// set the specified stylesheet 
	this.switchSheet = function(styleId, sheetname)
	{
		var ssheet = getelem(styleId);
		var winWidth = this.getWidth();
		if (winWidth < this.triggerwidth)
			ssheet.setAttribute('href', sheetname);
		else
			ssheet.setAttribute('href', this.nullstylesheet);	
	};

	// return the width of the browser's window
	this.getWidth = function()
	{
		var ssheet;
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				return document.body.offsetWidth;
			}
			else
			{
				return window.innerWidth;
			}
		}
	};

} // end of styleSwitcher

// Helpers
function getDefaultText(id) { return getelem(('def-' + id)).innerHTML };
function getelem(elem) { return document.getElementById(elem); }

var switcher	= new styleSwitcher();
window.onresize = function() { switcher.setStyleSheet() };


//function to show/hide divs used in case study dynamic hover panels
//onmouseover="caseStudySwapper(id, 'show')"
//onmouseout  ="caseStudySwapper(id, 'hide')"
function caseStudySwapper(id, flag){
	if(flag == "show"){
		Effect.Appear(id + "img", { duration:0.2, queue:'parallel', from:0.0, to:1.0, transition: Effect.Transitions.sinoidal });
		Effect.Appear(id + "text", { duration:0.2, queue:'parallel', from:0.0, to:1.0, transition: Effect.Transitions.sinoidal });
	}
	else{
		Effect.Fade(id + "img", { duration:0.2, queue:'parallel', from:1.0, to:0.0, transition: Effect.Transitions.sinoidal });
		Effect.Fade(id + "text", { duration:0.2, queue:'parallel', from:1.0, to:0.0, transition: Effect.Transitions.sinoidal });
	}
	// $(id + 'img').style.display = $(id + 'text').style.display = flag == "show" ? 'block' : 'none';
}
