// Required Functions


function isString(str)
{
	patternname=/^[a-zA-Z]+$/;
	if(patternname.test(str))
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function isDigit(c)
{
  return ((c >= "0") && (c <= "9"));
}


// Returns true if all chars in string s are numbers;
// first character is allowed to be + or -; does not 
// accept floating point, exponential notation, etc.

function isInteger(s)
{
  //if (isBlank(s))
   // return false;

  // skip leading + or -
  if ((s.charAt(0) == "-") || (s.charAt(0) == "+"))
    var i = 1;
  else
    var i = 0;

  // Search through string's chars one by one until we find a 
  // non-numeric char, then return false; if we don't, return true
  for (i; i<s.length; i++)
  { 
      
		// Check that current character is number
		var c = s.charAt(i);
		if (!isDigit(c)) 
		  return false;
  }
  // All characters are numbers
  return true;
}

function isPhone(phone,no)
{
	var temp="";
	if(phone.length<no)
		return false;
	for(var i=0;i<phone.length;i++)
	{
			temp=phone.charAt(i);
			if(!(temp>="0" && temp<="9"))
				return false;
	}
	return true;
}	

// Validation for Sign Up form...

function validate_signup(form)
{
	 

	var title=form['sel_title'];
	 var first_name=form['txt_fname'];
	var last_name=form['txt_lname'];
	var gender = form['sel_gender'];
	var address=form['txt_staddr'];
	var city=form['txt_state'];
	var state=form['txt_city'];
	var zip_code=form['txt_zip'];
	var country=form['sel_country'];
	var country_opt=form['is_us'];
	var is_us=country_opt[0].checked;
	 var ot_city=form['txt_otcity'];
	 var ot_state=form['txt_otstate'];
	 var ot_zip=form['txt_otzip'];
	 var ot_zone=form['txt_otzone'];
	var phone1=form['txt1_phone1'];
	var phone11=form['txt2_phone1'];
	var phone12=form['txt3_phone1'];
	var phone2=form['txt1_phone2'];
	var phone21=form['txt2_phone2'];
	var phone22=form['txt3_phone2'];
	var institute=form['txt_institute'];
	var insti_city=form['txt_insti_city'];
	var insti_state=form['txt_insti_state'];
	var rdgrade= form['rd_institue'];
	var user_id = form['txt_uname'];
	var pass = form['txt_pass'];
	var question = form['sel_que'];
	var ans = form['txt_ans'];
   var grd_schl= form['sel_school'] ;
   var grd_clg= form['sel_college'];
   var stu_email= form['txt_email1'];
    var chk1= form['chk1'];  var chk2= form['chk2'];var chk3= form['chk3'];var chk4= form['chk4'];var chk5= form['chk5'];var chk6= form['chk6'];
    var chk7= form['chk7'];var chk8= form['chk8'];
    var ref_f=form['refer_fname'] ;
      var ref_l=form['refer_lname'] ;
      var ref_e=form['refer_email'] ;
        var stat=form['station_name'] ;
        var srch=form['search_engine'] ;
         var src=form['source'] ;
   var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

 if(first_name.value==""){
		alert("Please specify your First Name.");
		first_name.focus();
		return false;
	}
	else if(!isString(first_name.value)) {
		alert("Please specify a valid First Name.");		
		first_name.value="";
		first_name.focus();
		return false;
	}
	 if(last_name.value==""){
		alert("Please specify your Last Name.");		
		last_name.focus();
		return false;
	}
	else if(!isString(last_name.value)) {
		alert("Please specify a valid Last Name.");		
		last_name.value="";
		last_name.focus();
		return false;
	}
	 if(gender.value==""){
		alert("Please specify your Gender.");		
		gender.focus();
		return false;
	}
		 if(stu_email.value.replace(/^\s*|\s*$/g,'')=="")
  {
    alert("Please enter student's email address.");
		stu_email.focus();
		return false;
}

 else if(!stu_email.value.match(filter))
  {
    alert("Please enter valid email address.");
		stu_email.focus();
		return false;
}

	 if(address.value==""){
		alert("Please specify your Address.");		
		address.focus();
		return false;
	}
	else if(isInteger(address.value)==true){
		alert("Please specify a valid Address.");
		address.value="";
		address.focus();
		return false;
	}
	if(country.value=="" && is_us==false){
		alert("Please specify your Country.");		
		country.focus();
		return false;
	}
	if(state.value=="" && ot_state.value=="" ){
		alert("Please specify your State.");
		state.focus();
		return false;
	}
	else if( (state.value != "" && isInteger(state.value)==true ) || (ot_state.value != "" && isInteger(ot_state.value)==true )){
		alert("Please specify a valid State.");
		state.value="Select State";
		state.focus();
		return false;
	}
	if(ot_zone.value=="" && is_us==false){
		alert("Please specify your time zone.");		
		country.focus();
		return false;
	}
	/*if(city.value=="" && ot_city.value==""){
		alert("Please specify your City.");		
		city.focus();
		return false;
	}
	else if((city.value != "" && isInteger(city.value)==true ) || (ot_city.value != "" && isInteger(ot_city.value)==true )){
		alert("Please specify a valid City.");
		city.value="";
		city.focus();
		return false;
	}*/
	

	
	if(phone1.value=="" || phone11.value=="" || phone12.value==""){
		alert("Please specify your Phone Number.");		
		phone1.focus();
		return false;
	}
	else if(!isPhone(phone1.value,3)==true){
		alert("Please specify a valid Phone Number.");
		phone1.value ="";
		phone1.focus();
		return false;
	}
	else if(!isPhone(phone11.value,3)==true){
		alert("Please specify a valid Phone Number.");
		phone11.value ="";
		phone11.focus();
		return false;
	}
	else if(!isPhone(phone12.value,4)==true){
		alert("Please specify a valid Phone Number.");
		phone12.value ="";
		phone12.focus();
		return false;
	}
	if(phone2.value!="" || phone21.value!="" || phone22.value!=""){

		  if(!isPhone(phone2.value,3)==true){
				alert("Please specify a valid Phone Number.");
				phone2.value ="";
				phone2.focus();
				return false;
			}
			else if(!isPhone(phone21.value,3)==true){
				alert("Please specify a valid Phone Number.");
				phone21.value ="";
				phone21.focus();
				return false;
			}
			else if(!isPhone(phone22.value,4)==true){
				alert("Please specify a valid Phone Number.");
				phone22.value ="";
				phone22.focus();
				return false;
			}
	}
if (!rdgrade[0].checked && !rdgrade[1].checked) {
alert("Please choose a grade");
return false;
} if(rdgrade[0].checked && grd_schl.value=="")
{
 alert("Select your grade level");
return false;
} if(rdgrade[1].checked && grd_clg.value=="")
{
 alert("Select your grade level");
return false;
}

	if(institute.value==""){
		alert("Please specify your Institution Name.");		
		institute.focus();
		return false;
	}
	else if(isInteger(institute.value)==true){
		alert("Please specify a valid Institution Name.");
		institute.value="";
		institute.focus();
		return false;
	}
	 if(insti_city.value==""){
		alert("Please specify your Institution City.");		
		insti_city.focus();
		return false;
	}
	else if(isInteger(insti_city.value)==true){
		alert("Please specify a valid Institution City.");
		insti_city.value="";
		insti_city.focus();
		return false;
	}
	if(insti_state.value==""){
		alert("Please specify your Institution State.");		
		insti_state.focus();
		return false;
	}
	else if(isInteger(insti_state.value)==true){
		alert("Please specify a valid Institution State.");
		insti_state.value="";
		insti_state.focus();
		return false;
	}
	
 if(!chk1.checked && !chk2.checked && !chk3.checked && !chk4.checked && !chk5.checked && !chk6.checked && !chk7.checked && !chk8.checked)
   {
    alert("Please specify the source from which you know about the company.");
 		return false;
 }
 /*if(chk1.checked)
 {  if(ref_f.value=="")   {
  	alert("Please specify friend's First Name.");
		ref_f.focus();
		return false;
}
 else if(ref_l.value=="")   {
  	alert("Please specify friend's Last Name.");
		ref_l.focus();
		return false;
		}
    if(ref_e.value=="")   {
  	alert("Please specify friend's E-mail.");
		ref_e.focus();
		return false;
		}

 }*/
 if(chk4.checked)
 {  if(stat.value=="")   {
  	alert("Please specify either TV/Radio and station name.");
		stat.focus();
		return false;
}
  }
  if(chk7.checked)
 {  if(srch.value=="")   {
  	alert("Please specify search engine name.");
		srch.focus();
		return false;
}
  }
  if(chk8.checked)
 {  if(src.value=="")   {
  	alert("Please specify the source.");
		src.focus();
		return false;
}
  }

}



function validate_signup_rest(form)
{
	 
	
	var user_name = form['txt_uname'];
	var pass = form['txt_pass'];
	var cpass = form['txt_cpass'];
	var question = form['sel_que'];
	var ans = form['txt_ans'];
	var len = pass.value;

   var filter = /^([a-zA-Z0-9_\.\-])+([a-zA-Z0-9\-])$/;

	if(user_name.value.replace(/^\s*|\s*$/g,'')==""){
		alert("Please specify your User Name..");
		user_name.focus();
		return false;
	}
	 else if(!user_name.value.match(filter))
  {
    alert("Please enter valid Username. Allowed only usernames with a combination of the (A-Z,a-z,0-9,-). No space allowed.");
		user_name.focus();
		return false;
  }
	else if(pass.value.replace(/^\s*|\s*$/g,'')==""){
		alert("Please enter Password..");		
		pass.focus();
		return false;
	}
	 else if(!pass.value.match(filter))
  {
    alert("Please enter a password having a combination of (A-Z,a-z,0-9,-). No space allowed.");
		pass.focus();
		return false;
  }
	else if(len.length < 6)
	{
		alert("Password should be of minimum six characters..");
		pass.value = "";
		pass.focus();
		return false;
	}
	else if(pass.value != cpass.value)
	{
		alert("Passwords are not matching..");
		pass.value="";
		cpass.value="";
		pass.focus();
		return false;
	}
	else if(question.value == "")
	{
		alert("Please select Question..");
		question.focus();
		return false;
	}
	else if(ans.value == "")
	{
		alert("Please fill Answer field..");
		ans.focus();
		return false;
	}
	
}