function clearForm() {
if (confirm("This will remove all you entered. Continue?")) {
   document.forms[0].reset();
}
}

function writeEmbeddedSWF(swftext)
{
   document.write(swftext+' ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>\n');
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function clearDetail(item) {
    document.getElementById(item.name+'_dtl').value='';
}

function checkYes(item) {
    var sItem,sId,nInd;
    if(trim(item.value)!=null && trim(item.value)!="") {
	   sItem = item.name;
	   nInd = sItem.indexOf('_dtl');
	   sId = sItem.substr(0,nInd);
	   document.getElementById(sId+'Y').checked = true; 
	}
}

function closeWindow() {
	window.open('','_parent','');
	window.close();
}

function toggle(label,cnt) {
    if (document.getElementById(label+'_img').src.indexOf('Open')<0) {
        document.getElementById(label+'_img').src = 'images/folderOpen.gif';
		for (x=1; x<=cnt; x++) {
             document.getElementById(label+x).style.display = 'block';
             document.getElementById(label+x).style.visibility = 'visible';
        }
    } else {
        document.getElementById(label+'_img').src = 'images/folderClose.gif';
		for (x=1; x<=cnt; x++) {
             document.getElementById(label+x).style.display = 'none';
             document.getElementById(label+x).style.visibility = 'hidden';
        }
    }
}
	
function showHideMe(whichEl, imageExpand, imageCollapse) {
    whichIm = event.srcElement;
    if (whichEl.style.display == "none") {
        whichEl.style.display = "block";
	whichEl.style.visibility = "visible";
        whichIm.src = imageExpand;
    } else {
        whichEl.style.display = "none";
	whichEl.style.visibility = "hidden";
        whichIm.src = imageCollapse;
    }
}

function redir() {
	if (document.menu.url.options[document.menu.url.selectedIndex].value != "none") {
		location = document.menu.url.options[document.menu.url.selectedIndex].value;
	}
	return false;
}

function elementExists(eName,eType) {
	var frm = document.frmAMRegistration;
	var formSize = frm.elements.length;
	var elem;
	
	for( var i = 0; i < formSize; i++) {
		elem = frm.elements[i];
		if(elem.type == eType && elem.name == eName) {
			return true;
		}
	}
	return false;
}

function formatFax(sFax) {
	var tmpFax = sFax.value;
	var newFax = "";
	for (var i = 0; i < tmpFax.length; i++) {
		if (!isNaN(tmpFax.charAt(i)) && tmpFax.charAt(i) != " ") {
			newFax = newFax + tmpFax.charAt(i);
		}
	}
	sFax.value = newFax;
}

function checkDate(dtField,dtDisp) {
	var frm = document.frmAMRegistration;
	var sDate = dtField.value;
	var month, day, year;
	var monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	month = sDate.substring(3,5) - 1;

	if (isNaN(month) || (month > 11) || (month < 0)) {
		month = "invalid";
	} else {
		month = monthName[month];
	}

	day = sDate.substring(0,2);
	if((day < 1) || (day > 31) || isNaN(day)) {
		day = "invalid";
	}

	if (sDate.length < 8) {
		year = "invalid";
	} else {
		year = sDate.substring(6,8)
		if ((year<0) || (year>99) || isNaN(year)) {
			year = "invalid";
		}
	}
	if((day == "invalid") || (month == "invalid") || (year == "invalid")) {
		dtField.value = "";
		dtDisp.innerText = "invalid";
	} else {
		dtDisp.innerText = day + " " + month + " 19" + year;
		dtField.value = sDate.substring(0,2) + "/" + sDate.substring(3,5) + "/" + sDate.substring(6,8);
	}
}

function checkTime(sTime) {
	var hour, min, time;

	time = sTime.value;
//	if (time.length < 5) {
//		sTime.value = "0" + sTime.value;
//		time = sTime.value;
//	}
//	hour = time.substring(0,2);
	hour = parseInt(time);
	min = time.substring(3,5);

	if((hour < 0) || (hour > 24)) {
		hour = "invalid";
	}
	if((min < 0) || (min > 59)) {
		min = "invalid";
	}

	if((hour == "invalid") || (min == "invalid")) {
		sTime.value = "";
		alert("Please use the correct format for entering the time.");
		sTime.focus();
	}
}

function chkEmail(src) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	var at_array = src.value.split("@");

	if(src.value.length > 0) {
		if ((src.value.indexOf(".") < 0) || (regex.test(src.value)==false)) {
			return false;
		}
	}
	if((src.value.indexOf(",") >= 0) || (src.value.indexOf(";") >= 0) || (at_array.length>2)) {
		return false;
	}
	
	return true;
}

