

function searchoptionchanged() {
	var div_search = document.getElementById("id_search");
	var div_match = document.getElementById("id_match");
	var search = document.getElementById("id_searchOption");
	var match = document.getElementById("id_matchOption");
	var index=search.selectedIndex;
	var optionID = search.options[index].value;

	if(optionID == '4') {
		div_search.style.display = "none";
		div_match.style.display = "block";
		match.options[4].selected = true;
	}
}


function matchoptionchanged() {
	var div_search = document.getElementById("id_search");
	var div_match = document.getElementById("id_match");
	var search = document.getElementById("id_searchOption");
	var match = document.getElementById("id_matchOption");
	var index=match.selectedIndex;
	var optionID = match.options[index].value;
	
	if(optionID != '4') {
		div_search.style.display = "inline";
		div_match.style.display = "none";
		var i = parseInt(optionID);
		search.options[i].selected = true;
	}

}


// Function for Popup Error Message Div
function funcCloseErrMsgDiv() {
	
	var div_err_msg = document.getElementById("err_msg_div");
	div_err_msg.style.display = "none";
}


// -------------------------
// functions for log in
// -------------------------

// Global var to indicate whether need to show popup log in

var popupLOginWindow = false;

function pop_login_button_pressed(buttionAction){
	
	
	if(buttionAction == 'login'){
		//action = login
		if(validateLoginForm(document.login_form)){ 
			// only submit the form after pass the validation
			document.login_form.action.value = 'userLogin';
			document.login_form.submit(); 
		}	
	}
	
	
	if(buttionAction == 'cancel'){
		document.login_form.action.value = 'cancelLogin';
		document.login_form.submit();
	}	
	
	
	
}


function funcLogin() {
	
	//alert('About to log in');
	//Scroll the screen to the top of the window

	
	var div_Login = document.getElementById("box_login");
	div_Login.style.display = "block";
}

function funcCloseLogin() {
	var div_Login = document.getElementById("box_login");
	div_Login.style.display = "none";
	
	
}

function submitLogOutForm(){
	document.logout_form.submit(); 
}

function IsEmpty(aTextField) {
	
	
	   if ((aTextField.value.length==0) ||
	   (aTextField.value==null)) {
	      return true;
	   }
	   else { return false; }
	}	

