// JavaScript Document
function showError(divid,errmsg){

	$("#"+divid).fadeTo(200,0.1,function() //start fading the messagebox
	{ 
		//add message and change the class of the box and star	t fading			
		$(this).html(errmsg).removeClass("success_div").addClass("error_div").fadeTo(900,1);
	});	
	
}
function hideError(divid){
	$("#"+divid).fadeTo(200,0.1,function() //start fading the messagebox
	{ 
		//add message and change the class of the box and star	t fading			
		$(this).html('').removeClass("error_div").removeClass("success_div").fadeTo(900,1);
	});

}

//====================================================================================================================================================================================

function validateRegistration(form){

selbox = form.membership_type_id.options[form.membership_type_id.selectedIndex].value;

	if(selbox == ''){
		showError("divMtype","<br />    Please select a membership type");
		return false;
	}else{
		hideError("divMtype");
	}

	if(form.username.value.length==0){ 
		showError("divUsername","<br />    Username cant be empty");
		return false;
	}
	
	if(form.pwd.value.length==0){
		showError("divPwd","<br />    Password cant be empty");
		return false;
	}else{
		hideError("divPwd");
	}
	
	if(form.cpwd.value.length==0){
		showError("divCPwd","<br />   Confirm Password cant be empty");
		return false;
	}else{
		hideError("divCPwd");
	}
	
	if(form.cpwd.value != form.pwd.value){
		showError("divCPwd","<br />   Password Mismatch");
		form.cpwd.value = '';
		return false;
	}else{
		hideError("divCPwd");
	}
	
	if(form.first_name.value.length==0){
		showError("divFname","<br />    First Name cant be empty");
		return false;
	}else{
		hideError("divFname");
	}
	
	if(form.last_name.value.length==0){
		showError("divLname","<br />    Last Name cant be empty");
		return false;
	}else{
		hideError("divLname");
	}
	
	if(form.email_id.value.length==0){
		showError("divEmail","<br />    Email cant be empty");
		return false;
	}else{
		hideError("divEmail");
	}
	
	if(form.email_id.value.length>0){
		is_valid_email =CheckEmailAddr(form.email_id.value);
		if(!is_valid_email){
			showError("divEmail","<br />  Enter a valid email address");
			return false;
		}else{
			hideError("divEmail");
		}
	}


        
	
	if(selbox == 1 || selbox == 2 || selbox == 3 ){	
		if(form.company.value.length==0){
			showError("divCompanyName","<br />    Please enter your company name");
			return false;
		}
	}
	//alert("company="+(form.ajax_company.value)+" username="+form.ajax_username.value)
	if( form.ajax_username.value == 'allow' ){
		
		if(form.ajax_company.value != null && form.ajax_company.value == 'dontallow'){
			return false;
		}else{
			return true;
		}
	}else{
		return false;
	}
	
	
}

//====================================================================================================================================================================================
function validateLogin(form){
	
	if(form.loginusername.value==""){
		hideError("msgBox")
		showError("divLoginBox"," Please enter your username");
		return false;
	}
	
	if(form.loginpwd.value==""){
		hideError("msgBox")
		showError("divLoginBox","Please enter your password");
		return false;
	}
	
}
//====================================================================================================================================================================================
function validateForgotPassword(form){
	
	if(form.forgot_email_id.value==""){
		showError("divForgotPwdMsg"," Please enter your email address");
		return false;
	}
	
	if(form.forgot_email_id.value.length>0){
		is_valid_email =CheckEmailAddr(form.forgot_email_id.value);
		if(!is_valid_email){
			showError("divForgotPwdMsg"," Enter a valid email address");
			return false;
		}else{
			hideError("divForgotPwdMsg");
		}
	}
	
}
//====================================================================================================================================================================================
function validateProfile(form){
	
	if(form.first_name.value==""){
		showError("divFname","<br /> Please enter your first name");
		return false;
	}
	
	if(form.last_name.value==""){
		showError("divLname","<br /> Please enter your last name");
		return false;
	}
	
	if(form.email_id.value==""){
		showError("divEmail","<br /> Please enter your email id");
		return false;
	}
	
	if(form.email_id.value !=""){
		is_valid_email2 = CheckEmailAddr(form.email_id.value); //alert(is_valid_email2)
		if(!is_valid_email2){
			showError("divEmail","<br />  Enter a valid email address");
			return false;
		}
		
	}

}
//====================================================================================================================================================================================

