function CheckHandle(El) {
	var myAjax = new Ajax.Updater(
		{success: 'HandlerContainer'}, 
		"_ajax.checkhandle.inc.php", 
		{
		method: 'get', 
		parameters: "username="+El.value, 
		evalScripts: true
	});
}

function CheckEvent(ElVal) {
	var myAjax = new Ajax.Updater(
		{success: 'EventContainer'}, 
		"_ajax.checkevent.inc.php", 
		{
		method: 'get', 
		parameters: "event_id="+ElVal, 
		evalScripts: true
	});
}

function CheckMandatory(daField,ElNum,daFieldType,MustEqual) {
	if ( MustEqual === undefined ) {
	MustEqual='';
	}
	if(daFieldType=="string") {
		if($(daField).value=="") {
		ShowCross(ElNum);
		}
		else {
			if(MustEqual!='') {
				if($(daField).value != $(MustEqual).value) {
				ShowCross(ElNum);
				}
				else {
				ShowTick(ElNum);
				}
			}
			else {
			ShowTick(ElNum);
			}
		}
	}
	if(daFieldType=="email") {
		if(!TestEmail($(daField).value)) {
		ShowCross(ElNum);
		}
		else {
		ShowTick(ElNum);
		}
	}
	if(daFieldType=="number") {
		if(isNaN($(daField).value)) {
		ShowCross(ElNum);
		}
		else {
		ShowTick(ElNum);
		}
	}
	if(daFieldType=="select") {
		if($(daField).value=="") {
		ShowCross(ElNum);
		}
		else {
		ShowTick(ElNum);
		}
	}
	if(daFieldType=="radio") {
		var FieldName=$(daField).name;
		if(MM_findObj(FieldName).length>0) {
			var Missed=true;
			for(x=0;x<MM_findObj(FieldName).length;x++) {	
				if(MM_findObj(FieldName)[x].checked==true) {
				Missed=false;
				}
			}
			if(Missed==true) {
			ShowCross(ElNum);
			}
			else {
			ShowTick(ElNum);
			}
		}
	}
	if(daFieldType=="checkbox") {
		var FieldName=$(daField).name;
		if(MM_findObj($(daField).name).length>0) {
			var Missed=true;
			for(x=0;x<MM_findObj(FieldName).length;x++) {
				if(MM_findObj(FieldName)[x].checked==true) {
				Missed=false;
				}
			}
		}
			if(Missed==true) {
			ShowCross(ElNum);
			}
			else {
			ShowTick(ElNum);
			}
	}
}

function ShowTick(ElNum) {
//alert(ElNum);
$('em'+ElNum).style.backgroundImage="url('images/ico_tick.gif')";
}

function ShowCross(ElNum) {
$('em'+ElNum).style.backgroundImage="url('images/ico_missing.gif')";
}

