//function to check if the value is not having only blanks
//if it is so then this funtion will return true else false
function isblank_text(txt)
{
	title1=new String(txt);
	len=title1.length;
	//alert(len);
	tot_space=0;
	for(i=0;i<len;i++)
	{
		if(title1.charAt(i)==' ')
		{
			tot_space++;
		}
	}
	//alert("tot_place:"+tot_space+"\nlen:"+len);
	if(tot_space==len)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//function to validate send message form
function validate_sendmessage(frm,reply) 
{
	if(!reply)
	{
		if(frm.txttouser.value=="" || frm.txttouser.value==null)
		{
			alert("Username cannot be left blank!!");
			frm.txttouser.focus();
			return false;
		}
		txt=new String(frm.txttouser.value);
		if(txt.length>0)
		{
			if(isblank_text(txt))
			{
				alert("Only spaces are not allowed as username!!");
				frm.txttouser.focus();		
				return false;
			}	
		}
	}
	
	if(frm.txtsubject.value=="" || frm.txtsubject.value==null)
	{
		alert("Subject cannot be left blank!!");
		frm.txtsubject.focus();
		return false;
	}
	txt=new String(frm.txtsubject.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as Subject!!");
			frm.txtsubject.focus();		
			return false;
		}	
	}	
	
	if(frm.txtmessage.value=="" || frm.txtmessage.value==null)
	{
		alert("Message cannot be left blank!!");
		frm.txtmessage.focus();
		return false;
	}
	txt=new String(frm.txtmessage.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as message!!");
			frm.txtmessage.focus();		
			return false;
		}	
	}	
}

//function to validate forward message form
function validate_forwardmsg(frm) 
{
	if(frm.txttouser.value=="" || frm.txttouser.value==null)
	{
		alert("Username cannot be left blank!!");
		frm.txttouser.focus();
		return false;
	}
	txt=new String(frm.txttouser.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as username!!");
			frm.txttouser.focus();		
			return false;
		}	
	}
	if(frm.txtsubject.value=="" || frm.txtsubject.value==null)
	{
		alert("Subject cannot be left blank!!");
		frm.txtsubject.focus();
		return false;
	}
	txt=new String(frm.txtsubject.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as Subject!!");
			frm.txtsubject.focus();		
			return false;
		}	
	}	
	
	if(frm.txtmessage.value=="" || frm.txtmessage.value==null)
	{
		alert("Message cannot be left blank!!");
		frm.txtmessage.focus();
		return false;
	}
	txt=new String(frm.txtmessage.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as message!!");
			frm.txtmessage.focus();		
			return false;
		}	
	}
	return true;	
}

//function to validate invite friend form
function validate_invite(frm) 
{
	if(frm.txtemailids.value=="" || frm.txtemailids.value==null)
	{
		alert("Username cannot be left blank!!");
		frm.txtemailids.focus();
		return false;
	}
	txt=new String(frm.txtemailids.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as username!!");
			frm.txtemailids.focus();		
			return false;
		}	
	}
	if(frm.txtmessage.value=="" || frm.txtmessage.value==null)
	{
		alert("Message cannot be left blank!!");
		frm.txtmessage.focus();
		return false;
	}
	txt=new String(frm.txtmessage.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as message!!");
			frm.txtmessage.focus();		
			return false;
		}	
	}	
	return true;
}

//function to validate the login form
function isblank_login(frm)
{
	if(frm.txtusername.value=="" || frm.txtusername.value==null)
	{
		alert("User Name cannot be left blank!!");
		frm.txtusername.focus();
		return false;
	}
	txt=new String(frm.txtusername.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as username!!");
			frm.txtusername.focus();		
			return false;
		}	
	}
	if(frm.txtpassword.value==null || frm.txtpassword.value=='')
	{
		alert("Password cannot be left blank!!");
		frm.txtpassword.focus();			
		return false;
	}
	txt=new String(frm.txtpassword.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as password!!");
			frm.txtpassword.focus();		
			return false;
		}	
	}
}

