function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function toggleAge(){
	// do nothing really
}

function checkVideo(){
	clearErrors();
	
	div = document.getElementById("trCheckVideo");
	
	iframe = window.frames["iframeCheckVideo"];	
	url = "videocontest_checkvideo.php";
	
	doc = document.EN07Registration;
	
	q = trim(doc.youtubeURL.value);
	doc.youtubeURL.value = q;
	
	if(sendError("youtube","youtubeURL","errorYoutubeURL","divErrorYoutubeURL","","Youtube URL must be valid.")){
		div.style.display = 'none';
		iframe.location.href = url;		
		return false;		
	}
	
	div.style.display = '';		
	
	url += "?q=";
	url += escape(q);
	url += "&formAction=checkvideo";
	
	iframe.location.href = url;
}

function isMobileInternational(){
	doc = document.EN07Registration;
	
	doc = document.EN07Registration;
	co = doc.country.options[doc.country.selectedIndex].value;		
	
	if(co!="0"){
		co_array = co.split(",");
		divInt = document.getElementById("divMobileNumberInternational");		
		divPH = document.getElementById("divMobileNumberPhilippines");		
		if(co_array[0]=="PH"){
			divInt.style.display = "none";
			divPH.style.display = "";
		}
		else{
			divInt.style.display = "";
			divPH.style.display = "none";
		}
	}
}

function isNumeric(str){
	if(str=="" || isNaN(str)){
		return false;					
	}
	return true;
}
			
function isEmailValid(str){
	if(str.indexOf(".") > 2 && str.indexOf("@") > 0){
		return true;
	}
	else{
		return false;
	}
}			
			
function setDateDropdown(mID,dID,yID) {
	monthField = document.getElementById(mID);
	dayField = document.getElementById(dID);
	yearField = document.getElementById(yID);
				
	daySelected = dayField.selectedIndex;
  				
	// remove day options above 29 				
	for(i=dayField.options.length ; i>=29 ; i--){
		// ie
		// dayField.options.remove(i);
		dayField.options[i] = null;
	}
  				
	switch(monthField.options[monthField.selectedIndex].value) {
		case "02":
			yr = yearField.options[yearField.selectedIndex].value;
			if ((yr % 4) == 0) dayField.options[29]=new Option("29");
			if (daySelected > 28){
	  			daySelected = 28;
			}
		  	break;
		case "04":
		case "06":
		case "09":
		case "11":
			dayField.options[29] = new Option("29");
			dayField.options[30] = new Option("30");
			if (daySelected > 30){
	  			daySelected = 30;
			}
			break;	  
		default:
			dayField.options[29] = new Option("29");
			dayField.options[30] = new Option("30");					
			dayField.options[31] = new Option("31");											
			break;
	  }
	  dayField.options[daySelected].selected = true;
}	
						
function copyField(toField,fromField,fieldType){
	t = document.getElementById(toField);
	f = document.getElementById(fromField);
	switch(fieldType){
		case "text":
			t.value = f.value;
			break;
		case "select":
			t.selectedIndex = f.selectedIndex;
			break;
	}
}

function toggleCountry(){
	doc = document.EN07Registration;
	co = doc.country.options[doc.country.selectedIndex].value;				
	if(co!="0"){
		co_array = co.split(",");
		if(co_array[1]=="1" && co_array[2]=="0"){
			document.getElementById("divSuggestACity").style.display = '';
			document.getElementById("divSuggestAChurch").style.display = '';
		}
		else{
			document.getElementById("divSuggestACity").style.display = 'none';
			document.getElementById("divSuggestAChurch").style.display = 'none';						
		}
	}				
}
			
function showCitySuggestions(){
	doc = document.EN07Registration;
	co = doc.country.options[doc.country.selectedIndex].value;
	if(co!="0"){
		co_array = co.split(",");
		if(co_array[1]=="1" && co_array[2]=="0"){
			co = co_array[0];
			tooltip_html = "<iframe id=\'iframeCitySuggestions\' style=\'width:100%; height:70px;\' frameborder=0 scrolling=\'no\' src=\'suggest.php?&fillField=city&suggest=city&co="+co+"\'></iframe>";
			return overlib(tooltip_html, STICKY, NOCLOSE, HEIGHT, 70, WIDTH, 337, BELOW, FIXY, 694);						
		}
		return false;
	}
	return false;
}
			