function IsNumeric(sText)
	
	{
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	
	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
	      { 
	      Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) == -1) 
	         {
	         IsNumber = false;
	         }
	      }
	   return IsNumber;
	   
	 }

	function isValidEmail(str) {

	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	}


	// Suppress form submission when hit enter key

	function checkEnter(e){ //e is event object passed from function invocation
		var characterCode; //literal character code will be stored in this variable

		if(e && e.which){ //if which property of event object is supported (NN4)
			e = e;
			characterCode = e.which; //character code is contained in NN4's which property
		}
		else{
			e = e;
			characterCode = e.keyCode; //character code is contained in IE's keyCode property
		}

		if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
			
			return false;
		}
		else{
			return true;
		}

	}
	
	

	function user_name_changed(aTextField){
		
		var fieldValue = aTextField.value;
		var trimedValue = trimAll(fieldValue); // trim off left right white space
		
		document.quick_registration.userName.value = trimedValue;
		
		if(trimedValue.length < 3 ){
			alert('Please enter a name more than 3 characters.'); 
		    
			
		    setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
	        setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
	        return false;

		}	

		

		
	   	return true;
	   	
	}

	
	
	function email_changed(aTextField){
		
		var fieldValue = aTextField.value;
		var trimedValue = trimAll(fieldValue); // trim off left right white space

		trimedValue  = removeSpaces(trimedValue ); //trim off inside white space
	    	
		document.quick_registration.email.value = trimedValue;
		
		if(trimedValue.length == 0 || (!isValidEmail(trimedValue)) ){
			alert('Please enter a valid email address.'); 
		    	
		    setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
	        setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
	        return false;

		}	
				
	   	return true;
	   	
	}
	
	function email2_changed(aTextField){
		
	   	if((aTextField.value != document.quick_registration.email.value) ){
	   		alert('Please type the same email address.');
	   		
		    setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
		    setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
		    
		    return false;
      
	   	}
	   	
	   	// Check whether got space in the password
	   	var fieldValue = aTextField.value;
		var trimedValue = trimAll(fieldValue); // trim off left right white space

		trimedValue  = removeSpaces(trimedValue ); //trim off inside white space
	    
		if(trimedValue != fieldValue){ // Got space inside
	   		alert('Please take out the "space" charachers.');
	   		
		    setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
		    setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
		    
		    return false;
 			
		}	
	   	
	   	
	   	return true;
	}
	
	
	
	function password_changed(aTextField){
		
		if(aTextField.value.length < 6){
			alert('Please enter a password with more than 6 characters.');
			
			setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
		    setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
		     	      
		    return false;
		}	
			
		
		return true;	
	}
	
	function password2_changed(aTextField){
		
	   	if( aTextField.value != document.quick_registration.password.value ){
	   		alert('Please type the same password.'); 
	   		
	   		setTimeout("document.quick_registration."+aTextField.name+".focus()", 1);
		    setTimeout("document.quick_registration."+aTextField.name+".select()", 1);
		    return false;
	   	}	
	   	
	   	return true;
	}
	
	
	function agree_terms_changed(aCheckbox){
		
		if(aCheckbox.checked == 1){
			document.quick_registration.registerButton.disabled=false;
		}
		else{
			document.quick_registration.registerButton.disabled=true;
		}
		
	}
	
	function validateRegistrationForm(form){
		
		//alert('About to check');
		if(!user_name_changed(form.userName)){
			//alert('About to check user name');
			return false;
		}	
		
		if(!email_changed(form.email)){
			//alert('check email failed');
			return false;
		}
		
		if(!email2_changed(form.email2)){
			//alert('check email 2 failed');
			return false;
		}
		
		if(!password_changed(form.password)){
			//alert('check password failed');
			return false;
		}
		
		if(!password2_changed(form.password2)){
			//alert('About to check password 2 failed');
			return false;
		}
		
		//alert('Checking pass!');
		return true;
	}
	
	
	function login_email_changed(aTextField){
		
		var fieldValue = aTextField.value;
		var trimedValue = trimAll(fieldValue); // trim off left right white space

		trimedValue  = removeSpaces(trimedValue ); //trim off inside white space
	    	
		document.login_form.email.value = trimedValue;
		
		if(trimedValue.length == 0 || (!isValidEmail(trimedValue)) ){
			alert('Please enter a valid email address.'); 
		    	
		    setTimeout("document.login_form."+aTextField.name+".focus()", 1);
	        setTimeout("document.login_form."+aTextField.name+".select()", 1);
	        return false;

		}	
				
	   	return true;
		
	}
	
	
	function login_password_changed(aTextField){
		if(aTextField.value.length == 0){
			alert('Please enter a password.');
			
			setTimeout("document.login_form."+aTextField.name+".focus()", 1);
		    setTimeout("document.login_form."+aTextField.name+".select()", 1);
		     	      
		    return false;
		}	
			
		
		return true;	
	}
	
	function validateLoginForm(form){
		
		if(!login_email_changed(form.email)){
			return false;
		}
		
		if(!login_password_changed(form.password)){
			return false;
		}
				
		return true;
	}
	
	
	
	
	function removeSpaces(string) {
		 return string.split(' ').join('');
	}


	// trim off left and right space
	function trimAll(sString)
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
		return sString;
	} 

	
	// Facebook
	function fb_onlogin(){
		alert('Logged in from Facebook!');
		
		FB.Connect.ifUserConnected("http://www.RafflesStreet.com/FacebookOnLog.jsp?back="+window.location,null);
		//$.post("FacebookOnLog.jsp?back="+window.location);
	}	
	