function CheckContentForm(daForm) {
	var MissingFields=0;
	var ManFieldValue=daForm.mandatory.value;
	var ManFields=ManFieldValue.split(",");
	for(x=0;x<ManFields.length;x++) {
	FieldName=ManFields[x];
		// go through validation options	
		if(FieldName.indexOf("{email}")>1) {
			FieldName=FieldName.replace("{email}","");
			if(!TestEmail($F(FieldName))) {
			var MissingFields=MissingFields+1;
			}
		}
		else if(FieldName.indexOf("{number}")>1) {
			FieldName=FieldName.replace("{number}","");			
			if(isNaN($F(FieldName))) {
			var MissingFields=MissingFields+1;
			}
		}
		else if(FieldName.indexOf("{amount}")>1) {
			FieldName=FieldName.replace("{amount}","");			
			FieldValue=$F(FieldName).replace("$","");
			if(isNaN(FieldValue) || $F(FieldName)=="") {
			var MissingFields=MissingFields+1;
			}
		}
		else if(FieldName.indexOf("[]")>1) {
			FieldName=FieldName.replace("[]","");
			//alert(FieldName);
		}
		else {
			if(MM_findObj(FieldName).length>1) {
				if(MM_findObj(FieldName)[0].type=="radio" || MM_findObj(FieldName)[0].type=="checkbox") {
					var MissingFields=MissingFields+1;
					for(y=0;y<MM_findObj(FieldName).length;y++) {
						if(MM_findObj(FieldName)[y].checked==true) {
						var MissingFields=MissingFields-1;
						}
					}
				}
			}
			else {
				if(MM_findObj(FieldName).value=="") {
				var MissingFields=MissingFields+1;
				}
			}
		}
	}
		
	if(ManFields.length > 0) {
		if(MissingFields>0) {
		alert("Please fill out all fields marked with an asterisk.");
		return false;
		}
		else {
		return true;
		}
	}
	else {
	return true;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function CheckRiderDetails(daForm) {
	var MissingFields=0;
	var CheckForSelected=false;
	var ManFieldValue=daForm.mandatory.value;
	var ManFields=ManFieldValue.split(",");
	if(ManFields.length>0) {
		for(x=0;x<ManFields.length;x++) {
		FieldName=ManFields[x];
			if(ManFields[x].type=="radio") {
				var CheckForSelected=true;
				if(ManFields[x].checked==true) {
				return true;
				}
				else {
				MissingFields=1;
				}
			}
		}
	}
	if(ManFields.length > 0) {
		if(MissingFields>0) {
		alert("Please fill out all fields marked with an asterisk.");
		return false;
		}
		else {
		return true;
		}
	}
	else {
	return true;
	}
}

function CheckPayment() {
	if($F("card_number")=="") {
	alert('Please enter your credit card number in the field provided');
	return false;
	}
	if($F("card_name")=="") {
	alert('Please enter the name as shown on your credit card in the field provided');
	return false;
	}
	if(MM_findObj("card_type")[0].checked==false && MM_findObj("card_type")[1].checked==false && MM_findObj("card_type")[2].checked==false) {
	alert('Please select your credit card type');
	return false;
	}
	if($F("card_cvv")=="") {
	alert('Please enter the 3 or 4 digit security code found on the back of your credit card');
	return false;
	}
}

function CopyPostalDetails() {
	$("m_postal_street").value=$F("m_address_street");
	$("m_postal_suburb").value=$F("m_address_suburb");
	$("m_postal_pcode").value=$F("m_address_pcode");
	$("m_postal_state").selectedIndex=$("m_address_state").selectedIndex;
	
}
	
function TestEmail(e) {
	if(e) {
	p = e.indexOf('@');
	s = e.indexOf(';');
	d = e.lastIndexOf('.');
		if (p<1 || p==(e.length-1) || d<p || d==(e.length-1) || s>-1) {
		return false;
		}
	return true;
	}
return false;
}

function RemoveJunk(El,DataType) {
var V = El.value;
	if(DataType=="int") {
		if (/[^0-9\.]/g.test(V)) {
		El.value = V.replace(/[^0-9\.]/g, '');
		}
	}
	else if(DataType=="string") {
		if (/[^a-zA-Z\.]/g.test(V)) {
		El.value = V.replace(/[^a-zA-Z\.]/g, '');
		}
	}
	else if(DataType=="stringint") {
		if (/[^a-zA-Z0-9]/g.test(V)) {
		El.value = V.replace(/[^a-zA-Z0-9]/g, '');
		}
	}
	else if(DataType=="username") {
		if (/[^a-zA-Z0-9_\-]/g.test(V)) {
		El.value = V.replace(/[^a-zA-Z0-9_\-]/g, '');
		}
	}
	else if(DataType=="password") {
		if (/[^a-zA-Z0-9_!\$\.]/g.test(V)) {
		El.value = V.replace(/[^a-zA-Z0-9_!\$\.]/g, '');
		}
	}
	else if(DataType=="amount") {
		if (/[^0-9\.\$]/g.test(V)) {
		El.value = V.replace(/[^0-9\.\$]/g, '');
		}
	}
	else if(DataType=="nofloat") {
		if (/[^0-9\$]/g.test(V)) {
		alert('Sorry, only whole dollar amounts are permitted.');
		El.value = V.replace(/[^0-9\$]/g, '');
		}
	}
}

function OpenSendEmailPage(dPageUrl,dFieldName) {
	if(document.FormDisplay!=null) {
	var Len=document.FormDisplay.elements.length;
	}
	else {
	var Len=0;
	}
	var RecipientList="";
	if(Len>0) {
		for(x=0;x<Len;x++) {
			if(document.FormDisplay.elements[x].name==dFieldName+"[]") {
				if(document.FormDisplay.elements[x].checked==true) {
				var IsaGo=true;
				RecipientList += dFieldName + "[]=" + document.FormDisplay.elements[x].value + "&";
				}
			}
		}
	}
	
	document.location.href=dPageUrl + "&" + RecipientList;
}

function OpenPopupSponsors() {
	if(document.FormDisplay!=null) {
	var Len=document.FormDisplay.elements.length;
	}
	else {
	var Len=0;
	}
	var IsaGo=false;
	var SponsorList="";
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name=="donation_id[]") {
			if(document.FormDisplay.elements[x].checked==true) {
			var IsaGo=true;
			SponsorList += "donation_id[]=" + document.FormDisplay.elements[x].value + "&";
			}
		}
	}
	
	//if(IsaGo==true) {
		window.open('pop.emailsponsor.html?'+SponsorList,'popwin','width=550,height=500,scrollbars=1');
		//ActivateIbox('pop.emailsponsor.html?'+SponsorList);
	//}
	//else {
	//alert('Please use the checkboxes to the left to select the sponsors you wish to email');
	//return false;
	//}
}