function showChurchSuggestions(){
	doc = document.EN07Registration;
	co = doc.country.options[doc.country.selectedIndex].value;
	ci = doc.city.value;
		
	if(co!="0"){
		co_array = co.split(",");
		if(co_array[1]=="1" && co_array[2]=="0"){
			co = co_array[0];
			tooltip_html = "<iframe id=\'iframeChurchSuggestions\' style=\'width:100%; height:70px;\' frameborder=0 scrolling=\'no\' src=\'suggest.php?&fillField=church&suggest=church&co="+co+"&ci="+escape(ci)+"\'></iframe>";
			return overlib(tooltip_html, STICKY, NOCLOSE, HEIGHT, 70, WIDTH, 337, BELOW, FIXY, 740);						
		}
		return false;
	}				
		else return false;
}			
	
function clearErrors(){
	doc = document.EN07Registration;
	
	// isRegistered
	doc.errorIsRegistered.value = "";
	document.getElementById("divErrorIsRegistered").style.display = 'none';
	
	// country				
	doc.errorCountry.value = "";
	document.getElementById("divErrorCountry").style.display = 'none';				
	
	// city				
	doc.errorCity.value = "";
	document.getElementById("divErrorCity").style.display = 'none';
				
	// church				
	doc.errorChurch.value = "";
	document.getElementById("divErrorChurch").style.display = 'none';
	
	// first name
	doc.errorFirstName.value = "";
	document.getElementById("divErrorFirstName").style.display = 'none';
	
	// last name
	doc.errorLastName.value = "";
	document.getElementById("divErrorLastName").style.display = 'none';
	
	// gender
	doc.errorGender.value = "";
	document.getElementById("divErrorGender").style.display = 'none';
	
	// age
	doc.errorAge.value = "";
	document.getElementById("divErrorAge").style.display = 'none';	
	
	// email
	doc.errorEmailAddress.value = "";
	document.getElementById("divErrorEmailAddress").style.display = 'none';
	
	// mobile number
	doc.errorMobileNumber.value = "";
	document.getElementById("divErrorMobileNumber").style.display = 'none';	
	
	// youtube url
	doc.errorYoutubeURL.value = "";
	document.getElementById("divErrorYoutubeURL").style.display = 'none';		
	
	// video category
	doc.errorVideoCategory.value = "";
	document.getElementById("divErrorVideoCategory").style.display = 'none';
	
	// iAgree
	doc.errorIAgree.value = "";
	document.getElementById("divErrorIAgree").style.display = 'none';	
}

function trimFields(){
	doc = document.EN07Registration;
	
	// city				
	doc.city.value = trim(doc.city.value);
					
	// church				
	doc.church.value = trim(doc.church.value);
	
	// first name
	doc.firstName.value = trim(doc.firstName.value);
	
	// last name
	doc.lastName.value = trim(doc.lastName.value);
	
	// email
	doc.emailAddress.value = trim(doc.emailAddress.value);
	
	// mobile number
	doc.mobileNumber.value = trim(doc.mobileNumber.value);
	
	// youtube url
	doc.youtubeURL.value = trim(doc.youtubeURL.value);
}
			
function sendError(inputType,fieldID,errorFieldID,errorDivID,compareValue,errorMessage){
	doc = document.EN07Registration;
	
	errorField = document.getElementById(errorFieldID);
	errorDiv = document.getElementById(errorDivID);
				
	switch(inputType){
		case "select":
			field = document.getElementById(fieldID);
				
			if(field.options[field.selectedIndex].value==compareValue){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				field.focus();
				return true;
			}
			break;
		case "radio":
			fieldArray = fieldID.split(",");
			unchecked = false;
			for(j=0 ; j<fieldArray.length ; j++){
				field = document.getElementById(fieldArray[j]);
				if(field.checked==true){
					unchecked = true;
				}
			}
			if(unchecked==false){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				field.focus();
				return true;
			}
			break;
		case "checkbox":
			fieldArray = fieldID.split(",");
			unchecked = false;
			for(j=0 ; j<fieldArray.length ; j++){
				field = document.getElementById(fieldArray[j]);
				if(field.checked==true){
					unchecked = true;
				}
			}
			if(unchecked==false){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				field.focus();
				return true;
			}
			break;			
		case "email":
			field = document.getElementById(fieldID);		
			if(field.value!=""){
				if(!isEmailValid(field.value)){
					errorField.value = errorMessage;
					errorDiv.style.display = '';
					field.focus();
					return true;
				}
			}
			break;
		case "youtube":
			field = document.getElementById(fieldID);
			tr = document.getElementById("trCheckVideo");
			
			if(field.value==""){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				field.focus();
				tr.style.display = 'none';
				return true;
			}
			else if(field.value.indexOf("http://www.youtube.com/watch?v=")!=0 && field.value.indexOf("http://youtube.com/watch?v=")!=0){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				tr.style.display = 'none';
				field.focus();
				return true;
			}		
			break;
		case "language":
			fieldArray = fieldID.split(",");
			
			c = document.getElementById(fieldArray[0]);
			s = document.getElementById(fieldArray[1]);
			
			if(c.checked && s.selectedIndex==0){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				s.focus();
				return true;
			}
			break;				
		default:
			field = document.getElementById(fieldID);		
			if(field.value==compareValue){
				errorField.value = errorMessage;
				errorDiv.style.display = '';
				field.focus();
				return true;
			}
			break;
	}
				
	return false;
}
			
