// function to remove leading & trailing spaces from a string
function trim(strField)
{
   //Count Leading Spaces 
   for(loop = 0, space = 0; strField.substr(loop, 1) == " " && loop < strField.length; loop++, space++);
   //Remove Leading Spaces 
   strField = strField.substr(space, (strField.length-space));
	
   //Count Trailing Spaces 
   for(loop = strField.length, space = 0; strField.substr(loop-1, 1) == " " && loop > 0; loop--,space++);
   //Remove Trailing Spaces 
   strField = strField.substr(0, strField.length-space);

   //Return Trimed String
   return strField;
}
whitespace = "\t \n\r";
function isEmptyString(s)
{
   var i;
   if((s == null) || (s.length == 0)) return true;
   for(i=0;i < s.length;i++)
   {
      var currchar = s.charAt(i);
  	  if(whitespace.indexOf(currchar) == -1) return false;
   }
   return true;
}

function isEmail(strEmail)
{
  if ((strEmail==null) || (strEmail.length==0))
  {
	return true;
  }
  if (isEmptyString(strEmail)) return false;
  var i=1;
  var nLength=strEmail.length;
  while((parseInt(i) < parseInt(nLength)) && (strEmail.charAt(parseInt(i)) != '@'))
  {
	i++;
  }
  if ((parseInt(i) >= parseInt(nLength)) || (strEmail.charAt(i)!="@"))
  {
    return false;	
  }	
  else i+=2;
  while((i<nLength) && (strEmail.charAt(i)!="."))
  {
	i++;
  }
  if ((i>=nLength-1) || (strEmail.charAt(i)!="."))
  {
	return false;	
  }	
  else return true;		
}

//***************function to check numeric value***************//
function isNotNumeric(s)
{
  	if(isNaN(s))
  	{
  		return(true);
  	}
  	return(false);
}
//----------**end of function to check numeric value**----------//

//*****************function for validate user name*****************************//
function ValidUserid(str)
{
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-";
  	var checkOK1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.";
	var checkStr = str;
 
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
  	{
  	  ch = checkStr.charAt(i);
  	  ch1 = checkStr.charAt(0);
	  for (k=0; k < checkOK1.length; k++)
   	  if (ch1 == checkOK1.charAt(k))
      break;
      if (k == checkOK.length)
      { 
       allValid = false;
       break;
      }
      for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
      if (j == checkOK.length)
      {
       allValid = false;
       break;
      }
    }
	if(isEmptyString(str))
	{
	allValid = false;
	}
   if (!allValid)
   {
     return false;
   } 
   return allValid;
 }
 //----------**end of function for validate username**----------//


function checkEvent(chk, status)
{
	var chkName1, chkName2;
	
	if(chk.name=='type[0]' || chk.name=='type[1]')
	{
		chkName1 = document.getElementsByName('type[0]')[0];
		chkName2 = document.getElementsByName('type[1]')[0];
		//chkName1 = document.all['type[0]'];
		//chkName2 = document.all['type[1]'];
	}
	else
	{
		chkName1 = document.getElementsByName('type[2]')[0];
		chkName2 = document.getElementsByName('type[3]')[0];
		//chkName1 = document.all['type[2]'];
		//chkName2 = document.all['type[3]'];
	}
	
	if (typeof chkName1 != 'undefined')
	{
		for (i=0; i < chkName1.length; i++)
		{
			chkName1[i].checked = false;
		}
	}
	
	if (typeof chkName2 != 'undefined')
	{
		for (i=0; i < chkName2.length; i++)
		{
			chkName2[i].checked = false;
		}
	}
	
	if(status)
	{
		chk.checked=true;
	}
	else
	{
		chk.checked=false;
	}
}

function checkCorseTypes(corse)
{
	if (typeof document.all['type[' +  corse + ']'] != 'undefined')
	{
		if (typeof document.all['type[' +  corse + ']'].length != 'undefined')
		{
			for(i=0;i<document.all['type[' +  corse + ']'].length;i++)
			{
				if(document.all['type[' +  corse + ']'][i].checked)
				{
					return true;
				}
			}
		}
		else
		{
			if (document.all['type[' +  corse + ']'].checked)
			{
				return true;
			}
		}
	}
	return false;
}