function OpenPopupTeamSponsors() {
	if(document.FormDisplay!=null) {
	var Len=document.FormDisplay.elements.length;
	}
	else {
	var Len=0;
	}
	var IsaGo=false;
	var SponsorList="";
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name=="donation_id[]") {
			if(document.FormDisplay.elements[x].checked==true) {
			var IsaGo=true;
			SponsorList += "donation_id[]=" + document.FormDisplay.elements[x].value + "&";
			}
		}
	}
	
	//if(IsaGo==true) {
		window.open('pop.emailteamsponsor.html?'+SponsorList,'popwin','width=550,height=500,scrollbars=1');
		//ActivateIbox('pop.emailteamsponsor.html?'+SponsorList);
	//}
	//else {
	//alert('Please use the checkboxes to the left to select the sponsors you wish to email');
	//return false;
	//}
}

function OpenPopupMembers() {
	if(document.FormDisplay!=null) {
	var Len=document.FormDisplay.elements.length;
	}
	else {
	var Len=0;
	}
	var IsaGo=false;
	var SponsorList="";
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name=="member_id[]") {
			if(document.FormDisplay.elements[x].checked==true) {
			var IsaGo=true;
			SponsorList += "member_id[]=" + document.FormDisplay.elements[x].value + "&";
			}
		}
	}
	
	//if(IsaGo==true) {
		window.open('pop.emailmember.html?'+SponsorList,'popwin','width=550,height=500,scrollbars=1');
		//ActivateIbox('pop.emailmember.html?'+SponsorList);
	//}
	//else {
	//alert('Please use the checkboxes to the left to select the members you wish to email');
	//return false;
	//}
}

function ActivateIbox(dUrl) {
	if(dUrl) {
	var params = parseQuery('height=400&width=521');
	//showBG();
	showIbox('../'+dUrl+'?js=1','',params);	// show ibox
	window.onscroll = maintPos;
	window.onresize = maintPos;
	}
}

function GetEmailData(DataID) {
	
	var myAjax = new Ajax.Request(
		"_ajax.emaildata.inc.php", 
		{
			method: 'post', 
			parameters: "id="+DataID, 
			onComplete: showResponse
		});
}
function showResponse(originalRequest) {
	//alert(originalRequest.responseXML.getElementsByTagName("subject")[0].firstChild.data);
	var subject = originalRequest.responseXML.getElementsByTagName("subject")[0].firstChild.data;
	//alert(subject);
	var message = originalRequest.responseXML.getElementsByTagName("message")[0].firstChild.data;
	
	$('sp_email_subject').value=subject;
	$('sp_email_message').value=message;
	
	$('SendOutlookLink').innerHTML='&nbsp;&nbsp;<a href="mailto:?subject='+subject+'&body='+encode(message)+'"><img src="/images/bt_outlook.gif" border="0" alt="" /></a>';
}

function encode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_. ]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function showEmailSent(originalRequest) {
	var status=originalRequest.responseXML.getElementsByTagName("status")[0].firstChild.data;
	var message = originalRequest.responseXML.getElementsByTagName("message")[0].firstChild.data;
	if(status=="sent") {
	//PopContainer.innerHTML=message;
	PopContent.innerHTML="<h3>"+message+"</h3>";
	}
	else {
	PopContainer.innerHTML="<h3>Your emails were unable to be sent</h3>";
	}
}

function DoSendEmail(daForm) {
	var RecipientList=$F("sp_email_recipients");
	if($F("sp_email_recipients_add")!="") {
	RecipientList += "," + $F("sp_email_recipients_add");
	}
	var EmailSubject=$F("sp_email_subject").replace('&','-ampersand-');
	var EmailMessage=$F("sp_email_message").replace('&','-ampersand-');
	var EmailMessage=EmailMessage.replace('+','-space-');
	
	if(RecipientList.length>1 && EmailSubject.length>1 && EmailMessage.length>1) {
		//var myAjax = new Ajax.Request(
		//"_ajax.sendemail.inc.php", 
		//{
		//	method: 'post', 
		//	parameters: "EmailSubject="+EmailSubject+"&EmailMessage="+EmailMessage+"&RecipientList="+RecipientList, 
		//	onComplete: showEmailSent
		//});
		var myAjax = new Ajax.Updater(
			{success: 'PopContent'}, 
			"_ajax.sendemail.inc.php", 
			{
			method: 'post', 
			parameters: "EmailSubject="+EmailSubject+"&EmailMessage="+EmailMessage+"&RecipientList="+RecipientList, 
			onFailure: reportError,
			onComplete: hideIndicator
		});
	}
	else {
	alert("Please fill out all the fields for sending");
	return false;
	}
}