function goRegister(){
	doc = document.EN07Registration;
				
	clearErrors();
	trimFields();	
				
	// validate form fields		
	
	if(sendError("radio","isRegistered[0],isRegistered[1]","errorIsRegistered","divErrorIsRegistered","","You must be registered for the conference to join the contest.")){
		// check if radio buttons are checked
		return false;
	}
	else if(!doc.isRegistered[0].checked){
		errorField = document.getElementById("errorIsRegistered");
		errorDiv = document.getElementById("divErrorIsRegistered");		
		errorField.value = "You must be registered for the conference to join the contest.";
		errorDiv.style.display = '';
		errorField.focus();		
		return false;
	}
	else if(sendError("select","country","errorCountry","divErrorCountry","0","Country must be selected.")){
		// country				
		return false;
	}
	else if(sendError("text","city","errorCity","divErrorCity","","City must be filled.")){
		// city
		return false;
	}
	else if(sendError("text","church","errorChurch","divErrorChurch","","Church must be filled.")){
		// church
		return false;
	}
	else if(sendError("text","firstName","errorFirstName","divErrorFirstName","","First name must be filled.")){
		// first name
		return false;
	}	
	else if(sendError("text","lastName","errorLastName","divErrorLastName","","Last name must be filled.")){
		// last name
		return false;
	}
	else if(sendError("radio","gender[Male],gender[Female]","errorGender","divErrorGender","","Gender must be indicated.")){
		// gender
		return false;
	}			
	else if(sendError("select","age","errorAge","divErrorAge","0","Age must be indicated.")){
		// age
		return false;
	}
	else if(sendError("text","emailAddress","errorEmailAddress","divErrorEmailAddress","","Email must be filled.")){
		// email				
		return false;
	}
	else if(sendError("email","emailAddress","errorEmailAddress","divErrorEmailAddress","","Email must be valid.")){
		// email				
		return false;
	}
	else if(sendError("text","mobileNumber","errorMobileNumber","divErrorMobileNumber","","Mobile or Manila-based contact number must be filled.")){
		// mobile number			
		return false;
	}
	else if(sendError("text","youtubeURL","errorYoutubeURL","divErrorYoutubeURL","","Youtube URL must be filled.")){
		// youtube
		tr = document.getElementById("trCheckVideo");
		tr.style.display = 'none';
		return false;
	}	
	else if(sendError("youtube","youtubeURL","errorYoutubeURL","divErrorYoutubeURL","","Youtube URL must be valid.")){
		// youtube
		return false;
	}
	else if(sendError("checkbox","videoCategory[1],videoCategory[2],videoCategory[3]","errorVideoCategory","divErrorVideoCategory","","Video Category must be indicated.")){
		// video category
		return false;
	}
	else if(sendError("checkbox","iAgree","errorIAgree","divErrorIAgree","","Please agree to the rules and terms of the contest.")){
		// iAgree
		return false;
	}
				
	doc.formAction.value = "join";
	doc.submit();					
}			
	
function listenToKeystrokes(){
	// enter
	if(window.event && window.event.keyCode == 13){
		goRegister();
		return false;			
	}	
				
	// arrow-down		
	if(window.event && window.event.keyCode == 40){
		// this.blur();
		return false;
	}								
}						