var sponsorCounter = 0;
var tempShare;
function toggleAccordion(id,param){
	new Effect.toggle(id,'BLIND',{afterFinish:function(){
		if(Element.visible(id)){
			document.getElementById(param).innerHTML = "Close";
		}else{
			document.getElementById(param).innerHTML = "Read more";
		}
	}});
}
function toggleSchedule(id,param){
	new Effect.toggle(id,'BLIND',{afterFinish:function(){
		if(Element.visible(id)){
			document.getElementById(param).innerHTML = "Close";
		}else{
			document.getElementById(param).innerHTML = "View schedule";
		}
	}});
}
function init(){
	tempShare = document.getElementById('share').innerHTML;
	setInterval(function(){rotateSponsor();},5000);
	initimg();
	updateTotal();
}

function rotateSponsor(){
	if(sponsorCounter >= sponsorItemTotal){ sponsorCounter = 0; }
	var i = sponsorCounter;
	var e = document.getElementById('sponsor-bottom');
	
	new Effect.Fade("sponsor-image", {afterFinish:function(){
		e.innerHTML = "<img src='http://www.singaporemasters.org/images/"+sponsor[i][0]+sponsor[i][1]+"' alt='"+sponsor[i][2]+"' id='sponsor-image' />";
		new Effect.Appear(e);
	}})
	
	
	sponsorCounter++;
	
}
function page(cat,num,each){
	new Ajax.Updater('photos','http://www.singaporemasters.org/include/pagethumbs.php?category='+cat+'&num='+num+'&each='+each,{asynchronous:true,onSuccess:function(){window.scroll(0,0);}});
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function initimg(){
	MM_preloadImages('http://www.singaporemasters.org/art/nav_home_o.png');
	MM_preloadImages('http://www.singaporemasters.org/art/nav_events_o.png');
	MM_preloadImages('http://www.singaporemasters.org/art/nav_reg_o.png');
	MM_preloadImages('http://www.singaporemasters.org/art/nav_travel_o.png');
	MM_preloadImages('http://www.singaporemasters.org/art/nav_photos_o.png');
	MM_preloadImages('http://www.singaporemasters.org/art/nav_contact_o.png');
}
function submitForm(id){ 
	var f = document.getElementById(id);
	if(window.XMLHttpRequest){ 
		var xmlReq = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) { 
		var xmlReq = new ActiveXObject('Microsoft.XMLHTTP'); 
	} 
	var formData = '', elem = ''; 
	for(var s=0; s<f.elements.length; s++){ 
		elem = f.elements[s]; 
		if(formData != ''){ 
			formData += '&'; 
		} 
		formData += elem.name+"="+elem.value; 
	} 
	xmlReq.onreadystatechange = function(){ 
		if(xmlReq.readyState == 4){ 
			if(id=="subscribeForm"){
				document.getElementById('subscribeResult').innerHTML = "<span style='font-size:10pt;color:#333333;padding-top:20px;'>"+xmlReq.responseText+"</span>";
			}else if(id=="shareForm"){
					document.getElementById('shareResponse').innerHTML = xmlReq.responseText;
					setTimeout(function(){ new Effect.SlideUp('shareFormContainer',{afterFinish:function(){
						document.getElementById('shareResponse').innerHTML = "";
					}}); },4000);
				}
		} 
	} 
	xmlReq.open(f.method, f.action, true); 
	xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xmlReq.send(formData); 
	return false; 
}
function checkYouremail (field) {
	var fieldID = field.id;
	var strng = field.value;
	var error="";
	if (strng == "") {
		YAHOO.util.Dom.addClass(fieldID,"error");
	    error = "Your e-mail address is required.\n";
	}

	var emailFilter=/^.+@.+\..{2,3}$/
	if (!(emailFilter.test(strng))) { 
	    error = "Invalid e-mail address.\n";
		YAHOO.util.Dom.addClass(fieldID,"error");
	}else {
	    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	    if (strng.match(illegalChars)) {
			YAHOO.util.Dom.addClass(fieldID,"error");
	        error = "The email address contains illegal characters.\n";
		}
	}
	if(!error){
		YAHOO.util.Dom.removeClass(fieldID,"error");
	}
	return error;    
}
function checkRadio(checkvalue) {
	var error = "";
	   if (!(checkvalue)) {
	       error = "Please select an age group.\n";
	    }
	return error;    
}
function isEmpty(field) {
	var strng = field.value;
	var error = "";
	var fieldID = field.id;
	  if (strng.length == 0) {
		 
		YAHOO.util.Dom.addClass(fieldID,"error");
	     error = "Please fill in the required field(s).\n"
	  }else if(strng != ''){
		YAHOO.util.Dom.removeClass(fieldID,"error")
	}
	return error;	  
}
function checkEventCB(theForm){
	var error="";
	var checkCounter=0;
	for (var i=0; i<theForm.evt.length; i++) {
		if(theForm.evt[i].checked){
			checkCounter++;
			var tfID = theForm.evt[i].id+"_field";
			if(document.getElementById(tfID).value=="" && theForm.id=="registration"){
				YAHOO.util.Dom.addClass(tfID,"error");
				error = "Please fill in time for selected event(s).\n";
			}
		}
	}
	if(checkCounter==0){
		error="Please select an event.\n";
	}
	return error;
}
function checkEventCBTeam(theForm){
	var error="";
	var checkCounter=0;
	for (var i=0; i<theForm.evt_1.length; i++) {
		if(theForm.evt_1[i].checked){
			checkCounter++;
		}
	}
	if(checkCounter==0){
		error="Please select an event.\n";
	}
	return error;
}
function checkGenderCB(cb){
	var checkCounter=0;
	var error="";
	for (var i=0; i<cb.length; i++) {
		if(cb[i].checked){
			checkCounter++;
		}
	}
	if(checkCounter==0){
		error="Please select a gender.\n";
	}
	return error;
}
function checkWholeForm(id) {
	var theForm = document.getElementById(id);
	var why = "";
	if(id == "subscribeForm"){ //subscribe form
		why += checkYouremail(theForm.email);
	}else if(id=="registrationteam"){
		/*var genderErr="";
		if(why==''){ why += isEmpty(theForm.club_name); }else{ isEmpty(theForm.club_name); }
		if(why==''){ why += isEmpty(theForm.name_1); }else{ isEmpty(theForm.name_1); }
		if(why==''){ why += isEmpty(theForm.date_1); }else{ isEmpty(theForm.date_1); }
		if(why==''){ why += isEmpty(theForm.month_1); }else{ isEmpty(theForm.month_1); }
		if(why==''){ why += isEmpty(theForm.year_1); }else{ isEmpty(theForm.year_1); }
		if(why==''){ why += isEmpty(theForm.age_1); }else{ isEmpty(theForm.age_1); }
		if(why==''){ why += isEmpty(theForm.name_2); }else{ isEmpty(theForm.name_2); }
		if(why==''){ why += isEmpty(theForm.date_2); }else{ isEmpty(theForm.date_2); }
		if(why==''){ why += isEmpty(theForm.month_2); }else{ isEmpty(theForm.month_2); }
		if(why==''){ why += isEmpty(theForm.year_2); }else{ isEmpty(theForm.year_2); }
		if(why==''){ why += isEmpty(theForm.age_2); }else{ isEmpty(theForm.age_2); }
		if(why==''){ why += isEmpty(theForm.name_3); }else{ isEmpty(theForm.name_3); }
		if(why==''){ why += isEmpty(theForm.date_3); }else{ isEmpty(theForm.date_3); }
		if(why==''){ why += isEmpty(theForm.month_3); }else{ isEmpty(theForm.month_3); }
		if(why==''){ why += isEmpty(theForm.year_3); }else{ isEmpty(theForm.year_3); }
		if(why==''){ why += isEmpty(theForm.age_3); }else{ isEmpty(theForm.age_3); }
		if(why==''){ why += isEmpty(theForm.name_4); }else{ isEmpty(theForm.name_4); }
		if(why==''){ why += isEmpty(theForm.date_4); }else{ isEmpty(theForm.date_4); }
		if(why==''){ why += isEmpty(theForm.month_4); }else{ isEmpty(theForm.month_4); }
		if(why==''){ why += isEmpty(theForm.year_4); }else{ isEmpty(theForm.year_4); }
		if(why==''){ why += isEmpty(theForm.age_4); }else{ isEmpty(theForm.age_4); }
		if(why==''){ why += isEmpty(theForm.confirm_name); }else{ isEmpty(theForm.confirm_name); }
		if(why==''){ why += isEmpty(theForm.confirm_addr); }else{ isEmpty(theForm.confirm_addr); }
		if(why==''){ why += isEmpty(theForm.confirm_phone); }else{ isEmpty(theForm.confirm_phone); }
		if(why==''){ why += isEmpty(theForm.confirm_email); }else{ isEmpty(theForm.confirm_email); }
		if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_1); }else{ checkGenderCB(theForm.gender_1);}
		if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_2); }else{ checkGenderCB(theForm.gender_2); }
		if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_3); }else{ checkGenderCB(theForm.gender_3); }
		if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_4); }else{ checkGenderCB(theForm.gender_4); }
		
		for (var i=0; i<theForm.agegroup.length; i++) { //check agegroup radios
		   if (theForm.agegroup[i].checked) {
		      var checkvalue = theForm.agegroup[i].value;
		      break;
		   }
		}
		why += checkRadio(checkvalue);
		why += checkEventCB(theForm);
		why += genderErr;*/
	}else if(id=="registration"){ //registration form
		if(Element.visible('individual_form')){
			if(why==''){ why+=isEmpty(theForm.surname); }else{ isEmpty(theForm.surname); }
			if(why==''){ why+=isEmpty(theForm.givenname); }else{ isEmpty(theForm.givenname); }
			if(why==''){ why+=isEmpty(theForm.date); }else{ isEmpty(theForm.date); }
			if(why==''){ why+=isEmpty(theForm.month); }else{ isEmpty(theForm.month); }
			if(why==''){ why+=isEmpty(theForm.year); }else{ isEmpty(theForm.year); }
			if(why==''){ why+=isEmpty(theForm.address); }else{ isEmpty(theForm.address); }
			if(why==''){ why+=isEmpty(theForm.postcode); }else{ isEmpty(theForm.postcode); }
			if(why==''){ why+=isEmpty(theForm.phone_1); }else{ isEmpty(theForm.phone_1); }
			if(why==''){ why+=isEmpty(theForm.phone_2); }else{ isEmpty(theForm.phone_2); }
			if(why==''){ why+=isEmpty(theForm.phone_3); }else{ isEmpty(theForm.phone_3); }
			if(why==''){ why+=isEmpty(theForm.mobile); }else{ isEmpty(theForm.mobile); }
			if(why==''){ why+=isEmpty(theForm.nationality); }else{ isEmpty(theForm.nationality); }
			if(why==''){ why+=isEmpty(theForm.emergency_contact); }else{ isEmpty(theForm.emergency_contact); }
			if(why==''){ why+=isEmpty(theForm.emergency_phone_1); }else{ isEmpty(theForm.emergency_phone_1); }
			if(why==''){ why+=isEmpty(theForm.emergency_phone_2); }else{ isEmpty(theForm.emergency_phone_2); }
			if(why==''){ why+=isEmpty(theForm.emergency_phone_3); }else{ isEmpty(theForm.emergency_phone_3); }
			why+=checkYouremail(theForm.email);
			for (var i=0; i<theForm.agegroup.length; i++) { //check agegroup radios
			   if (theForm.agegroup[i].checked) {
			      var checkvalue = theForm.agegroup[i].value;
			      break;
			   }
			}
			why += checkRadio(checkvalue);
			why += checkEventCB(theForm);
			why += checkGenderCB(theForm.gender);
		}
		if(Element.visible('team_form')){
			var genderErr="";
			if(document.registration.synchronized_with.value!=1){
				if(why==''){ why += isEmpty(theForm.name_1); }else{ isEmpty(theForm.name_1); }
				if(why==''){ why += isEmpty(theForm.date_1); }else{ isEmpty(theForm.date_1); }
				if(why==''){ why += isEmpty(theForm.month_1); }else{ isEmpty(theForm.month_1); }
				if(why==''){ why += isEmpty(theForm.year_1); }else{ isEmpty(theForm.year_1); }
				if(why==''){ why += isEmpty(theForm.age_1); }else{ isEmpty(theForm.age_1); }
			}
			if(document.registration.synchronized_with.value!=2){
				if(why==''){ why += isEmpty(theForm.name_2); }else{ isEmpty(theForm.name_2); }
				if(why==''){ why += isEmpty(theForm.date_2); }else{ isEmpty(theForm.date_2); }
				if(why==''){ why += isEmpty(theForm.month_2); }else{ isEmpty(theForm.month_2); }
				if(why==''){ why += isEmpty(theForm.year_2); }else{ isEmpty(theForm.year_2); }
				if(why==''){ why += isEmpty(theForm.age_2); }else{ isEmpty(theForm.age_2); }
			}
			if(document.registration.synchronized_with.value!=3){
				if(why==''){ why += isEmpty(theForm.name_3); }else{ isEmpty(theForm.name_3); }
				if(why==''){ why += isEmpty(theForm.date_3); }else{ isEmpty(theForm.date_3); }
				if(why==''){ why += isEmpty(theForm.month_3); }else{ isEmpty(theForm.month_3); }
				if(why==''){ why += isEmpty(theForm.year_3); }else{ isEmpty(theForm.year_3); }
				if(why==''){ why += isEmpty(theForm.age_3); }else{ isEmpty(theForm.age_3); }
			}
			if(document.registration.synchronized_with.value!=4){
				if(why==''){ why += isEmpty(theForm.name_4); }else{ isEmpty(theForm.name_4); }
				if(why==''){ why += isEmpty(theForm.date_4); }else{ isEmpty(theForm.date_4); }
				if(why==''){ why += isEmpty(theForm.month_4); }else{ isEmpty(theForm.month_4); }
				if(why==''){ why += isEmpty(theForm.year_4); }else{ isEmpty(theForm.year_4); }
				if(why==''){ why += isEmpty(theForm.age_4); }else{ isEmpty(theForm.age_4); }
			}
			
			if(document.registration.synchronized_with.value!=1){
				if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_1); }else{ checkGenderCB(theForm.gender_1);}
			}
			if(document.registration.synchronized_with.value!=2){
				if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_2); }else{ checkGenderCB(theForm.gender_2); }
			}
			if(document.registration.synchronized_with.value!=3){
				if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_3); }else{ checkGenderCB(theForm.gender_3); }
			}
			if(document.registration.synchronized_with.value!=4){
				if(genderErr==''){ genderErr += checkGenderCB(theForm.gender_4); }else{ checkGenderCB(theForm.gender_4); }
			}

			for (var i=0; i<theForm.agegroup_1.length; i++) { //check agegroup radios
			   if (theForm.agegroup_1[i].checked) {
			      var checkvalue = theForm.agegroup_1[i].value;
			      break;
			   }
			}
			why += checkRadio(checkvalue);
			why += checkEventCBTeam(theForm);
			why += genderErr;
		}
		if(why==''){ why += isEmpty(theForm.confirm_name); }else{ isEmpty(theForm.confirm_name); }
		if(why==''){ why += isEmpty(theForm.confirm_addr); }else{ isEmpty(theForm.confirm_addr); }
		if(why==''){ why += isEmpty(theForm.confirm_phone_1); }else{ isEmpty(theForm.confirm_phone_1); }
		if(why==''){ why += isEmpty(theForm.confirm_phone_2); }else{ isEmpty(theForm.confirm_phone_2); }
		if(why==''){ why += isEmpty(theForm.confirm_phone_3); }else{ isEmpty(theForm.confirm_phone_3); }
		if(why==''){ why += isEmpty(theForm.confirm_email); }else{ isEmpty(theForm.confirm_email); }
	}else if(id=="disclaimer"){
		if(theForm.readDisclaimer.checked==false){
			why = "Please check the tick box provided, as a sign that you have agreed to this disclaimer statements.";
		}
	}else if(id == "shareForm"){
				why += isEmpty(theForm.yourname);
				if(why==''){ why += isEmpty(theForm.youremail); }else{isEmpty(theForm.youremail); }
				if(why==''){ why += isEmpty(theForm.friendname); }else{isEmpty(theForm.friendname); }
				if(why==''){ why += isEmpty(theForm.friendemail); }else{ isEmpty(theForm.friendemail);}
			}
	
	//process error / sending
	if (why != "") {
		alert(why);
	    return false;
	}else{
		if(id=="subscribeForm" || id=='shareForm'){
			submitForm(id);
		}else{
			theForm.submit();
		}
		return false;
	}
}
function validateIndividualForm(){
	var theForm = document.getElementById('registration');
	var why = "";
	if(why==''){ why+=isEmpty(theForm.surname); }else{ isEmpty(theForm.surname); }
	if(why==''){ why+=isEmpty(theForm.givenname); }else{ isEmpty(theForm.givenname); }
	if(why==''){ why+=isEmpty(theForm.date); }else{ isEmpty(theForm.date); }
	if(why==''){ why+=isEmpty(theForm.month); }else{ isEmpty(theForm.month); }
	if(why==''){ why+=isEmpty(theForm.year); }else{ isEmpty(theForm.year); }
	if(why==''){ why+=isEmpty(theForm.address); }else{ isEmpty(theForm.address); }
	if(why==''){ why+=isEmpty(theForm.postcode); }else{ isEmpty(theForm.postcode); }
	if(why==''){ why+=isEmpty(theForm.phone_1); }else{ isEmpty(theForm.phone_1); }
	if(why==''){ why+=isEmpty(theForm.phone_2); }else{ isEmpty(theForm.phone_2); }
	if(why==''){ why+=isEmpty(theForm.phone_3); }else{ isEmpty(theForm.phone_3); }
	if(why==''){ why+=isEmpty(theForm.mobile); }else{ isEmpty(theForm.mobile); }
	if(why==''){ why+=isEmpty(theForm.nationality); }else{ isEmpty(theForm.nationality); }
	if(why==''){ why+=isEmpty(theForm.emergency_contact); }else{ isEmpty(theForm.emergency_contact); }
	if(why==''){ why+=isEmpty(theForm.emergency_phone_1); }else{ isEmpty(theForm.emergency_phone_1); }
	if(why==''){ why+=isEmpty(theForm.emergency_phone_2); }else{ isEmpty(theForm.emergency_phone_2); }
	if(why==''){ why+=isEmpty(theForm.emergency_phone_3); }else{ isEmpty(theForm.emergency_phone_3); }
	why+=checkYouremail(theForm.email);
	for (var i=0; i<theForm.agegroup.length; i++) { //check agegroup radios
	   if (theForm.agegroup[i].checked) {
	      var checkvalue = theForm.agegroup[i].value;
	      break;
	   }
	}
	why += checkRadio(checkvalue);
	why += checkEventCB(theForm);
	why += checkGenderCB(theForm.gender);
	if(why==""){
		return true;
	}else{
		return false;
	}
}
function switchTextBox(theid){
	var theForm = document.getElementById('registration');
	var cb = document.getElementById(theid);
	var tb = document.getElementById(theid+"_field");
	//var totalField = document.registration.totalEvent.value;
	var checkCounter=0;
	for (var i=0; i<theForm.evt.length; i++) {
		if(theForm.evt[i].checked){
			checkCounter++;
		}
	}
	if(checkCounter<=7){
		
		if(cb.checked==true){
			tb.disabled = false;
			tb.value = "99:99:99"
			updateFees('event');
		}else{
			tb.disabled = true;
			tb.value="";
			updateFees('event');
			YAHOO.util.Dom.removeClass(tb,"error");
		}
	}else{
		alert("You cannot select more than 7 events");
		cb.checked=false;
	}
}
function switchTextBox2(theid){
	var theForm = document.getElementById('registration');
	var cb = document.getElementById(theid);
	var tb = document.getElementById(theid+"_field");
	//var totalField = document.registration.totalEvent.value;
	var checkCounter=0;
	for (var i=0; i<theForm.evt.length; i++) {
		if(theForm.evt[i].checked){
			checkCounter++;
		}
	}
	if(checkCounter<=7){
		
		if(cb.checked==true){
			tb.disabled = false;
			tb.value = "99:99:99"
			updateFees('event');
		}else{
			tb.disabled = true;
			tb.value="";
			updateFees('event');
			YAHOO.util.Dom.removeClass(tb,"error");
		}
	}else{
		alert("You cannot select more than 7 events");
		cb.checked=false;
	}
}
function updateFees(item){
	if(item=='nonaffiliate'){
		if(document.getElementById(item).checked==true){
			document.getElementById(item+"fee").value=10;
		}else if(document.getElementById(item).checked==false){
			document.getElementById(item+"fee").value=0;
		}
	}else if(item=='event'){
		var total = 0;
		var max = document.registration.evt.length;
		for (var i = 0; i < max; i++) {
			if (document.registration.evt[i].checked == true) {
		    	total += 1;
		   	}
		}
		document.registration.totalEvent.value = total;
		document.registration.eventfees.value=total*10;
	}
	updateTotal();
}
function toggleRegField(container,field){
	var objRef = document.getElementById(field);
	if(field == "regid_registered"){
		objRefField = document.getElementById('waived');
	}else if(field == "regid_registered_1"){
		objRefField = document.getElementById('waived_1');
	}else if(field == "regid_registered_2"){
		objRefField = document.getElementById('waived_2');
	}else if(field == "regid_registered_3"){
		objRefField = document.getElementById('waived_3');
	}else if(field == "regid_registered_4"){
		objRefField = document.getElementById('waived_4');
	}
	if(Element.visible(container)){
		new Effect.Fade(container);
		objRef.value = "";
		objRefField.value = "0";
		var sync = document.getElementById("synchronized_with").value;
		if(sync != ""){
			document.getElementById("waived_"+sync).value="0";
		}
		checkWaivedFields();
		updateTotal();
		//updateAffiliateDiscount("notvalid");
	}else{
		new Effect.Appear(container);
		objRef.value = "Enter registration ID...";
		eventAdd(objRef,"onclick","resetRegIdValue('"+field+"','');");
		eventAdd(objRef,"onblur","resetRegIdValue('"+field+"','Enter registration ID...');");
	}
}
function checkWaivedFields(){
	var ctr2 = 0;
	if(Element.visible('individual_form')){
		if(document.registration.waived.value=="0"){
			ctr2++;
		}
	}
	if(Element.visible('team_form')){
		if(document.registration.waived_1.value=="0"){
			ctr2++;
		}
		if(document.registration.waived_2.value=="0"){
			ctr2++;
		}
		if(document.registration.waived_3.value=="0"){
			ctr2++;
		}
		if(document.registration.waived_4.value=="0"){
			ctr2++;
		}
	}
	rebate = parseInt(ctr2) * 30;
	document.registration.regfee.value = rebate;
}
function checkAffiliateFields(){
	var ctr2 = 0;
	if(Element.visible('individual_form')){
		if(document.registration.affiliate_discount.value=="1"){
			ctr2++;
		}
	}
	if(Element.visible('team_form')){
		if(document.registration.affiliate_discount_1.value=="1"){
			ctr2++;
		}
		if(document.registration.affiliate_discount_2.value=="1"){
			ctr2++;
		}
		if(document.registration.affiliate_discount_3.value=="1"){
			ctr2++;
		}
		if(document.registration.affiliate_discount_4.value=="1"){
			ctr2++;
		}
	}
	discount = parseInt(ctr2) * 10;
	document.registration.affiliatediscount.value = discount;
}
function checkTeamWaivedFields(){
	var ctr = 0;
	if(document.registration.waived_1.value=="1"){
		ctr++;
	}
	if(document.registration.waived_2.value=="1"){
		ctr++;
	}
	if(document.registration.waived_3.value=="1"){
		ctr++;
	}
	if(document.registration.waived_4.value=="1"){
		ctr++;
	}
	return ctr;
}
function checkTeamAffiliateFields(){
	var ctr = 0;
	if(document.registration.affiliate_discount_1.value=="1"){
		ctr++;
	}
	if(document.registration.affiliate_discount_2.value=="1"){
		ctr++;
	}
	if(document.registration.affiliate_discount_3.value=="1"){
		ctr++;
	}
	if(document.registration.affiliate_discount_4.value=="1"){
		ctr++;
	}
	return ctr;
}
function checkIndividualAffiliateFields(){
	var ctr = 0;
	if(document.registration.affiliate_discount.value=="1"){
		ctr++;
	}
	return ctr;
}
function checkIndividualWaivedFields(){
	var ctr = 0;
	if(document.registration.waived.value=="1"){
		ctr++;
	}
	return ctr;
}
function checkIDValidity(id,name,returnDiv,cField){
	new Ajax.Request('http://www.singaporemasters.org/include/validateID.php',{asynchronous:true,method:'post',postBody:'id='+id+'&name='+name+'&returnDiv='+returnDiv+'&cField='+cField,onSuccess: function(req){
		eval("response = " + req.responseText); //parse JSON
		flag = response.flag;
		message = response.message;
		if(flag == "1"){
			prefix = "<span class='green'>";
			suffix = "</span>";
		}else{
			prefix = "<span class='red'>";
			suffix = "</span>";
		}
		document.getElementById(returnDiv).innerHTML = prefix + message + suffix;
		document.getElementById(cField).value = flag;
		var sync = document.getElementById("synchronized_with").value;
		if(sync != ""){
			document.getElementById("waived_"+sync).value=flag;
		}
		checkWaivedFields();
		updateTotal();
	}});
	
}
function resetRegIdValue(id,value){
	var objRef = document.getElementById(id);
	if(id=="regid_registered"){
		name = document.registration.givenname.value + " " +document.registration.surname.value;
		returnDiv = "return_div";
		cField = "waived";
	}else if(id=="regid_registered_1"){
		name = document.registration.name_1.value;
		returnDiv = "return_div_1";
		cField = "waived_1";
	}else if(id=="regid_registered_2"){
		name = document.registration.name_2.value;
		returnDiv = "return_div_2";
		cField = "waived_2";
	}else if(id=="regid_registered_3"){
		name = document.registration.name_3.value;
		returnDiv = "return_div_3";
		cField = "waived_3";
	}else if(id=="regid_registered_4"){
		name = document.registration.name_4.value;
		returnDiv = "return_div_4";
		cField = "waived_4";
	}
	checkIDValidity(objRef.value,name,returnDiv,cField); //ajax routine
	if(value != ""){ //onblur
		if(objRef.value == ""){
			objRef.value = value;
		}else{
			if(objRef.value != "Enter registration ID..."){
				
			}
			//updateAffiliateDiscount("valid");
		}
	}else if(value == ""){ //onclick
		if(objRef.value == "Enter registration ID..."){
			objRef.value = value;
		}
	}
}
function toggleClubName(container,field){
	id = field;
	if(id == "club_name"){
		cField = "affiliate_discount";
	}else if(id == "club_name_1"){
		cField = "affiliate_discount_1";
	}else if(id == "club_name_2"){
		cField = "affiliate_discount_2";
	}else if(id == "club_name_3"){
		cField = "affiliate_discount_3";
	}else if(id == "club_name_4"){
		cField = "affiliate_discount_4";
	}
	var objRef = document.getElementById(field);
	var objRefField = document.getElementById(cField);
	if(Element.visible(container)){
		new Effect.Fade(container);
		objRef.value = "";
		objRefField.value = "0";
		var sync = document.getElementById("synchronized_with").value;
		if(sync != ""){
			document.getElementById("affiliate_discount_"+sync).value="0";
		}
		checkAffiliateFields();
		updateTotal();
	}else{
		new Effect.Appear(container);
		objRef.value = "Enter club name here...";
		eventAdd(objRef,"onclick","resetValue('"+field+"','');");
		eventAdd(objRef,"onblur","resetValue('"+field+"','Enter club name here...');");
	}
	
}
function resetValue(id,value){
	if(id == "club_name"){
		cField = "affiliate_discount";
	}else if(id == "club_name_1"){
		cField = "affiliate_discount_1";
	}else if(id == "club_name_2"){
		cField = "affiliate_discount_2";
	}else if(id == "club_name_3"){
		cField = "affiliate_discount_3";
	}else if(id == "club_name_4"){
		cField = "affiliate_discount_4";
	}
	var objRef = document.getElementById(id);
	if(value != ""){ //onblur
		if(objRef.value == ""){
			objRef.value = value;
		}else{
			document.getElementById(cField).value="1";
			var sync = document.getElementById("synchronized_with").value;
			if(sync != ""){
				document.getElementById("affiliate_discount_"+sync).value="1";
			}
			checkAffiliateFields();
			updateTotal();
		}
	}else if(value == ""){ //onclick
		if(objRef.value == "Enter club name here..."){
			objRef.value = value;
		}
	}
}
function updateAffiliateDiscount(discount){
	if(parseInt(document.registration.affiliatediscount.value) >= 0){
		if(discount == "valid"){
			document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) + 10;
		}else if(discount == "notvalid"){
			if(parseInt(document.registration.affiliatediscount.value) > 0){
				document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) - 10;
			}
		}
	}
	updateTotal();
}
function updateTotal(){
	var eventFees = document.registration.eventfees.value;
	if(!Element.visible('individual_form')){
		eventFees = 0;
	}
	document.registration.totalfees.value = (parseInt(document.registration.regfee.value) + parseInt(eventFees) + parseInt(document.registration.entryfee.value)) - parseInt(document.registration.affiliatediscount.value);
	document.getElementById('total').value= document.registration.totalfees.value;
}

