var ie = document.all;
var ob = document.getElementById && !ie
var Old = 'Welcome';
var New = '';
var Count = 100;
var Direction = 'Down';
var Locked =  'No';
var oldThumb = 'XetalWeb';
var oldPos = 0;
var posLocked =  'No';
var oldSection = 'Web';
var sectionLocked = 'No';
var urlCount = 0;
var urlList = new Array('XetalWeb','Add3Web','ZoobioticWeb','AspirationalHomesWeb','GlobalBoardersWeb','HarrisBassettWeb','ClickManagewareWeb','FireProtectionServicesEco','LovelyYouEco','BytemartEco','WalesEnvironmentTrustEma','BytestockEma','ZoobioticFacilityInf','ZoobioticLifecycleInf', 'ZoobioticApplicationInf', 'Map'); 
var brochureExists = 'Yes';
var enquiryExists = 'Yes';
var xmlHttp	= null;
var update = '';

/*******************************************************************************************/

function fadeOut(New) {
	if (Old != New) {
		if (Locked == 'No') {
			Locked = 'Yes';
		}
		if ((Count > 0) && (Direction == 'Down')) {
			if (Old == 'Services' && document.getElementById('BrochureForm').style.display == 'block') {
				closeBrochure();
			}
			Count = Count - 15;
			document.getElementById(Old+'Section').style.opacity = Count/100;
			document.getElementById(Old+'Section').style.filter = 'alpha(opacity=' + Count + ')';
			setTimeout("fadeOut('" + New + "')", 10);
		} else {
			if (enquiryExists == 'No') {
				changePage('enquiry-form.htm','EnquiryForm');
				enquiryExists =='Yes';
			}
			if (brochureExists == 'No') {
				changePage('brochure-form.htm','BrochureForm');
				brochureExists =='Yes';
			}
			Direction = 'Up';
			document.getElementById(Old+'Section').style.display = 'none';
			document.getElementById(Old+'Tab').className = '';
			document.getElementById(New+'Tab').className = 'Selected';
			document.location.href = '#'+New+'Section';
			setTimeout("fadeIn('" + New + "')", 10);
		}
	}
}

function fadeIn(New) {
	if ((Count < 100) && (Direction == 'Up')) {
		document.getElementById(New+'Section').style.display = 'block';
		Count = Count + 15;
		document.getElementById(New+'Section').style.opacity = Count/100;
		document.getElementById(New+'Section').style.filter = 'alpha(opacity=' + Count + ')';
		setTimeout("fadeIn('" + New + "')", 10);
	} else {
		if (New == 'Contact') {
			document.getElementById('EnquiryName').focus();
		}
		if (!ob) {
				document.getElementById(New+'Section').style.removeAttribute('filter');
		}
		Direction = 'Down';
		Old = New;
		Locked = 'No';
	}
}

/*******************************************************************************************/

function openBrochure() {
		document.getElementById('BrochureForm').style.display = 'block';
		setTimeout('document.getElementById(\'BrochureName\').focus()', 50);
}

function closeBrochure() {
		document.getElementById('BrochureForm').style.display = 'none';
		changePage('brochure-form.htm','BrochureForm');
		document.location.href='#ServicesText';
}

/*******************************************************************************************/

function changeCase(s,p,c) {
	posLocked = 'Yes';
	if(oldPos < p) {
		oldPos = oldPos + 90;
		document.getElementById(s + 'Panel').style.left = '-' + oldPos + 'px';
		setTimeout('changeCase(\'' + s + '\',' + p + ',\'' + c + '\')', 5);
	} else if(oldPos > p) {
		oldPos = oldPos - 90;
		document.getElementById(s + 'Panel').style.left = '-' + oldPos + 'px';
		setTimeout('changeCase(\'' + s + '\',' + p + ',\'' + c + '\')', 5);
	} else {
		posLocked = 'No';
		document.location.href = '#' + c + 'Case';
		document.getElementById(oldThumb + 'Thumbnail').className = '';
		oldThumb = c + s;
		document.getElementById(c + s + 'Thumbnail').className = 'Selected';
	}
}

/*******************************************************************************************/

