//General function used for form validation and ajax.
function validateField(sValue)
{
	if (sValue == '' || sValue == null)
	{
		return false;
	}
	else
	{
		return true;
	}
}


function checkemail(vemail)
{
	/*
	var testresults;
	var str=vemail;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
	{	
		testresults=true;
	}	
	else
	{
		testresults=false;
	}
	*/
	testresults = checkMailId(vemail);
	return (testresults);
}

function checkMailId(mailids)
{
var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro'
,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
'.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
'.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
'.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
'.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
'.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
'.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
'.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
'.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
'.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
'.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
'.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
'.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
'.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
'.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
'.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
'.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
'.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
'.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
'.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw');

	var mai = mailids;
	var val = true;
	var beforeat="";
	var afterat="";
	var afterat2="";

	var dot=mai.lastIndexOf(".");
	var con=mai.substring(dot, mai.length);
	con=con.toLowerCase();
	con=con.toString();

	for(var i=0;i<(arr.length);i++)
	{
		if(con == arr[i])
		{
			val=true;
			break;
		}
		else
		{
			val=false;
		}
	}

	var att=mai.lastIndexOf("@");
	beforeat=mai.substring(0,att);
	beforeat=beforeat.toLowerCase();
	beforeat=beforeat.toString();
	var asci1=beforeat.charCodeAt(0);

	afterat=mai.substring(att+1, dot);
	afterat=afterat.toLowerCase();
	afterat=afterat.toString();

	afterat2=mai.substring(att+1, mai.length);
	afterat2=afterat2.toLowerCase();
	afterat2=afterat2.toString();

	if(beforeat=="" || afterat=="" || beforeat.length>30)
		val=false;

	if(afterat2.length>64 || afterat.length<2)
		val=false;

	if((afterat.charCodeAt(0))==45 || (afterat.charCodeAt(afterat.length-1))==45)
		val=false;

	if(val==true)
	{
		if(asci1 > 47 && asci1 < 58)
			val=false;
			
		if(asci1 < 48 || asci1 > 57)
		{
			for(var i=0; i<=beforeat.length-1; i++)
			{
				var asci2=beforeat.charCodeAt(i);
				if((asci2<=44 || asci2==47) || (asci2>=58 && asci2<=94) || (asci2==96) || (asci2>=123 && asci2<=127))
				{
					val=false;
					break;
				}
			}
			
			for(var j=0; j<=afterat.length-1; j++)
			{
				var asci3=afterat.charCodeAt(j);
				if((asci3<=44) || (asci3==46) || (asci3==47) || (asci3>=58 && asci3<=96) || (asci3>=123 && asci3<=127))
				{
					val=false;
					break;
				}
			}	
		}
	}

	if(val==false)
	{
		//alert("Your maild "+mai+" is not valid");
		return false;
	}
	else
	{
		return true;
	}
}



function checkforLettersAndNumbers(inputStr)
{
	var nonLettersRegExp = /[^A-Za-z]/;
	//alert(nonLettersRegExp.test(inputStr));
	return nonLettersRegExp.test(inputStr);
}

function checkForSpecialCharacters(inputStr,forWebstore)
{
	var x;
	var SpecChars = new Array();
	SpecChars[0] = "<";
	SpecChars[1] = ">";
	SpecChars[2] = "http://";
	
	if (forWebstore != undefined)
	{
		is_present1 = checkforLettersAndNumbers(inputStr);
		if(is_present1 == true)
		{
			return false;
		}
	}
	for(x in SpecChars)
	{
		is_present = inputStr.indexOf(SpecChars[x]);
		if(is_present != '-1')
		{
			return false;
			break;
		}
	}
}

function showError(fieldID,errMsg)
{
	message = document.getElementById(fieldID + "errmsg");
	message.className = "error";
	strText = message.innerHTML;
	if (errMsg!="")
	{
		message.innerHTML = "<b><font face='Helvetica,arial,Tahoma,verdana' color='#D85643' size='2'>"+ errMsg + "</font></b>";
	}
	message = document.getElementById(fieldID + "msg");
	message.className = "hidden";
}		

function showMsg(fieldID,msg)
{
	errmessage = document.getElementById(fieldID + "errmsg");
	if(errmessage.className == "hidden")
	{
		message = document.getElementById(fieldID + "msg");
		if(msg != '')
		{
			message.innerHTML = "<b><font face='Helvetica,arial,Tahoma' size='2'>"+ msg + "</font></b>";
		}
		message.className = "visible";
	}
}

function removeMsg(fieldID)
{
	errmessage = document.getElementById(fieldID + "errmsg");
	if(errmessage.className == "hidden")
	{
		message = document.getElementById(fieldID + "msg");
		message.className = "hidden";
	}
}

function removeError(fieldID)
{
	message = document.getElementById(fieldID + "errmsg");
	message.className = "hidden";
	message = document.getElementById(fieldID + "msg");
	message.className = "hidden";
}

function send_request(url) 
{
	http_request = false;
	if(window.XMLHttpRequest) 
	{ 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) 
	{
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}
	
	if (!http_request) 
	{ 
		window.alert("can't create XMLHttpRequest object.");
		return false;
	}
	http_request.onreadystatechange = processRequest;

	http_request.open("GET", url, true);
	http_request.send(null);
}

//Form Validation Functions
function validate_required(field,alerttxt)
{
 	with (field)
 	{ 
  		if (value==null || value=="")
  		{
	 		alert(alerttxt);
			field.focus();
	 		return false;
		}
  		else 
  		{
	 		return true;
		}
 	}
}

function validate_email(field,alerttxt)
{
 	with (field)
 	{
  		apos=value.indexOf("@")
  		dotpos=value.lastIndexOf(".")
  		if (apos<1||dotpos-apos<2) 
  		{
	 		alert(alerttxt);
			field.focus();
	 		return false;
		}
  		else 
		{
	 		return true;
		}
 	}
}

function check_for_spec_chars(field,alerttxt)
{
	with(field)
	{
		var x;
		var SpecChars = new Array();
		SpecChars[0] = "<";
		SpecChars[1] = ">";
		
		for(x in SpecChars)
		{
			is_present = value.indexOf(SpecChars[x]);
			if(is_present != '-1')
			{
				alert(alerttxt);
				field.focus();
				return false;
			}
		}
	}
}

function showDiv(fieldID)
{
	vControl = document.getElementById(fieldID + "msg");
	vControl.className = "visible";
}