function checkBlankField(txt)
{
	var mint_txt = txt.length;
	var mstr_txt = txt;
	var mint_count = 0;
	for (var iloop = 0; iloop<mint_txt; iloop++)
	{
		if (mstr_txt.charAt(iloop) == " ")
		{
		   mint_count = mint_count+1;
		}
	}    
// if nothing entered in the field
	if (txt == "")
	{
		return false;
	}
	else if (mint_count == mint_txt)
	{
		return false;
	}
	return true;
}
				
function valid_email(eml)
{
	//declare the required variables
	var mint_len;
	var mstr_eml=eml;
	var mint_at=0;
	var mint_atnum=0;
	var mint_dot=0;
	var mint_dotnum=0;

	mint_len = eml.length; //takes the length of the email address entered
	//checking for the symbol single quote. If found replace it with its html code
	if (mstr_eml.indexOf("'")!=-1)
	{	
		mstr_eml=mstr_eml.replace("'","'");
	}
	//checking for the (@) & (.) symbol
	for(var iloop=0;iloop<mint_len;iloop++)
	{
		if(mstr_eml.charAt(iloop)=="@")
		{
			mint_at=iloop+1;
			mint_atnum=mint_atnum+1;
		}
		if(mstr_eml.charAt(iloop)==".")
		{
			mint_dot=iloop+1;
			mint_dotnum=mint_dotnum+1;
		}
	}
	//if nothing entered in the field
	if (mstr_eml=="")
	{
		return true;
	}
	//if @ entered more than once & dot (.) entered more than 4 times
	else if((mint_atnum!=1)||(mint_dotnum>4)||((mint_dot-mint_at)<2)||((mint_len-mint_dot)<2)||(mint_at<3))
	{
		return true;
	}
	//if any blank space is entered in the email address
	else if (mstr_eml.indexOf(" ")!=-1)
	{
		return true;
	}
	return false;
}


function refresh_img()
{
	document.images.rand_img.src = "randomImage.php";
}
function IsNumeric(strString)
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
		for (i = 0; i < strString.length && blnResult == true; i++)
		{
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1)
			{
			blnResult = false;
			}
		}
   		return blnResult;
 }