function showPortfolio(s,t) {
	if (s != oldSection) {
		document.getElementById(oldSection + 'Panel').style.display = 'none';
		document.getElementById(oldSection + 'Thumbnails').style.display = 'none';
		document.getElementById(oldSection + 'Tab').className = '';
		document.getElementById(s + 'Panel').style.display = 'block';
		document.getElementById(s + 'Thumbnails').style.display = 'block';
		document.location.href = '#' + s + 'Thumbnails';
		document.getElementById(s + 'Tab').className = 'Selected';
		document.getElementById(oldThumb + 'Thumbnail').className = '';
		oldPos = 0
		document.getElementById(s + 'Panel').style.left = '0px';
		oldSection = s;
		document.getElementById(t + s + 'Thumbnail').className = 'Selected';
		oldThumb = t + s;
	}
}

/*******************************************************************************************/

function openWin(u) {
	window.open(u);
}

function replaceURLs() {
	if (urlCount < 16) {
		document.getElementById(urlList[urlCount] + 'URL').href = 'javascript:openWin(\'' + document.getElementById(urlList[urlCount] + 'URL').href + '\')';
		urlCount = urlCount + 1;
		replaceURLs()
	}
}

/*******************************************************************************************/

function emailCheck(el) {
	var emailStr = el;
	var emailPat = /^[\'_\.0-9A-Za-z-]+@([0-9A-Za-z-]+[0-9A-Za-z-\.]+)\.([A-Za-z]){2,4}$/;
	var email = emailStr.toLowerCase();
	var matchArray = email.match(emailPat);
	if (matchArray==null) {
		return false;
	}
	return true;
}

function validateBrochure() {
	if (document.getElementById('BrochureName').value == '') {
		alert('Please enter your name');
		document.getElementById('BrochureName').focus();
		return false;
	} else if (document.getElementById('BrochureCompany').value == '') {
		alert('Please enter your company name');
		document.getElementById('BrochureCompany').focus();
		return false;
	} else if (emailCheck(document.getElementById('BrochureEmail').value) == false) {
		alert('Please enter a valid email address');
		document.getElementById('BrochureEmail').focus();
		return false;
	} else if (document.getElementById('BrochureAddress').value == '') {
		alert('Please enter your address');
		document.getElementById('BrochureAddress').focus();
		return false;
	} else if (document.getElementById('BrochurePostcode').value == '') {
		alert('Please enter your postcode');
		document.getElementById('BrochurePostcode').focus();
		return false;
	} else {
		brochureExists = 'No';
		changePage('nospam-brochure.php?name=' + document.getElementById('BrochureName').value + '&company=' + document.getElementById('BrochureCompany').value + '&email=' + document.getElementById('BrochureEmail').value + '&telephone=' + document.getElementById('BrochureTelephone').value + '&address=' + document.getElementById('BrochureAddress').value.replace(/\n/g,', ') + '&postcode=' + document.getElementById('BrochurePostcode').value,'BrochureForm');
	}
	return false;
}

function validateEnquiry() {
	if (document.getElementById('EnquiryName').value == '') {
		alert('Please enter your name');
		document.getElementById('EnquiryName').focus();
		return false;
	} else if (document.getElementById('EnquiryCompany').value == '') {
		alert('Please enter your company name');
		document.getElementById('EnquiryCompany').focus();
		return false;
	} else if (document.getElementById('EnquiryEmail').value == '') {
		alert('Please enter your email address');
		document.getElementById('EnquiryEmail').focus();
		return false;
	} else if (emailCheck(document.getElementById('EnquiryEmail').value) == false) {
		alert('Please enter a valid email address');
		document.getElementById('EnquiryEmail').focus();
		return false;
	} else if (document.getElementById('EnquiryTelephone').value == '') {
		alert('Please enter your telephone number');
		document.getElementById('EnquiryTelephone').focus();
		return false;
	} else if (document.getElementById('EnquiryEnquiry').value == '') {
		alert('Please enter your enquiry');
		document.getElementById('EnquiryEnquiry').focus();
		return false;
	} else {
		enquiryExists = 'No';
		changePage('nospam-enquiry.php?name=' + document.getElementById('EnquiryName').value + '&company=' + document.getElementById('EnquiryCompany').value + '&email=' + document.getElementById('EnquiryEmail').value + '&telephone=' + document.getElementById('EnquiryTelephone').value + '&enquiry=' + document.getElementById('EnquiryEnquiry').value,'EnquiryForm');
	}
	return false;
}

/*******************************************************************************************/

function changePage(url,area) {
	update = area;
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Browser does not support HTTP Request")
		return false;
	}
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function GetXmlHttpObject() {
	var objXMLHttp = null;
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp
}

function stateChanged() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') {
		document.getElementById(update).innerHTML = xmlHttp.responseText;
	}
}