// Clean nodeset
function removeEmpty(node) {
  if( !node ) node=document.body;
  for (var x = 0; x < node.childNodes.length; x++) {
    var child = node.childNodes[x];
    if ((child.nodeType == 3)&&(!/\S/.test(child.nodeValue))) {
      node.removeChild(node.childNodes[x]);
      x--;
    } else if (child.nodeType == 1) {
      removeEmpty(child);
    }
  }
}

// a rel="external"
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.target = '_blank';
	}
}

// IE Opacity fix for png background's
function alphaBackgrounds(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (i=0; i<document.all.length; i++){
		var bg = document.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg){
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
				document.all[i].style.backgroundImage = "url('/images/interface/blank.gif')";
			}
		}
	}
}

// Images disabled, but javascript still enabled?
function checkImages() {
	if (document.getElementById) {
		var x = document.getElementById('image-check').offsetWidth;
		if (x < 1) {
			// Disable the stylesheet
			var css = document.getElementById('css');
			css.innerHTML='';
		}
	}
}

// Default field values
function checkVal(obj){

	if(obj.value=='*'){
		obj.value='';
	}
	else if(obj.value==''){
		obj.value='';
	}
}

// Check profile form fields
function checkMember(obj){

	var field;
	var errors = new Array();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var termscheck = /iagree/g;

	// Check for null string in required fields
	for(var i=0;i<requiredIds.length;i++){
		field = document.getElementById(requiredIds[i][0]);
		if(requiredIds[i][0] == 'mpcat'){
			var count = 0;
			for(var j=0;j<field.options.length;j++){
				if(field.options[j].value && field.options[j].selected==true){
					count++;
				}
			}
			if(!count){
				errors.push('* \''+requiredIds[i][1]+'\' is a required field');
			}
		}
		else{
			if(!field.value.replace(/\s+/,'')){
				errors.push('* \''+requiredIds[i][1]+'\' is a required field');
			}
		}
	}

	// Check if passwords match
	if(document.getElementById('mpwd') && document.getElementById('mpwd2')){
		if(document.getElementById('mpwd').value != document.getElementById('mpwd2').value){
			errors.push('* Passwords do not match');
		}
	}

	// Check terms and conditions agreed to
	if(!document.getElementById('terms').checked){
		errors.push('* You have not agreed to our terms and conditions');
	}

	// Check email address is correctly formed
	if(!filter.test(document.getElementById('memail').value)){
		errors.push('* Email address does not appear to be valid');
	}

	if(errors.length){
		str = '';
		for(var err=0;err<errors.length;err++){
			str += errors[err]+"\n";
		}
		alert(str);
		return false;
	}
	else{
		return true;
	}
}

// Populate a field with a value
function populateField(id,v){
	if(obj = document.getElementById(id)){
		if(!obj.value.replace(/\s+/,'')){
			obj.value = v;
		}
	}
}

// Disable fields
function disableFields(id,disable){
	var div = document.getElementById(id);
	removeEmpty(div);

	for(var i=0;i<div.childNodes.length;i++){
		if(div.childNodes[i]){
			if(/textbox/.test(div.childNodes[i].className)){
				if(disable){
					div.childNodes[i].disabled = true;
					div.childNodes[i].style.background='transparent';
				}
				else{
					div.childNodes[i].disabled = false;
					div.childNodes[i].style.background='transparent';
				}
			}
		}
	}
}

// Get the turnover figures
function calcTurn(i){
	var subscription = document.getElementById('subscription');
	var join = document.getElementById('join-fee');
	var subtotal = document.getElementById('subtotal');
	var total = document.getElementById('total');

	if(i){
		var url = '/turnover.php?i='+i;

		xmlhttp.open("GET",url,true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4 && xmlhttp.status == 200){
				if(xmlhttp.responseText != "ok"){
					var rsp = xmlhttp.responseText;
					if(rsp){
						var fees = rsp.split('&');
						subscription.innerHTML = '&pound;'+fees[0];
						join.innerHTML = '&pound;'+fees[1];
						subtotal.innerHTML = '&pound;'+fees[2];
						total.innerHTML = '&pound;'+fees[3];
					}
				}
				else{
					subscription.innerHTML = '&pound;TBA';
					join.innerHTML = '&pound;TBA';
					subtotal.innerHTML = '&pound;TBA';
					total.innerHTML = '&pound;TBA';
				}
			}
		}
		xmlhttp.setRequestHeader('Accept','message/x-jl-formresult');
		xmlhttp.send(null);
	}
}

// Pagination
function changePg(p,u){
   if(p){
		var del = '?';
		if(u.match(/\?/)){
			del = '&';
		}
       location.href=u+del+'p='+p;
   }
} 

// Pagination htdig
function changePgHt(p,u){
	if(p){
		location.href=u+';page='+p;
	}
}

// Per Pg
function changePp(v,u){
	var today = new Date();
	var expire = new Date();
	var nDays = 1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	
	var del = '?';
	if(u.match(/\?/)){
		del = '&';
	}

	document.cookie = 'pp='+v+';expires='+expire.toGMTString();
	location.href=u+del+'p=1';
}

// Multiple onload functionality
function multipleOnload(){
	externalLinks();
	checkImages();
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
		//alphaBackgrounds();
	}
}

window.onload = multipleOnload;