var alpha_numeric_with_no_space = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var alpha_numeric_with_space = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
var alpha_with_space = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var alpha_with_no_space = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var num = "0123456789";
var string_without_space = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|\=-`~{}[]:?<>/.0123456789";
var address_string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#()-{}[]/\.0123456789, ";

function checking_characters(t,v)
{
	var w = "";
	var flag = 0;
	for (i=0; i < t.length; i++)
	{
		x = t.charAt(i);
		//indexOf has a useful return value of -1 if the character or string you searched for is not contained within the string. 
		if (v.indexOf(x,0) != -1)
		{
			w += x;
		}
		else
		{ 	
			w += x;
			flag = 1;
			//break;
		}
	}
	t.value = w;
	return flag;
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


/*
Input: string for validation, mimimum length, maximum length
Output:
0: String is Correct
1: Length Short
2: Length Exceed
3: Only Alphanumeric without Space are allowed
*/
function is_Alpha_Numeric_With_Out_Space(str, minimum, maximum )
{
	var f=0;
	
	if(str.length<minimum )
	{
		return 1;
	}
	else if(str.length>maximum)
	{
		return 2;	
	}
	else
	{
		f = checking_characters(str,alpha_numeric_with_no_space);
		if(f == 1)
		{
			return 3;	
		}
		else
		{
			return 0;
		}
	}
	
}


function validate_signup()
{
	if(checkBlankField(document.frm_register.username.value) == false)
	{
		//alert("Please enter desired user name.");
		document.getElementById('msg_username').innerHTML = '<font color="#FF0000">Please enter desired user name.</font>';
		document.frm_register.username.focus();
		return false
	}
	else
	{
		//alert('TEST');
		var usernamevalidate = is_Alpha_Numeric_With_Out_Space(document.frm_register.username.value, 1, 50);
		//alert(usernamevalidate);
		if(usernamevalidate != 0)
		{
			//alert("Please enter desired user name.");
			document.getElementById('msg_username').innerHTML = '<font color="#FF0000">Please use Only Letters and Numbers. No spaces allowed.</font>';
			document.frm_register.username.value = trim(document.frm_register.username.value);
			document.frm_register.username.focus();
			return false
		}
		else
		{
			document.getElementById('msg_username').innerHTML = 'You will use this to log in';
		}
	}

	if(checkBlankField(document.frm_register.email.value) == false)
	{
		//alert("Please enter email address.");
		document.getElementById('msg_email').innerHTML = '<font color="#FF0000">Please enter email address.</font>';
		document.frm_register.email.focus();
		return false
	}
	else if(valid_email(document.frm_register.email.value) == true)
	{
		//alert("Please enter a valid email address.");
		document.getElementById('msg_email').innerHTML = '<font color="#FF0000">Please enter a valid email address.</font>';
		document.frm_register.email.select();
		return false;
	}
	else
	{
		document.getElementById('msg_email').innerHTML = 'Email addresses are never sold or shared';
	}

	if(checkBlankField(document.frm_register.passwd.value) == false)
	{
		//alert("Please enter password.");
		document.getElementById('msg_passwd').innerHTML = '<font color="#FF0000">Please enter password.</font>';
		document.frm_register.passwd.focus();
		return false
	}
	else if(checkBlankField(document.frm_register.passwd.value) == true)
	{
		if(document.frm_register.passwd.value.length<6)
		{
			//alert("Password must be at least 6 characters in length.");
			document.getElementById('msg_passwd').innerHTML = '<font color="#FF0000">Password must be at least 6 characters in length.</font>';
			document.frm_register.passwd.focus();
			return false
		}
		else
		{
			document.getElementById('msg_passwd').innerHTML = 'Password must be at least 6 characters in length.';
		}
	}

	if(checkBlankField(document.frm_register.cpasswd.value) == false)
	{
		//alert("Please confirm the password.");
		document.getElementById('msg_cpasswd').innerHTML = '<font color="#FF0000">Please confirm the password.</font>';
		document.frm_register.cpasswd.focus();
		return false
	}
	else
	{
		document.getElementById('msg_cpasswd').innerHTML = 'Enter your password again for confirmation';
	}

	if(document.frm_register.passwd.value!=document.frm_register.cpasswd.value)
	{
		//alert("Password and confirm password must be same.");
		document.getElementById('msg_cpasswd').innerHTML = '<font color="#FF0000">Password and confirm password must be same.</font>';
		document.frm_register.cpasswd.select();
		return false
	}
	else
	{
		document.getElementById('msg_cpasswd').innerHTML = 'Enter your password again for confirmation';
	}

	if(checkBlankField(document.frm_register.blog_url.value) == false)
	{
		//alert("Please enter blog address.");
		//document.getElementById('msg_blog_url').innerHTML = '<font color="#FF0000">Please enter blog address.</font>';
		//document.frm_register.blog_url.focus();
		//return false
	}
	else
	{
		var blog_urlvalidate = is_Alpha_Numeric_With_Out_Space(document.frm_register.blog_url.value, 1, 50);
		//alert(usernamevalidate);
		if(blog_urlvalidate != 0)
		{
			//alert("Please enter desired user name.");
			document.getElementById('msg_blog_url').innerHTML = '<font color="#FF0000">Please use Only Letters and Numbers. No spaces allowed.</font>';
			document.frm_register.blog_url.value = trim(document.frm_register.blog_url.value);
			document.frm_register.blog_url.focus();
			return false
		}
		else
		{
			document.getElementById('msg_blog_url').innerHTML = 'Your blog address will be eduTecher.net/this';
		}
	}

	if(document.getElementById("agree").checked == false){	
	alert("Please agree to the terms of service.");
	document.frm_register.agree.select();
	return false;	
	}

}

function check_duplicate(val,dbfield, tagname)
{
	
	
	if(val != '')
	{
		var http1;
		url='check_duplicate.php?val='+val+'&field='+dbfield;
		//alert(url);
		if(window.XMLHttpRequest){
			http1=new XMLHttpRequest();
		}else if(window.ActiveXObject){
			http1=new ActiveXObject('msxml2.XMLHTTP');
		//	new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(http1){
			http1.open('get',url,true);
			http1.send(null);
			http1.onreadystatechange=function(){
				if(http1.readyState==4){
					if(http1.status==200){
						//alert(http1.responseText);
						if(http1.responseText > 0)
						{
							document.getElementById('msg_'+dbfield).innerHTML = '<font color="#FF0000">'+tagname+' '+val+' already exists.</font>';
							//document.frm_register.username.focus();
							document.getElementById(dbfield).value = '';
							document.getElementById(dbfield).focus();
						}
						else
						{
							document.getElementById(dbfield).value = val;
							document.getElementById('msg_'+dbfield).innerHTML = '<font color="#4E9258">available</font>';
						}
						
					}
				}
			}
		}
	}
 }