function validateEventRegNonMembers(form){
	
	if(form.first_name.value==""){
		showError("divFname","<br /> Please enter your first name");
		return false;
	}
	
	if(form.last_name.value==""){
		showError("divLname","<br /> Please enter your last name");
		return false;
	}

	
	if(form.email_id.value==""){
		showError("divEmail","<br /> Please enter your email id");
		return false;
	}
	
	if(form.email_id.value !=""){
		is_valid_email2 = CheckEmailAddr(form.email_id.value); //alert(is_valid_email2)
		if(!is_valid_email2){
			showError("divEmail","<br />  Enter a valid email address");
			return false;
		}
		
	}

        if(form.phone.value.length>0){
		valid_phone =is_valid_phone(form.phone.value);

		if(!valid_phone){
			showError("divPhone","<br />  Phone No must not have characters other than 0 to 9 () hyphen and space");
			return false;
		}else{
			hideError("divPhone");
		}
	}
	
}
//====================================================================================================================================================================================

function validationAddMembers(form){
	
uname = 	document.getElementsByName("username[]");

email = 	document.getElementsByName("email_id[]");


tot_rows = uname.length;

	forEmptyCheckloop:
	
	for(i=0; i< parseInt(tot_rows); i++){
		
		if(uname[i].value !="" && email[i].value != ""){
			
			all_rows_empty = false;
			break  forEmptyCheckloop;
			
		}else{
			
			all_rows_empty = true;
			
		}
		
	}
	
	
	if(all_rows_empty){
		
		showError("divAddMembers"," Please add atleast one member");
		return false;
		
	} 
	
	// IF ALL ROWS ARE NOT EMPTY THEN CHECK FOR THE FOLLOWING
		
		for(i=0; i< parseInt(tot_rows); i++){
			
			line_no =parseInt(i)+1;	
			
		 if(uname[i].value !="" || email[i].value !=""){ // checking if atleast one among username or email is filled
			
			if(uname[i].value ==""){
				
				showError("divAddMembers"," Enter an username on line "+line_no);
				
				return false;
				
			}
			
			if(email[i].value ==""){
				
				showError("divAddMembers"," Enter an email id on line "+line_no);
				
				return false;
				
			}
			
			
			is_valid_email_id =	CheckEmailAddr(email[i].value);
			
			if( !is_valid_email_id ) {
					
				
				
				showError("divAddMembers"," Enter a valid email id on line "+line_no);
				
				return false;
				
			}
			
		  }
		
		}
		
		username_error_line = checkDuplicates("username[]", tot_rows );
		
		if( parseInt(username_error_line) > 0 ){ //alert("error_line"+username_error_line)
		
			showError("divAddMembers"," Duplicate username on line "+username_error_line);
			
			return false;
			
		}
		
		emailid_error_line = checkDuplicates("email_id[]", tot_rows );
		
		if( parseInt(emailid_error_line) > 0 ){ //alert("error_line"+username_error_line)
		
			showError("divAddMembers"," Duplicate email id on line "+emailid_error_line);
			
			return false;
			
		}
		

	
	
}

function checkDuplicates(entity,tot_rows){
	line_no = 0;
	obj = eval(document.getElementsByName(entity)); //alert(obj)
	forOuterloop:
	for(i=0; i< parseInt(tot_rows); i++){
			
			
			if( obj[i].value !="" ){
				
				for(j=0; j< parseInt(tot_rows); j++){
						
					if( i!=j &&  obj[j].value == obj[i].value ){
						
						line_no = parseInt(j)+1;  //alert("inside")
						obj[j].style.borderColor = "#c4352e";
						
						break forOuterloop;
						
						
					}
				
				}
				
			}
			
			
	 }
	
	
	return line_no; 
	
}

function setBorder(obj){
	obj.style.borderColor = "#ccc";
}