function setTime(sTime,ampm) {
	var time, range, hour, tmpTm;

	time = sTime.value;
	tmpTm = sTime.value;
	if (time.length < 5) {
		sTime.value = "0" + sTime.value;
		time = sTime.value;
	}
//	hour = time.substring(0,2);
	hour = parseInt(tmpTm);
	
	if(hour<13) {
		range = ampm.value;
		if(hour==12) {
			if(range=="AM") {
				time = "00" + time.substring(2,5);
			}
		} else {
			if(range=="PM") {
				hour = 12 + hour;
				time = hour + time.substring(2,5);
			}
		}
	}

	if(hour==24) {
		time = "00" + time.substring(2,5);
	}

	sTime.value = time;

}

function show(id) {
	elem = document.all(id);
	if (elem.style.visibility == 'hidden') {
		elem.style.visibility = 'visible';
		elem.style.display = 'block';
	}
}
function hide(id) {
	elem = document.all(id);
	if (elem.style.visibility == 'visible') {
		elem.style.visibility = 'hidden';
		elem.style.display = 'none';
	}
}

function checkMandatoryItems() {
	var frm = document.forms[0];
	var bContinue = true;
	var error_msg = "";
	var error_cnt = 0;

	translateAmper(frm);
	if(trim(frm.fname.value)==null || trim(frm.fname.value)=="") {
		error_cnt++;
        error_msg = error_msg+error_cnt+". Please indicate your first name.\n";
		bContinue = false;
	}
	if(trim(frm.sname.value)==null || trim(frm.sname.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your surname.\n";
		bContinue = false;
	}
	if(trim(frm.age.value)==null || trim(frm.age.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your age.\n";
		bContinue = false;
	}
	if(frm.gender[0].checked==false && frm.gender[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your gender.\n";
		bContinue = false;
	}
	if((trim(frm.add1.value)==null || trim(frm.add1.value)=="") && (trim(frm.add2.value)==null || trim(frm.add2.value)=="") && (trim(frm.add3.value)==null || trim(frm.add3.value)=="")) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your mailing address.\n";
		bContinue = false;
	}
	if(trim(frm.suburb.value)==null || trim(frm.suburb.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your town/city.\n";
		bContinue = false;
	}
	if(trim(frm.state.value)==null || trim(frm.state.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your state/province.\n";
		bContinue = false;
	}
	if(trim(frm.country.value)==null || trim(frm.country.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your country of residence.\n";
		bContinue = false;
	}
	if(trim(frm.pcode.value)==null || trim(frm.pcode.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your zip/post code.\n";
		bContinue = false;
	}
	if(trim(frm.citizenship.value)==null || trim(frm.citizenship.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your nationality.\n";
		bContinue = false;
	}
	if(trim(frm.e_mail.value)==null || trim(frm.e_mail.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your email address.\n";
		bContinue = false;
	} else {
       if(!chkEmail(frm.e_mail)) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Email Address should be a single valid email address.\n";
		   bContinue = false;
	   }
	}
	if(trim(frm.education.value)==null || trim(frm.education.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your education level.\n";
		bContinue = false;
	}
	if(trim(frm.youdoing.value)==null || trim(frm.youdoing.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate what you are doing at present.\n";
		bContinue = false;
	} else {
       if(trim(frm.youdoing.value)=="Working" && (trim(frm.job.value)==null || trim(frm.job.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please indicate the nature of your work and position.\n";
		   bContinue = false;
	   }
	}
	if(trim(frm.nlanguage.value)==null || trim(frm.nlanguage.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate your native language.\n";
		bContinue = false;
	}
	if(trim(frm.mychar.value)==null || trim(frm.mychar.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please explain briefly your character as best as you can.\n";
		bContinue = false;
	}
	if(trim(frm.myreason.value)==null || trim(frm.myreason.value)=="") {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please give reasons for wanting to become an Island Volunteer.\n";
		bContinue = false;
	}
	if(frm.otherprog[0].checked==false && frm.otherprog[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Have you been a member of other volunteer programs?\n";
		bContinue = false;
	} else {
       if(frm.otherprog[1].checked && (trim(frm.otherprog_dtl.value)==null || trim(frm.otherprog_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please give details of your other volunteer programs.\n";
		   bContinue = false;
	   }
	}
	if(frm.beenasia[0].checked==false && frm.beenasia[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Have you been to asia before?\n";
		bContinue = false;
	} else {
       if(frm.beenasia[1].checked && (trim(frm.beenasia_dtl.value)==null || trim(frm.beenasia_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please indicate where in asia have you been.\n";
		   bContinue = false;
	   }
	}
	if(frm.availjune[0].checked==false && frm.availjune[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Are you available in June?\n";
		bContinue = false;
	}
	if(frm.diet[0].checked==false && frm.diet[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Do you have special dietary requirements?\n";
		bContinue = false;
	} else {
       if(frm.diet[1].checked && (trim(frm.diet_dtl.value)==null || trim(frm.diet_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please indicate what your dietary requirements are.\n";
		   bContinue = false;
	   }
	}
	if(frm.criminal[0].checked==false && frm.criminal[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Do you have any criminal convictions?\n";
		bContinue = false;
	} else {
       if(frm.criminal[1].checked && (trim(frm.criminal_dtl.value)==null || trim(frm.criminal_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please give details of your criminal conviction.\n";
		   bContinue = false;
	   }
	}
	if(frm.disable[0].checked==false && frm.disable[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Do you have any disabilities?\n";
		bContinue = false;
	} else {
       if(frm.disable[1].checked && (trim(frm.disable_dtl.value)==null || trim(frm.disable_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please give details of your disabilities.\n";
		   bContinue = false;
	   }
	}
	if(frm.illness[0].checked==false && frm.illness[1].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Do you have any history of mental illness or depression?\n";
		bContinue = false;
	} else {
       if(frm.illness[1].checked && (trim(frm.illness_dtl.value)==null || trim(frm.illness_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please give details of your illnesses.\n";
		   bContinue = false;
	   }
	}
	if(frm.referrer[0].checked==false && frm.referrer[1].checked==false && frm.referrer[2].checked==false && frm.referrer[3].checked==false && frm.referrer[4].checked==false && frm.referrer[5].checked==false) {
		error_cnt++;
		error_msg = error_msg+error_cnt+". Please indicate how you learned about Island Volunteers.\n";
		bContinue = false;
	} else {
       if(frm.referrer[3].checked && (trim(frm.website_dtl.value)==null || trim(frm.website_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please specify the website that referred Island Volunteers.\n";
		   bContinue = false;
	   }
       if(frm.referrer[5].checked && (trim(frm.other_dtl.value)==null || trim(frm.other_dtl.value)=="")) {
		   error_cnt++;
		   error_msg = error_msg+error_cnt+". Please specify how you learned about Island Volunteers (Others).\n";
		   bContinue = false;
	   }
	}

    if(!bContinue) {
	   alert("Form not submitted due to the following reasons:\n\n"+error_msg);
/*       tab_pages = document.getElementsByClassName('tab_page');
       TabToggle(tab_pages[0]); */
	}
	return bContinue;
}

function translateAmper(frm) {
	var formSize = frm.elements.length;
	var elem;
	var cnt = 0;
	
	for( var i = 0; i < formSize; i++) {
		elem = frm.elements[i];
		if(elem.type == "text" || elem.type == "hidden") {
			while (elem.value.indexOf("&") > 0) {
				elem.value = elem.value.replace("&","and");
			}
		}
	}
}