var FE;
$(document).ready(function(){
	//IE Drop downs
	if (window.attachEvent) window.attachEvent("onload", sfHover);
	//External Links
	externalLinks();
	//Scripts Class
	//FE = new CFE();
});

function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && 
			(anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow") )
			anchor.target = "_blank"; 
	} 
}
sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function GetMap(url, name, height, width) {							
	var bh = screen.availHeight - 30;
	var bw = screen.availWidth - 10;
	var xw = (bw - 200) / 2;
	var yh = (bh - 200) / 2;
	window.moveTo(xw, yh);
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (window.screen)  {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		//str += ",scrollbars";
	}
	win = window.open(url, name, str);
}

function GoogleMap(){
	var myLatlng = new google.maps.LatLng(51.74994,-2.22610);
	var myOptions = {
		zoom: 14,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);
	var contentString = '<div id="mapinfo">'+
		'<h3>Stratford Park Leisure Centre</h3>'+
		'<p>Stratford Road<br />Stroud<br />Gloucestershire<br />GL5 4AF</p>'+
		'<p>Tel: 01453 766771</p>'+
		'</div>';
	var infowindow = new google.maps.InfoWindow({
		content: contentString
	});
	var marker = new google.maps.Marker({
		position: myLatlng,
		map: map,
		title:"Stratford Park Leisure Centre"
	});
	infowindow.open(map,marker);	
};

function ViewSquad(FixtureID){
	$('tr.squadtable').hide();
	$('#FixtureSquad'+FixtureID+' .squad').html('Loading...');
	$('#FixtureSquad'+FixtureID).show();
	$.ajax({
		type : 'get',
		url : '/includes/squadtable.asp?FixtureID='+FixtureID,
		success : function(html){
			$('#FixtureSquad'+FixtureID+' .squad').html(html);
		},
		error : function(XMLHttpRequest,textStatus,errorThrown){
			//alert('Error loading squad table');
			$('#FixtureSquad'+FixtureID).hide();
			alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
		}
	});
};

/*function CFE() {
	var main = this;
	this.UpdatePPI = function() {
		$.ajax({
			type : 'get',
			url : '/account/products.asp?act=1',
			success : function(html) {
				$("#view").html(html);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+shorten(XMLHttpRequest.responseText,250));
			}
		});
	};
	this.AddToBasket = function() {
		$.ajax({
			type : 'post',
			url : '/account/Actions.asp?act=1',
			data : formData2QueryString(document.forms['frmAddBasket']),
			success : function(html) {
				//alert(html)
				main.UpdateBasketTotal();
				alert('Item added to basket');
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
				//what should we do in the event of an error
				alert('An error has occured.  Please try logging in and trying the operation again.');
			}
		})
	};
	this.UpdateBasketQuantity = function(BC,Q) {
		$.ajax({
			type : 'get',
			url : '/account/Actions.asp?act=2&BC='+BC+'&Q='+Q,
			success : function(html) {
				window.location='/account/basket.asp';
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
				//what should we do in the event of an error
				//alert('An error has occured.  Please try logging in and trying the operation again.');
			}
		});
	};
	this.RemoveFromBasket = function(BC) {
		$.ajax({
			type : 'get',
			url : '/account/Actions.asp?act=3&BC='+BC,
			success : function(html) {
				window.location='/account/basket.asp';
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
				//what should we do in the event of an error
				//alert('An error has occured.  Please try logging in and trying the operation again.');
			}
		});
	};
	this.Confirm = function(Message) {
		if(confirm(Message)) {
			return true;
		} else {
			return false;
		}
	};
	this.UpdateBasketTotal = function() {
		$.ajax({
			type : 'get',
			url : '/account/Actions.asp?act=5',
			success : function(html) {
				$('#BasketTotal').html(html);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
				//what should we do in the event of an error
				//alert('An error has occured.  Please try logging in and trying the operation again.');
			}
		});
	};
	this.LoadAddress = function(a,id) {
		$.ajax({
			type : 'get',
			url : '/account/Actions.asp?act=6&a='+a+'&i='+id,
			success : function(html) {
				$('#'+id).html(html);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(textStatus+'\n\n'+XMLHttpRequest.responseText);
				//what should we do in the event of an error
				//alert('An error has occured.  Please try logging in and trying the operation again.');
			}
		});
	};
	this.CopyAddress = function(FromID, ToID) {
		var fieldArray = Array('Title','Firstname','Surname','Address1','Address2','Address3','Town','County','Country','Postcode','Phone','CountryID');
		for (var field in fieldArray) {
			$('#'+ToID+fieldArray[field]).val($('#'+FromID+fieldArray[field]).val());
		}
	};
	this.UpdateAS = function() {
		var output = '';
		var updateArray = $('#ALUpdateList').val().split(',');
		for (var i in updateArray) {
			if(updateArray[i].toString() != '') {
				output += $('#'+updateArray[i]+' option:selected').text()+', '
			}
		}
		output = output.slice(0, -2)
		$('#ProdIdentifier').html(output);
	};
}*/

function shorten(s, outputLength) {
	return s.substr(s.length-outputLength);
}

function formData2QueryString(docForm) {

	var strSubmitContent = '';
	var formElem;
	var strLastElemName = '';
	
	for (i = 0; i < docForm.elements.length; i++) {
		
		formElem = docForm.elements[i];
		switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				strSubmitContent += formElem.name + '=' + escape(formElem.value).replace(/\+/g,'%2B') + '&'
				break;
				
			// Radio buttons
			case 'radio':
				if (formElem.checked) {
					strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
				}
				break;
				
			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Continuing multiple, same-name checkboxes
					if (formElem.name == strLastElemName) {
						// Strip of end ampersand if there is one
						if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
							strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
						}
						// Append value as comma-delimited string
						strSubmitContent += ',' + escape(formElem.value);
					}
					else {
						strSubmitContent += formElem.name + '=' + escape(formElem.value);
					}
					strSubmitContent += '&';
					strLastElemName = formElem.name;
				}
				break;
				
		}
	}
	
	// Remove trailing separator
	strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
	return strSubmitContent;
}
