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 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;
}

function Toggle(idclass){
	$(idclass).toggle();
}
function ShowProfile(id){
	$('li.profile').removeClass('active');
	$('div.profile').hide();
	$('#profile'+id).addClass('active');
	$('#profile'+id+' .profile').show();
}

function removeVal(input,value){
	var val = $('#'+input).val();
	if(val==value){
		$('#'+input).val('');
	}
}
