/* Member logo fader */

var opacity = 101;
var intval = 0;
var picno = -1;
var numpics = 8;
var fadein = 2;


var picarray = new Array('dimplex.gif', 'gewiss.gif', 'heatrae.gif', 'manrose.gif', 'marshall.gif', 'mem.gif', 'mita.gif', 'ventaxia.gif', 'bg.gif');

var linkarray = new Array('members.asp', 'members.asp', 'members.asp', 'members.asp', 'members.asp', 'members.asp', 'members.asp', 'members.asp', 'bg.gif');

function rotate_pic() {
	if(opacity > 100) {
		(picno >= numpics) ? picno = 0 : picno++;
		fadein == 1 ? fadein = 2 : fadein = 1;
		var r = $('img-' + fadein);
		r.src = 'assets/ir/' + picarray[picno];
		$('lnk-' + fadein).href = linkarray[picno];
		opacity = 0;
		window.clearInterval(intval);
		intval = window.setInterval('setfade()',1500);
	} else {
		fader();
	}
}

function setfade() {
	window.clearInterval(intval);
	intval = window.setInterval('rotate_pic()',30);
}

function fader() {
	var obj, op;
	opacity += 1;
	for(var i = 1; i < 3; i++)
	{
		obj = $('img-' + i);
		i == fadein ? op = opacity : op = (100 - opacity);
		dofade(obj, op);

	}
} // end of fader()


function dofade(obj, op) {
	obj.style.MozOpacity = '0';
	if (obj.filters)  {
		obj.style.filter="alpha(opacity=0)";
		obj.filters.alpha.opacity = op;
	}

	if (obj.style.MozOpacity) 
		obj.style.MozOpacity = '' + op;

	if (obj.style.opacity) 
		obj.style.opacity = op/100 + '';

	if (obj.style.setProperty) 
		obj.style.setProperty('-khtml-opacity', op/100, null);

}

var curTab = "members";

function setTab(tabName) {
	curTab = tabName;
	var type = $('searchType');
	type.value = tabName;
}

function switchTab(tabThis) {
	var tab = $(curTab);
	var newTab = $(tabThis);
	var newImg = $(tabThis+'Img');
    var img = $(curTab+'Img');
	var type = $('searchType');
	type.value = tabThis;
	tab.className = "na";
	newTab.className = "selected";
	img.style.display = "none";
	newImg.style.display = "";
	curTab = tabThis;
}

function switchPic(switchTo, regionName) {
	var overlay = $('overlay');
	overlay.src = '/content_graphics/'+switchTo+'.gif';
	var regionEl = $('mapLoc');
	regionEl.value = regionName;
}

function switchPic2(switchTo, regionName) {
	var overlay = $('mapRO');
	overlay.src = '/content_graphics/map/'+switchTo+'.gif';
	//var regionEl = document.getElementById('mapLoc');
	//regionEl.value = regionName;
}

function doSearch(searchType) {
	var method = $('searchBy');
	method.value = searchType;
	document.formSearch.submit();
}

function doGroupSearch(groupName) {
	var method = $('searchBy');
	if (method) {
		method.value = "group";
		var input = $('groupName');
		if (input) {
			input.value = groupName;
			document.formSearch.submit();
		}
	}
}

function finishSearch() {
	var uniqueField = $('unique');
	var uniqueData = $('uniqueRows');

	if (uniqueField) {
		uniqueField.innerHTML = uniqueData.value;
	}

	for (i = 1; i < uniqueData.value + 1 ;i++) {
		
		var group = $('group'+i);
		if (group) {
			var viewRow = $('view'+i);
			if (viewRow)	{
				viewRow.style.display = "";
			}
			if (uniqueData.value == 1) {
				showStores('group'+i, 'view'+i);
			}
		}
	}
}

function showStores(groupName, textID) {
	var group = $(groupName);
	if (group)
	{
		if (group.style.display == "")
		{
			group.style.display = "none";
		} else {
			group.style.display = "";
		}
		var textBlock = $(textID);
		if (textBlock)
		{
			if (group.style.display == "")
			{
				textBlock.innerHTML = "Hide all branches";
			} else {
				textBlock.innerHTML = "View all branches";
			}
		}
	}
}