//function to validate the forgot login form
function isblank_forget(frm)
{
	if(frm.txt1.value=="" || frm.txt1.value==null)
	{
		alert("User Name cannot be left blank!!");
		frm.txt1.focus();
		return false;
	}
	txt=new String(frm.txt1.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as username!!");
			frm.txt1.focus();		
			return false;
		}	
	}
}
function validate_search(frm)
{
	if(frm.txtsearch.value == null || frm.txtsearch.value == "")
	{
		alert("Please enter search text!!");
		frm.txtsearch.focus();
		return false;
	}
	txt=new String(frm.txtsearch.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as search text!!");
			frm.txtsearch.focus();		
			return false;
		}	
	}
	return true;
}

///function to validate post video form
function validate_postvideo(frm)
{	
	if(frm.selcategory.value == "-")
	{
		alert("Please Select the Category!!");
		frm.selcategory.focus();
		return false;
	}
	if(frm.txttitle.value == "")
	{
		alert("Title cannot be left blank!!");
		frm.txttitle.focus();
		return false;
	}
	if(frm.txtembeded.value == "")
	{
		alert("Embeded code cannot be left blank!!");
		frm.txtembeded.focus();
		return false ;
	}
	if(frm.turing_number.value=="")
	{
		alert("Please fill up Turning Number!!");
		frm.turing_number.focus();
		return false;
	}
	return true;	
}
//function to validate the registration form
function validate_registration(frm,edit)
{
	//validate email address
	if(frm.txtemail.value=="" || frm.txtemail.value==null)
	{
		alert("Email address cannot be left blank!!");
		frm.txtemail.focus();
		return false;
	}	
	//check for all the fields if they are not having only spaces
	txt=new String(frm.txtemail.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as email address!!");
			frm.txtemail.focus();		
			return false;
		}	
	}
	if(!emailcheck(txt))
	{
		frm.txtemail.focus();		
		return false;
	}
	
	//validate username
	if(frm.txtusername.value=="" || frm.txtusername.value==null)
	{
		alert("User name cannot be left blank!!");
		frm.txtusername.focus();
		return false;
	}
	//check for all the fields if they are not having only spaces
	txt=new String(frm.txtusername.value);
	if(txt.length>0)
	{
		if(isblank_text(txt))
		{
			alert("Only spaces are not allowed as user name!!");
			frm.txtusername.focus();		
			return false;
		}	
	}
	//if(isAlphanum(frm.txtusername.value)==false)
	//{
	//	alert("Username can only contain alphanumeric characters!!");
	//	frm.txtusername.focus();
	//	return false;
	//}		
	var do_password_validation;
	do_password_validation = true;	
	if(edit == "true")
	{
		if((frm.txtoldpassword.value == null || frm.txtoldpassword.value == '') && (frm.txtcpassword.value == null || frm.txtcpassword.value == '') && (frm.txtpassword.value == null || frm.txtpassword.value == ''))
		{
			do_password_validation = false;
		}
		else
		{
			//validate old password field
			if(frm.txtoldpassword.value==null || frm.txtoldpassword.value=='')
			{
				alert("Old password cannot be left blank!!");
				frm.txtoldpassword.focus();			
				return false;
			}
			if(frm.txtoldpassword.value.length < 6 )
			{
				alert("Old password must contain atleast 6 characters!!");
				frm.txtoldpassword.focus();
				return false;
			}
			txt=new String(frm.txtoldpassword.value);
			if(txt.length>0)
			{
				if(isblank_text(txt))
				{
					alert("Only spaces are not allowed as Old password!!");
					frm.txtoldpassword.focus();		
					return false;
				}	
			}
		}
	}
	else
	{
		//validate turing no field
		if(frm.turing_number.value.length < 6 )
		{
			alert("Turing no cannot be left blank!!");
			frm.turing_number.focus();
			return false;
		}
		txt=new String(frm.turing_number.value);
		if(txt.length>0)
		{
			if(isblank_text(txt))
			{
				alert("Only spaces are not allowed as turing no!!");
				frm.turing_number.focus();		
				return false;
			}	
		}
	}
	
	if(do_password_validation == true)
	{
		//validate password fields
		if(frm.txtpassword.value==null || frm.txtpassword.value=='')
		{
			alert("Password cannot be left blank!!");
			frm.txtpassword.focus();			
			return false;
		}
		if(frm.txtpassword.value.length < 6 )
		{
			alert("Password must contain atleast 6 characters!!");
			frm.txtpassword.focus();
			return false;
		}
		txt=new String(frm.txtpassword.value);
		if(txt.length>0)
		{
			if(isblank_text(txt))
			{
				alert("Only spaces are not allowed as password!!");
				frm.txtpassword.focus();		
				return false;
			}	
		}
		
		if(frm.txtcpassword.value==null || frm.txtcpassword.value=='')
		{
			alert("Confirm Password cannot be left blank!!");
			frm.txtcpassword.focus();			
			return false;
		}
		txt = new String(frm.txtcpassword.value);
		if(txt.length>0)
		{
			if(isblank_text(txt))
			{
				alert("Only spaces are not allowed as Confirm password!!");
				frm.txtcpassword.focus();		
				return false;
			}	
		}
		if(frm.txtpassword.value!="" && frm.txtpassword.value!="" && frm.txtpassword.value!=frm.txtcpassword.value)
		{
			alert("Password and Confirm password must be the same!!");
			frm.txtcpassword.focus();			
			return false;		
		}
	}//end if (do_password_validation)
	if(frm.selcountry.value == "-")
	{
		alert("Please select the country!!");
		frm.selcountry.focus();
		return false;
	}
	if(frm.selyear.value == "-")
	{
		alert("Please select the year!!");
		frm.selyear.focus();
		return false;
	}
	if(frm.selmonth.value == "-")
	{
		alert("Please select the month!!");
		frm.selmonth.focus();
		return false;
	}
	if(frm.seldate.value == "-")
	{
		alert("Please select the date!!");
		frm.seldate.focus();
		return false;
	}

	return true;		
}

