/*javascript: for Victory Sermon's registration form
written by: Vanj Dolores 
Date: December 15, 2009
*/
//sisable enter key

//affiliation default
var affiliation = 'fromEN';


function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13){
	      formsubmit();
          return false;
     }
     else{
          return true;
     }
}


//clears errorss
function clearErrors(){
//clear errors
        document.getElementById("err_firstname").style.display = 'none';
		document.getElementById("err_lastname").style.display = 'none';
		document.getElementById("err_nickname").style.display = 'none';
		document.getElementById("err_designation").style.display = 'none';
		document.getElementById("err_church").style.display = 'none';
		document.getElementById("err_email").style.display = 'none';
		document.getElementById("err_username").style.display = 'none';
		document.getElementById("err_pass").style.display = 'none';
		document.getElementById("err_passconfirm").style.display = 'none';
		
}

/*************************************Errors ****************************/
//checks if it's empty
function isEmpty(str){
	if (str == ""){
	return true;
	}
	return false;     
} //end isEmpty

//checks for select options
function isOptionBlank(v, cv){
 if (v == cv){
	return true; 
 }
 return false;
}

//checks if email is single
function isEmailSingle(str){
	if(str.indexOf(".") > 2 && str.indexOf("@") > 0 && str.indexOf(",") < 0 && str.indexOf(";") < 0 && str.indexOf(" ") < 0){
		return false;
   	}
   	else{
		return true;
   	}
	return false;
}

//checks for valid emails <user>@provider.com
function isEmailValid(str){


                if(str.indexOf(".") > 2 && s.indexOf("@") > 0){
					return false;
   				}else {
				return true;
				}
				
				
   				return false;
              
}	

//not matching
function match(s,c){
	ans = s == c;
	return ans;
}

/************************************* End errors ****************************/



//send errors
function sendError(errType,compareValue,fieldID,errorDivID,errMsg){
    errorDiv = document.getElementById(errorDivID);
     
    

	switch (errType){
	case 'Empty':
		field = document.getElementById(fieldID);

		if (isEmpty(field.value)){
		errorDiv.style.display = 'block';
		errorDiv.style.border = '1px solid #ff5f5f';
		errorDiv.style.padding = '3px';
		errorDiv.innerHTML = errMsg;
		field.focus();
		return true;
		}
	break;
	
	case 'noOption':
	    
	
		field = document.getElementById(fieldID);
		var str = field.value;
	   
		if (isOptionBlank(str,compareValue)) {
		errorDiv.style.display = 'block';
		errorDiv.innerHTML = errMsg;
		errorDiv.style.border = '1px solid #ff5f5f';
		errorDiv.style.padding = '3px';	
        field.focus();
		return true;	
		}
		
	break;
	
    case 'notSingleEmail':
		field = document.getElementById(fieldID);
		var str = field.value;
	   
		
		if (isEmailSingle(str)){ 
		//if email is not single
		errorDiv.style.display = 'block';
		errorDiv.innerHTML = errMsg;
		errorDiv.style.border = '1px solid #ff5f5f';
		errorDiv.style.padding = '3px';	
        field.focus();
		return true;	
		}
	break;
	
	
	case 'notValidEmail':
		field = document.getElementById(fieldID);
		var str = field.value;
	   
	  
	   
	   
		if (isEmailValid(str)){ 
		//if email is not valid
		errorDiv.style.display = 'block';
		errorDiv.innerHTML = errMsg;
		errorDiv.style.border = '1px solid #ff5f5f';
		errorDiv.style.padding = '3px';	
        field.focus();
		return true;	
		}
		
		
	break;
	
	case 'notMatch':
		field = document.getElementById(fieldID);
		var str = field.value;
		
		if (!match(str,compareValue)){ 
		//password does not match
		errorDiv.style.display = 'block';
		errorDiv.innerHTML = errMsg;
		errorDiv.style.border = '1px solid #ff5f5f';
		errorDiv.style.padding = '3px';	
        field.focus();
		return true;	
		}
	break;

	default:
	break;
	}

}


//function that actuall submits
function submit(){
doc = document.register;
document.register.wlpeRegisterButton.name= "service";
document.register.wlpeRegisterButton.value= "register"; 

//submits the form
doc.submit();
} //end of submit

//show and hide div
function showhidediv(divID,opt){

	divID = divID.split(',');
    i = divID.length;
	//array for split
	c = i-1;
	
	
	

	switch (opt){
	case 'show':
		for (var x = 0; x <= c; x++)
		{
		  div = document.getElementById(divID[x]);
		  div.style.display = 'block';
		}	
	break;
	case 'hide': 
		for (var x = 0; x <= c; x++)
		{
		 div = document.getElementById(divID[x]);
		 div.style.display = 'none';
		} 	
	break;
	
	default:
	break;
	}
	
}

function defineForm(hideID,showID){
            
			
	
			//hide this ID
			showhidediv( hideID,'hide');
			//show this ID
			showhidediv(showID,'show');
			
			affiliation = showID;
		
}//end of defineForm

//form validate
function formValidate(){


doc = document.register;


			//error checks
            if (sendError("Empty","","firstname","err_firstname","Input First Name.")){
			   //first name
			   return false;
			}else if (sendError("Empty","","lastname","err_lastname","Input Last Name.")){
				//last name
			   return false;
			}else if(sendError("Empty","","nickname","err_nickname","Input Nickname.")){
				//nickname 
			   return false;
			}else if (sendError("noOption","","registrantdesignation","err_designation","Please choose your designation")){
				//designation
				return false;
			}
			
			//if from EN check if option was selected
			//if not on the list, check if church text box's not empty
            
			if (affiliation == 'fromEN'){
			
			  //should choose from the options
			  if (sendError("noOption","","registrantENID","err_church","Please choose your Every Nation church from the list.")){
						return false;
			  }
			   doc.churchID.value = doc.registrantENID.value;
			  
			}else if (affiliation == 'notListed'){
				doc.churchID.value = 'na';
					
				if (sendError("Empty","","church","err_church","Input church.")){
							return false;
				}
				
			}
			
			
			if(sendError("Empty","","wlpeRegisterEmail","err_email","Input email address.")){
				//email
			   return false;
			}
			
			
			
			//pass the fullname name + last name
			fn = doc.firstname.value;
			ln = doc.lastname.value;
			
			
			doc.wlpeRegisterFullName.value = fn + ' ' + ln;
			submit();
}

//form submit
function formsubmit(){
	//clear errors
	clearErrors();
	//validate form
	formValidate();
}