function mapSearch() {
	var regionEl = $('mapLoc');
	if (regionEl.value == 'NONE')
	{
		return false;
	} else {
		document.mapForm.submit();
	}
}

var lastRegion = ""

function showRegion(regionImg, regionID) {
	var ER = $('mapER');
	if (ER)
	{
		ER.src = '/content_graphics/map/'+regionImg;
		new Effect.Appear('mapER');
	}
	var RO = $('mapRO');
	if (RO)
	{
		RO.style.display = "none";
		RO.src = '/content_graphics/map/base_fade.gif';
		new Effect.Appear('mapRO', {duration: 0.6});
	}
	var lastPinGroup = $('reg_'+lastRegion);
	if (lastPinGroup)
	{
		lastPinGroup.style.display = "none";
	}
	var curPinGroup = $('reg_'+regionID);
	if (curPinGroup)
	{
		new Effect.Appear(curPinGroup, {delay: 0.5});
		lastRegion = regionID;
	}
	new Effect.Appear('fullMap', {duration: 0.4});
}

var lastPinID = 0;
var infoTimer;

function pinOut() {
	clearInterval(infoTimer);
	var lastPin = $('pin'+lastPinID);
	if (lastPin)
	{
		if (lastPin.src.indexOf('multi') > 0)
		{
			lastPin.src = '/content_graphics/map/pin_multi.gif';
		} else {
			lastPin.src = '/content_graphics/map/pin.gif';
		}
	}
	lastPinID = 0;
}

function pinOver(pinID) {
	var lastPin = $('pin'+lastPinID);
	if (lastPin)
	{
		if (lastPin.src.indexOf('multi') > 0)
		{
			lastPin.src = '/content_graphics/map/pin_multi.gif';
		} else {
			lastPin.src = '/content_graphics/map/pin.gif';
		}
	}
	lastPinID = pinID;
	var curPin = $('pin'+pinID);
	if (curPin)
	{
		clearInterval(infoTimer);
		if (curPin.src.indexOf('multi') > 0)
		{
			curPin.src = '/content_graphics/map/pin_multi_ro.gif';
		} else {
			curPin.src = '/content_graphics/map/active_pin.gif';
		}
		infoTimer = setInterval('setInfo(\''+pinID+'\')',4000);
	}
}