//function to validate email id
function emailcheck(str,idofstr)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (typeof idofstr == "undefined" || idofstr==null || idofstr=='')
	{
		var idofstr="E-mail ID";
	}
	if (str.indexOf(at)==-1)
	{
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	    //alert("Invalid E-mail ID");
	    alert("Invalid "+idofstr+"!!");
	    return false;
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(" ")!=-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	 
	return true					
}


var numb = '0123456789.';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

//general function to check whether the given parm is valid according to the given val
//val can be any of the single or combination of the above three variables
//if there is any need to validate for special characters than one var can be added and allowed chars can be stored
function isValid(parm,val)
{
  if (parm == "")
  {
  	 return true;
  }
  for (i=0; i<parm.length; i++) 
  {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
//function to check numeric values
function isNum(parm)
{
	return isValid(parm,numb);
}

//function to check for lower case
function isLower(parm)
{
	return isValid(parm,lwr);
}

//function to check for upper case
function isUpper(parm)
{
	return isValid(parm,upr);
}

//function to check for aplhabetic value
function isAlpha(parm) 
{
	return isValid(parm,lwr+upr);
}

//function to check for aplhanumeric value
function isAlphanum(parm) 
{
	return isValid(parm,lwr+upr+numb);
} 
function isAlphaSpace(parm) 
{
	return isValid(parm,lwr+' '+upr);
} 
function emailCheck1 (emailStr) 
{
	var checkEmail = "@.";
	var checkStr = emailStr;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0; j < checkEmail.length; j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			if (EmailAt && EmailPeriod)
				break;
			if (j == checkEmail.length)
				break;
		}
		 
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid)
	{
		alert("The \"Email ID\" must contain \"@\" and a \".\"");
		return (false);
	} 
	return true;
}
function validate_bookmark(frm)
{
	return true;	
}