function displayThumbs(cat){
	if(Element.visible('albums')){ 
		new Effect.BlindUp('albums',{afterFinish:function(){ 
				new Ajax.Updater('thumbs','http://www.singaporemasters.org/include/thumbs.php?category='+cat,{asynchronous:true, onComplete:function(){
					new Effect.BlindDown('thumbs', {delay:1.0}); 
		}})}})
	}else{ 
		new Ajax.Updater('thumbs','http://www.singaporemasters.org/thumbs.php?category='+cat,{asynchronous:true, onComplete:function(){
			new Effect.BlindDown('thumbs', {delay:1.0}); 
}});
	}
}
function closeThumbs(){
	new Effect.BlindUp('thumbs',{afterFinish:function(){
		new Effect.BlindDown('albums');
	}})
}

function displayShare(){
	new Effect.toggle('shareFormContainer','SLIDE');
}
function eventAdd(objAttrib,handler,addFunction){
     
   if ((!document.all)&&(document.getElementById)){
       objAttrib.setAttribute(handler,addFunction);
   }    
   //workaround for IE 5.x
   if ((document.all)&&(document.getElementById)){
       objAttrib[handler]=new Function(addFunction);
   }
}

function activateForm(name){
	var theDiv = name+"_form";
	var theField = name+"form";
	if(document.getElementById(theField).value == "0"){
		new Effect.BlindDown(theDiv,{afterFinish:function(){
			if(name=="individual"){
				ctr = checkIndividualWaivedFields();
				val = 30 - (parseInt(ctr) * 30);
				ctr2 = checkIndividualAffiliateFields();
				val2 = parseInt(ctr2) * 10;
				document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) + val2;
				document.registration.regfee.value = parseInt(document.registration.regfee.value) + val;
				new Effect.Appear('fee_individual_1');
			}else if(name="team"){
				ctr = checkTeamWaivedFields();
				val = 120 - (parseInt(ctr) * 30);
				ctr2 = checkTeamAffiliateFields();
				val2 = parseInt(ctr2) * 10;
				document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) + val2;
				document.registration.regfee.value = parseInt(document.registration.regfee.value) + val;
				document.registration.entryfee.value = "20";
				new Effect.Appear('fee_team_1');
			}
			document.getElementById(theField).value = "1";
			document.registration.processPayment.disabled = false;
			updateTotal();
			//document.registration.totalfees.value = parseInt(document.registration.totalfees.value) + parseInt(document.registration.eventfees.value);
		}});
		
		
	}else{
		new Effect.BlindUp(theDiv,{afterFinish:function(){
			if(name=="individual"){
				ctr = checkIndividualWaivedFields();
				val = 30 - (parseInt(ctr) * 30);
				ctr2 = checkIndividualAffiliateFields();
				val2 = parseInt(ctr2) * 10;
				document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) - val2;
				document.registration.regfee.value = parseInt(document.registration.regfee.value) - val;
				new Effect.Fade('fee_individual_1');
				if(!Element.visible('team_form')){
					document.registration.processPayment.disabled = true;
				}
			}else if(name="team"){
				ctr = checkTeamWaivedFields();
				val = 120 - (parseInt(ctr) * 30);
				ctr2 = checkTeamAffiliateFields();
				val2 = parseInt(ctr2) * 10;
				document.registration.affiliatediscount.value = parseInt(document.registration.affiliatediscount.value) - val2;
				document.registration.regfee.value = parseInt(document.registration.regfee.value) - val;
				document.registration.entryfee.value = 0;
				new Effect.Fade('fee_team_1');
				if(!Element.visible('individual_form')){
					document.registration.processPayment.disabled = true;
				}
			}
			document.getElementById(theField).value = "0";
			updateTotal();
			//document.registration.totalfees.value = parseInt(document.registration.totalfees.value) - parseInt(document.registration.eventfees.value);
		}});
		
		
	}
	
}
function synchronizeRegistrant(cbId,num,status){
	num1 = num;
	num = "_"+num;
	var cb = document.getElementById(cbId);
	var checkCounter=0;
	for (var i=0; i<document.registration.as_above.length; i++) {
		if(document.registration.as_above[i].checked){
			checkCounter++;
		}
	}
	if(checkCounter<2){
		if(status == "checked"){
			if(validateIndividualForm()){
				document.getElementById("name"+num).disabled = true;
				document.getElementById("name"+num).value = document.getElementById("givenname").value + " " + document.getElementById("surname").value;
				document.getElementById("date"+num).disabled = true;
				document.getElementById("month"+num).disabled = true;
				document.getElementById("year"+num).disabled = true;
				document.getElementById("age"+num).disabled = true;
				document.getElementById("gender"+num+"_1").disabled = true;
				document.getElementById("gender"+num+"_2").disabled = true;
				document.getElementById("affiliate"+num).disabled = true;
				document.getElementById("registered"+num).disabled = true;
				document.getElementById('waived'+num).value=1;
				document.getElementById('affiliate_discount'+num).value=document.getElementById('waived').value;
				document.registration.synchronized_with.value = num1;
			}else{
				alert("Please complete the particulars for individual registrant (including age group and event selection).");
				cb.checked = false;
			}
		}else if(status == "unchecked"){
			document.getElementById("name"+num).disabled = false;
			document.getElementById("name"+num).value = "";
			document.getElementById("date"+num).disabled = false;
			document.getElementById("month"+num).disabled = false;
			document.getElementById("year"+num).disabled = false;
			document.getElementById("age"+num).disabled = false;
			document.getElementById("gender"+num+"_1").disabled = false;
			document.getElementById("gender"+num+"_2").disabled = false;
			document.getElementById("affiliate"+num).disabled = false;
			document.getElementById("registered"+num).disabled = false;
			document.getElementById('waived'+num).value=0;
			document.getElementById('affiliate_discount'+num).value=0;
		}
		checkWaivedFields();
		checkAffiliateFields();
		updateTotal();
	}else if(checkCounter > 1){
		cb.checked = false;
	}
	
}

window.onload = init;