function setInfo(pinID) {
	clearInterval(infoTimer);
	var branchDiv = $('branchContainer');
	if (branchDiv.style.display == "none")
	{
		new Effect.Appear('branchContainer');
	}
	var updateML = function(q) {
		var code = '';
		if (q.responseXML.documentElement.getElementsByTagName("status").item(0).firstChild.data == 'ok') {
			if (branchDiv)
			{
				for (i=0;i<=q.responseXML.documentElement.getElementsByTagName("memberName").length;i++)
				{
					var memberID = q.responseXML.documentElement.getElementsByTagName("memberID").item(i).firstChild.data;
					var memberName = q.responseXML.documentElement.getElementsByTagName("memberName").item(i).firstChild.data;
					code += '<table class="anew-results-table" cellspacing="0"><tr><th colspan="2"><img src="assets/bullet_th.gif" class="anew-results-bullet" alt="bullet" width="17" height="19" /> '+memberName+'</th><td class="viewBranches" align="right">&nbsp;</td></tr><tr><td class="anew-results-label">Address:</td><td class="inner">';
					if (q.responseXML.documentElement.getElementsByTagName("address1").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("address1").item(i).firstChild.data + ',';
					}
					if (q.responseXML.documentElement.getElementsByTagName("address2").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("address2").item(i).firstChild.data + ',';
					}
					if (q.responseXML.documentElement.getElementsByTagName("address3").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("address3").item(i).firstChild.data + ',';
					} 
					if (q.responseXML.documentElement.getElementsByTagName("town").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("town").item(i).firstChild.data + ',';
					}
					if (q.responseXML.documentElement.getElementsByTagName("county").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("county").item(i).firstChild.data + ',';
					}																	
					if (q.responseXML.documentElement.getElementsByTagName("postcode").item(i).firstChild.data != 'NULLLLUN') {
						code += q.responseXML.documentElement.getElementsByTagName("postcode").item(i).firstChild.data + ',';
					}
					code += '</td><td class="anew-results-table-logo" rowspan="6"><div class="anew-mmbr-logo"><img src="members_logos/'+q.responseXML.documentElement.getElementsByTagName("graphic").item(i).firstChild.data+'" alt="'+memberName+'" title="'+memberName+'" /></div></td></tr>';
					if (q.responseXML.documentElement.getElementsByTagName("phone").item(i).firstChild.data != 'NULLLLUN') {
						code += '<tr><td class="anew-results-label">Tel:</td><td class="inner">'+q.responseXML.documentElement.getElementsByTagName("phone").item(i).firstChild.data+'</td></tr>';
					}
					if (q.responseXML.documentElement.getElementsByTagName("fax").item(i).firstChild.data != 'NULLLLUN') {
						code += '<tr><td class="anew-results-label">Fax:</td><td class="inner">'+q.responseXML.documentElement.getElementsByTagName("fax").item(i).firstChild.data+'</td></tr>';
					}
					if (q.responseXML.documentElement.getElementsByTagName("website").item(i).firstChild.data != 'NULLLLUN') {
						code += '<tr><td class="anew-results-label">Web:</td><td class="inner"><a href="http://'+q.responseXML.documentElement.getElementsByTagName("website").item(i).firstChild.data+'" target="_blank">'+q.responseXML.documentElement.getElementsByTagName("website").item(i).firstChild.data+'</a></td></tr>';
					}
					code += '</table>';
					branchDiv.innerHTML = code;
				}
			}
		} else {
			alert('Sorry we could\'t retrieve that members details. Please try again.\n\nThe reason for this error is: '+q.responseText);
		}
	}
	var failureML = function(q) {
		alert('Sorry we could\'t retrieve that members details. Please try again.\n\nThe reason for this error is: '+q.statusText+' '+q.status);
	}
	var pin = $('pin'+pinID);
	if (pin.src.indexOf('multi') > 0)
	{
		var a = {
			mlFunc: 'getMembers',
			mlID: pinID
		};
	} else {
		var a = {
			mlFunc: 'getMember',
			mlID: pinID
		};
	}
	var params = $H(a);
	new Ajax.Request('/getmember.asp', {parameters: params.toQueryString(), onSuccess:updateML, onFailure:failureML});	
}

function closeRegion() {
	var ER = $('mapER');
	if (ER)
	{
		new Effect.Fade('mapER');
	}
	var RO = $('mapRO');
	if (RO)
	{
		$('mapRO').src = '/content_graphics/map/clear.gif';
		new Effect.Appear('mapRO', {delay: 0.7});
	}
	var lastPinGroup = $('reg_'+lastRegion);
	if (lastPinGroup)
	{
		new Effect.Fade(lastPinGroup, {duration: 0.4});
		lastPinGroup.style.display = "none";
	}
	new Effect.Fade('fullMap', {duration: 0.4});
	new Effect.Fade('branchContainer', {duration: 0.4});
}

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;
}

function showError(fld, msg)
{
	fld.focus();
	alert(msg);
	return false;
}

function check_contact_form()
{
	var frm = $('anew-contact-form');
	var flds = new Array('cont_fname', 'cont_lname', 'cont_email');
	var fldnames = new Array('your first name', 'your last name', 'your email address');
	if (!isEmailFormatCorrect(trim(frm.cont_email.value)))
		return showError($('cont_email'), 'The email address you have entered is not in a valid format');
	for (var i = 0; i < flds.length; i++ )
	{
		if(trim(frm[flds[i]].value) == '')
			return showError(frm[flds[i]], ('Please enter ' + fldnames[i]));
	}
	return true;
}
