function CheckSyntax(control)
{
	re1 ='<[0-9a-zA-Z!]';
	re2 ='[0-9a-zA-Z]<';
	if(isNotEmpty(control))
	{
		if( (control.value.search(re1)>-1) || (control.value.search(re2)>-1) )
			return false;
		else
			return true;
	}
	else
	return true;
}

function file_type(control)
{
	len=control.value.length;
	if(len == 0)
		return true;

	var f_type = control.value.substring(len-4,len).toLowerCase();
	if(f_type == '.jpg' || f_type == '.png' || f_type == '.gif')
		return true;
	return false;

}

function CheckSyntaxinnerHTML(control)
{
	re1 ='<[0-9a-zA-Z!]';
	re2 ='[0-9a-zA-Z]<';
	
	if(isNotEmptyinnerHTML(control))
	{
		if( (control.innerHTML.search(re1)>-1) || (control.innerHTML.search(re2)>-1) )
			{
				return false;
			}
		else
			{
				return true;
			}
	}
	else
	return true;
}

function leftTrim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	return sString;
}


function rightTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function trimAll(sString) 
{
	if(sString)
	{
		while (sString.substring(0,1) == ' ')
		{
		sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
		sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}

function isCell(control)
	{
		if(isNotEmpty(control))
		{
			if(control.value.search("[^0-9]")>-1)
			{
				return false;
			}
			return true;
		 }
	  	return true;
	}

function isNumeric(control)
	{
		if(isNotEmpty(control))
		{  
		if(control.value.search("[^0-9]")>-1)
			{
				return false;
			}
			return true;
		}
		return false;
	}
	
function isAlphaNumeric(control)
	{
		if(isNotEmpty(control))
		{  
		if(control.value.search("[^a-zA-Z0-9.-][\s]")>-1)
			{
				return false;
			}
		return true;
		}
		return false;
	}
	
function isAlpha(control)
	{
		if(isNotEmpty(control))
		{  
		if(control.value.search("[\s][^a-zA-Z.-][\s]")>-1)
			{
				return false;
			}
		return true;
		}
		return false;
	}
	
function isNotEmpty(control)
	{
	
		var value = control.value;
		if(trimAll(value).length>0)
		{	
			return true;
		}
		return false;
	}
	
function isNotEmptyinnerHTML(control)
	{

		var value = control.innerHTML;
		if(trimAll(value).length>0)
		{	
			return true;
		}
		return false;
	}
	
function ChkinnerHTML_image(control)
	{
		var value = trimAll(control.innerHTML);
		if((value == '<b>Picture uploaded</b>') || (value == '<B>Picture uploaded</B>')  )
		{	
			return false;
		}
		return true;
	}
	
function ChkinnerHTML_audio(control)
	{
		var value = trimAll(control.innerHTML);
		if( (value == '<b>Song uploaded</b>') || (value == '<B>Song uploaded</B>'))
		{	
			return false;
		}
		return true;
	}

function isDate(b_dd,b_mm,b_yyyy)
	{
		var isLeapYear = false;
		//var year = b_yyyy.options[b_yyyy.selectedIndex].value;
		var month = b_mm.options[b_mm.selectedIndex].value;
		var date = b_dd.options[b_dd.selectedIndex].value;
		
		if(date == 31)
		{
			if((month==2) || (month==4) || (month==6) || (month==9) || (month==11))
			{
				return false;
			}
		}
		if(month==2 && date>29) return false;
	
		return true;
	
	}
	
	
function isEmail(control)
	{
		
		//var regexp = "[0-9a-z]([-\_\.0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]";
		//var reg2=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;  
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
//old		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		var reg2=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;  

		var add = trimAll(control.value);
		
		if(!reg1.test(add ) && reg2.test(add ))	
		{
			return true;
		}
		return false;
	}

function isEduEmail(control)
	{
		var end = /.edu$/;
		var mail =trimAll(control.value);
		if(isEmail(control) && end.test(mail))
		{
			return true;
		} 
		return false;
	}
	
function isCorrectLength(control,atleast,atmost)
	{
		if(isAtleastLength(control,atleast) && isAtmostLength(control,atmost))
		{
			return true;
		}
		return false;
	}
	
function isAtleastLength(control,atleast)
	{
		if(isNotEmpty(control))
		{
			if(control.value.length<atleast)
			{
				return false;
			}
			return true;
		}
		return false;
		
	}

function isAtmostLength(control,atmost)
	{
		if(control.value.length>atmost)
			{
				return false;
			}
			return true;


	}

function isSame(control1,control2)
	{
	
	
		if(isNotEmpty(control1))
		{
			if(control1.value==control2.value)	
			{
				return true;
			}
		}
		return false;
	}

	
function isRegularExpression(control,expression)
	{
		return expression.test(control.value);		
	}



	function validate_form(varStr){

		if (varStr=='reg'){
			return validateReg();
		}
		else if (varStr=='login'){
			return validateLogin();
		}
		else if (varStr=='order'){
			return validateorder();
		}
		else if (varStr=='invoiceorder'){
			return validateinvoiceorder();
		}
		else if (varStr=='forgotpwd'){
			return validatepwd();
		}
		else if (varStr=='question'){
			return validatequestion();
		}
		else{
			return true;
		}
	}
	
	function validateReg(){
		var email=document.getElementById("email");
		var password=document.getElementById("password");
		var password2=document.getElementById("password2");
		var name=document.getElementById("name");
		var phone=document.getElementById("phone");
		var mobile=document.getElementById("mobile");
		var city=document.getElementById("city");
		
		if(!isNotEmpty(email))
			{
				alert('请输入注册邮箱.');
				return false;
			}
		if(!isEmail(email))
		{
			alert('不合适的注册邮箱地址.');
			return false;
		}
		if(!isNotEmpty(password))
		{
			alert('请输入密码.');
			return false;
		}

		if(!isNotEmpty(password2))
		{
			alert('请输入重复密码.');
			return false;
		}
		if(!CheckSyntax(password))
		{
			alert('密码包含无效字符.');
			return false;
		}
		if(!CheckSyntax(password2))
		{
			alert('重复密码包含无效字符.');
			return false;
		}
		if(!isAtleastLength(password,4))
		{
			alert('密码至少需要4位字符.');
			return false;
		}
		if(!isAtleastLength(password2,4))
		{
			alert('重复密码至少需要4位字符.');
			return false;
		}
		if(!isSame(password,password2))
		{
		alert('两次密码不一致.');
		return false;
		}
		if(!isNotEmpty(name))
		{
			alert('请输入您的姓名称呼.');
			return false;
		}
		if(!CheckSyntax(name))
		{
			alert('姓名称呼包含无效字符.');
			return false;
		}
		if(!isNotEmpty(phone))
		{
			alert('请输入电话.');
			return false;
		}

		if(!isNotEmpty(mobile))
		{
			alert('请输入移动电话.');
			return false;
		}
	
	
		return true;
		
	}

	function validateinvoiceorder(){
		var name=document.getElementById("cname");
		var email=document.getElementById("cemail");
		var fname=document.getElementById("cfullname");
		var phone=document.getElementById("cphone");
		var mobile=document.getElementById("cmobile");
		if(!isNotEmpty(name))
			{
				alert('请输入联系人.');
				return false;
			}
		if(!isNotEmpty(fname))
			{
				alert('请输入公司名称.');
				return false;
			}
			
		if(!isNotEmpty(email))
			{
				alert('请输入邮箱.');
				return false;
			}
		if(!isEmail(email))
		{
			alert('不合适的邮箱地址.');
			return false;
		}
		if(!isNotEmpty(phone))
		{
			alert('请输入联系电话.');
			return false;
		}
		if(!isNotEmpty(mobile))
		{
			alert('请输入手机.');
			return false;
		}
		return true;
	}
	
	function validateorder(){
		var name=document.getElementById("cname");
		var email=document.getElementById("cemail");
		var fname=document.getElementById("cfullname");
		var phone=document.getElementById("cphone");
		var mobile=document.getElementById("cmobile");
		var checkcode=document.getElementById("checkcode");
		if(!isNotEmpty(name))
			{
				alert('请输入联系人.');
				return false;
			}
		if(!isNotEmpty(fname))
			{
				alert('请输入公司名称.');
				return false;
			}
			
		if(!isNotEmpty(email))
			{
				alert('请输入邮箱.');
				return false;
			}
		if(!isEmail(email))
		{
			alert('不合适的邮箱地址.');
			return false;
		}
		if(!isNotEmpty(phone))
		{
			alert('请输入联系电话.');
			return false;
		}
		if(!isNotEmpty(mobile))
		{
			alert('请输入手机.');
			return false;
		}
		if(!isNotEmpty(checkcode))
			{
				alert('请输入验证码.');
				return false;
			}

		if(!isAtleastLength(checkcode,4))
		{
			alert('验证码至少需要4位字符.');
			return false;
		}
		return true;
	}
	function validatepwd(){
		var email=document.getElementById("loginemail");
		
		if(!isNotEmpty(email))
			{
				alert('请输入注册邮箱.');
				return false;
			}
		if(!isEmail(email))
		{
			alert('不合适的注册邮箱地址.');
			return false;
		}

		return true;
	}	
	
	function validateLogin(){

		var email=document.getElementById("loginemail");
		var password=document.getElementById("loginpwd");
		if(!isNotEmpty(email))
			{
				alert('请输入注册邮箱.');
				return false;
			}
		if(!isEmail(email))
		{
			alert('不合适的注册邮箱地址.');
			return false;
		}
		if(!isNotEmpty(password))
		{
			alert('请输入密码.');
			return false;
		}

		if(!CheckSyntax(password))
		{
			alert('密码包含无效字符.');
			return false;
		}
		if(!isAtleastLength(password,4))
		{
			alert('密码至少需要4位字符.');
			return false;
		}
		return true;
	}
	
	
	function validatequestion(){
		var context=document.getElementById("context");

		if(!isNotEmpty(context))
			{
				alert('请输入您的问题.');
				return false;
			}

		if(!isAtleastLength(context,4))
		{
			alert('您的问题至少需要4位字符.');
			return false;
		}
		return true;
	}