function PostSearchForm()
{
	var msg="Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n";
	
	var j,k=0;
	
	var chObj = document.getElementById('ch');
	
	var subscriber_form = document.getElementById('subscriber_form');
	
	if (typeof chObj != 'undefined')
	{
		if (typeof chObj.length != 'undefined')
		{
			for (j = 0; j < chObj.length; j++)
			{
				if (chObj[j].checked==true)
				{
					k=1;
					break;
				}
			}
		}
		else if (subscriber_form.ch.checked==true)
		{
			k = 1;
		}
	}
	
	if(k==0)
	{
		alert(msg+"please select at least one subscription.");
		return false;
	}
	
	var i;
	var check = checkCorseTypes(2);
	
	if (!check)
		check = checkCorseTypes(3);
	
	var checkSubs = checkCorseTypes(0);
	
	/*
	if(check)
	{
		if(isEmptyString(subscriber_form.friend_email.value))
		{
			alert(msg+"please enter friends e-mail address");
			subscriber_form.friend_email.value=""; 
			subscriber_form.friend_email.focus();
			return false;
		}
		
		if(!isEmail(subscriber_form.friend_email.value))
		{
			alert(msg+"please enter e-mail address in correct format");
			subscriber_form.friend_email.focus();
			return false;
		}
	}
	if(isEmptyString(subscriber_form.fname.value))
	{
		alert(msg+"please enter your first name");
		subscriber_form.fname.value="";
		subscriber_form.fname.focus();
		return false;
	}
	
	if(isEmptyString(subscriber_form.lname.value))
	{
		alert(msg+"please enter your last name");
		subscriber_form.lname.value="";
		subscriber_form.lname.focus();
		return false;
	}
	*/

	if(isEmptyString(subscriber_form.email.value))
	{
		alert(msg+"Please enter  e-mail address");
		subscriber_form.email.value=""; 
		subscriber_form.email.focus();
		return false;
	}
	
	if(!isEmail(subscriber_form.email.value))
	{
		alert(msg+"Please enter e-mail address in correct format");
		subscriber_form.email.focus();
		return false;
	}
	
	if(isEmptyString(subscriber_form.country.value))
	{
		alert(msg+"Please select your country");
		subscriber_form.country.value="";
		subscriber_form.country.focus();
		return false;
	}
	
	if (checkSubs)
	{
		if(isEmptyString(subscriber_form.username.value))
		{
			alert(msg+"Please enter your username");
			subscriber_form.username.focus();
			return false;
		}
		
		if(isEmptyString(subscriber_form.password.value))
		{
			alert(msg+"Please your enter password");
			subscriber_form.password.value="";
			subscriber_form.password.focus();
			return false;
		}
		
		s1 = subscriber_form.password.value;
		len2 = s1.length;
		
		if(len2 < 1 || len2 > 20 )
		{
			alert(msg+"please enter your password");
			subscriber_form.password.focus();
			return false;
		}
		
		if(isEmptyString(subscriber_form.repassword.value))
		{
			alert(msg+" Please enter re-enter password");
			subscriber_form.repassword.value="";
			subscriber_form.repassword.focus();
			return false;
		}
		if(subscriber_form.repassword.value!=subscriber_form.password.value)
		{
			alert(msg+" Please ensure that both passwords are the same. Please re-enter your passwords");
			subscriber_form.repassword.focus();
			subscriber_form.repassword.value="";
			return false;
		}
		
		if(isEmptyString(subscriber_form.s_question.value))
		{
			alert(msg+ "Please select a security question");
			subscriber_form.s_question.focus();
			return false;
		}
		
		if(isEmptyString(subscriber_form.answer.value))
		{
			alert(msg+"Please enter a answer");
			subscriber_form.answer.focus();
			return false;
		}
	}
	
	if(!subscriber_form.terms.checked)
	{
		alert(msg+"You must agree to the Terms of Service");
		return false;
	}
	return true;
}