function DoSendFriendEmail(daForm) {
	var RecipientName=$F("sf_name_fr").replace('&','-ampersand-');;
	var RecipientList=$F("sf_email_fr");
	var EmailSubject="check out this site";
	var EmailMessage=$F("sf_message").replace('&','-ampersand-');;
	var FromEmail=$F("sf_email");
	var FromName=$F("sf_name");
	var PageUrl=$F("PageUrl");
	
	if(!TestEmail(FromEmail)) {
	alert("Please enter your email address in the field provided");
	return false;
	}
	else if(!TestEmail(RecipientList)) {
	alert("Please enter your friends email address in the field provided");
	return false;
	}	
	else {
	
		//var myAjax = new Ajax.Request(
		//"_ajax.sendemail.inc.php", 
		//{
		//	method: 'post', 
		//	parameters: "EmailSubject="+EmailSubject+"&EmailMessage="+EmailMessage+"&RecipientList="+RecipientList+"&FromEmail="+FromEmail+"&FromName="+FromName+"&PageUrl="+PageUrl+"&RecipientName="+RecipientName,
		//	onComplete: showEmailSent
		//});
		var myAjax = new Ajax.Updater(
			{success: 'PopContent'}, 
			"_ajax.sendemail.inc.php", 
			{
			method: 'post', 
			parameters: "EmailSubject="+EmailSubject+"&EmailMessage="+EmailMessage+"&RecipientList="+RecipientList+"&FromEmail="+FromEmail+"&FromName="+FromName+"&PageUrl="+PageUrl+"&RecipientName="+RecipientName, 
			onFailure: reportError,
			onComplete: hideIndicator
		});
		
	}
}

function reportError() {
	hideIndicator();
	alert("We were unable to send your emails.");
}

function CheckIsSelected(ElName,ElType) {
	var Len=document.FormDisplay.elements.length;
	var IsaGo=false;
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name==ElName) {
			if(document.FormDisplay.elements[x].checked==true) {
			var IsaGo=true;
			}
		}
	}
	
	if(IsaGo==true) {
	return true;
	}
	else {
	alert('Please use the checkboxes to the left to select the '+ ElType);
	return false;
	}
}

var CheckedStatus="off";

function doSelectAll(ElName) {
	var Len=document.FormDisplay.elements.length;
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name==ElName) {
			if(CheckedStatus=="off") {
				if(document.FormDisplay.elements[x].checked==false) {
				document.FormDisplay.elements[x].checked=true;
				}			
			}
			else {
			document.FormDisplay.elements[x].checked=false;
			}
		}
	}
	if(CheckedStatus=="off") {
	CheckedStatus="on";
	}
	else {
	CheckedStatus="off";
	}
}

function DoTeamAccess(AccessLevel) {
	var Len=document.FormDisplay.elements.length;
	var IsaGo=false;
	var MemberList="";
	for(x=0;x<Len;x++) {
		if(document.FormDisplay.elements[x].name=="member_id[]") {
			if(document.FormDisplay.elements[x].checked==true) {
			var IsaGo=true;
			MemberList += "member_id[]=" + document.FormDisplay.elements[x].value + "&";
			}
		}
	}
	if(AccessLevel!="") {
		//if(IsaGo==true) {
		document.location.href='team.edit.html?members&team_access='+AccessLevel+'&'+MemberList;
		//ActivateIbox('pop.teamaccess.html?team_access+'+AccessLevel+'&'+MemberList);
		//}
		//else {
		//alert('Please use the checkboxes to the left to select the members you wish to specify team access for');
		//$("team_access").selectedIndex=0;
		//return false;
		//}
	}
}

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
			
			if (elm.type == "radio" || elm.type == "checkbox")
			{
				elm.checked = false;
			}
			
			if (elm.length > 1 && (elm.type != "radio" || elm.type != "checkbox"))
			{
			
				elm.selectedIndex = 0;
			}
		}
}

Ajax.Responders.register({
onCreate: function() {
 if($('busy') && Ajax.activeRequestCount>0)
// $('busy').style.display='';
showIndicator();
},

onComplete: function() {
 if($('busy') && Ajax.activeRequestCount==0)
 //$('busy').style.display='none';
hideIndicator();
}
});