
/*cut space*/
function Trim(sString){
	while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '�@')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '||sString.substring(sString.length-1, sString.length) == '�@'){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function Checkimage(hinhanh)
{
	var filename = hinhanh;	
	var dotpos;
	filename = filename.substring(filename.lastIndexOf("\\")+1,filename.length);
	dotpos=filename.lastIndexOf('.');
	ext=filename.substr(dotpos+1,3);
	ext=ext.toLowerCase();
	fname=filename.substr(0,dotpos);
		
	if (ext!=""){
		if ((ext!="gif") && (ext!="jpg") && (ext!="png")){
			return false;
		}
	}
	return true;
}

function CheckPdf(hinhanh)
{
	var filename = hinhanh;	
	var dotpos;
	filename = filename.substring(filename.lastIndexOf("\\")+1,filename.length);
	dotpos=filename.lastIndexOf('.');
	ext=filename.substr(dotpos+1,3);
	ext=ext.toLowerCase();
	fname=filename.substr(0,dotpos);
	//alert("ddd");
	if (ext!=""){
		if ((ext!="pdf")){
			return false;
		}
	}
	return true;
}

/*=======Check EMAIL=========*/
function isEmail(myMail) {
	var reEmail = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	return reEmail.test(myMail);
}

/*==============LOGIN================*/
function CheckLogin(){
	
	var temp = document.getElementById("txtUserName");	
	if(temp.value==""){
		alert(msg001);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	var temp = document.getElementById("txtPassword");	
	if(temp.value==""){
		alert(msg001);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
}

function CheckLoginFront(){
	
	var temp = document.getElementById("username");	
	if(temp.value==""){
		alert(msg001);
		temp.focus();
		return false;	
	}
	var temp = document.getElementById("password");	
	if(temp.value==""){
		alert(msg002);
		temp.focus();
		return false;	
	}
}



/*=============Register=============*/
function CheckRegister(flag){
	
	//Check user name not null
	var temp = document.getElementById("txtUserName");	
	if(Trim(temp.value)==""){
		alert(msg080);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	if(flag==1){
		//Check password not null
		var temp = document.getElementById("txtPassword");	
		if(Trim(temp.value)==""){
			alert(msg081);
			temp.focus();
			temp.className="bg_color_01";
			return false;	
		}temp.className="bg_color_02";
		
		// Check confirm password not null
		var temp2 = document.getElementById("txtPassConfirm");	
		if(Trim(temp2.value)==""){
			alert(msg086);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
		
		//confirm pass have to equal password
		if(Trim(temp.value)!=Trim(temp2.value)){
			alert(msg087);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
	}
	
	//Check email not null
	var temp = document.getElementById("txtEmail");	
	if(Trim(temp.value)==""){
		alert(msg082);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check valid email
	if(!isEmail(temp.value)){
		alert(msg083);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
		
	//Check picture
	temp=document.getElementById("txtPicture");
	if(temp.value!=""){
		if(!Checkimage(temp.value)){
			alert(msg085);		
			temp.focus();
			return false;
		}
	}
	
	//Check picture
	temp=document.getElementById("txtEssayPdf");
	if(temp.value!=""){
		if(!CheckPdf(temp.value)){
			alert(msg091);		
			temp.focus();
			return false;
		}
	}
	
	if(flag==1){
	
		//Check code have to equal 6 character
		var temp = document.getElementById("code");	
		if(Trim(temp.value).length!=6){
			alert(msg084);
			temp.focus();
			temp.className="bg_color_01";
			return false;	
		}temp.className="bg_color_02";
	}
	
	if(flag==0){
		var temp = confirm(msg004);
		if(!temp)
			return false;
	}
		
}



//function check ko cho user nhap vao textfield upload
function UploadPopup(aaa){
		aaa.click();
}


/*=============Register=============*/
function CheckContact(){
	
	//Check user name not null
	var temp = document.getElementById("txtName");	
	if(Trim(temp.value)==""){
		alert(msg088);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check email not null
	var temp = document.getElementById("txtEmail");	
	if(Trim(temp.value)==""){
		alert(msg082);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check valid email
	if(!isEmail(temp.value)){
		alert(msg083);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	
	//Check subject not null
	var temp = document.getElementById("txtSubject");	
	if(Trim(temp.value)==""){
		alert(msg089);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check subject not null
	var temp = document.getElementById("txtMessage");	
	if(Trim(temp.value)==""){
		alert(msg090);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check code have to equal 6 character
	var temp = document.getElementById("code");	
	if(Trim(temp.value).length!=6){
		alert(msg084);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
}



/*=============add page=============*/
function CheckPage(flag){
	
	//Check pageID not null
	var temp = document.getElementById("txtID");	
	if(Trim(temp.value)==""){
		alert(msg005);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check pageID have to be larger 6 characters
	var temp = document.getElementById("txtID");	
	if(Trim(temp.value).length !=6){
		alert(msg006);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check page name not null
	var temp = document.getElementById("txtPage");	
	if(Trim(temp.value)==""){
		alert(msg007);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check Page Title not null
	var temp = document.getElementById("txtTitle");	
	if(Trim(temp.value)==""){
		alert(msg008);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	if(flag==0){
		var temp = confirm(msg004);
		if(!temp)
			return false;
	}

}





/*=============change pass=============*/

function ChangePass(){	
	//Check pass not null
	var temp = document.getElementById("txtPass");	
	if(Trim(temp.value)==""){
		alert(msg010);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check confirm pass not null
	var temp2 = document.getElementById("txtConfirmPass");	
	if(Trim(temp2.value)==""){
		alert(msg086);
		temp2.focus();
		temp2.className="bg_color_01";
		return false;	
	}temp2.className="bg_color_02";
	
	//confirm pass have to equal password
	if(Trim(temp.value)!=Trim(temp2.value)){
		alert(msg087);
		temp2.focus();
		temp2.className="bg_color_01";
		return false;	
	}temp2.className="bg_color_02";
	
	
	
}



/*=============Add student=============*/
function CheckStudent(flag){
	
	//Check user name not null
	var temp = document.getElementById("txtUserName");	
	if(Trim(temp.value)==""){
		alert(msg080);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//neu la add thi them doan nay
	if(flag==1){
		//Check password not null
		var temp = document.getElementById("txtPassword");	
		if(Trim(temp.value)==""){
			alert(msg081);
			temp.focus();
			temp.className="bg_color_01";
			return false;	
		}temp.className="bg_color_02";
		
		// Check confirm password not null
		var temp2 = document.getElementById("txtPassConfirm");	
		if(Trim(temp2.value)==""){
			alert(msg086);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
		
		//confirm pass have to equal password
		if(Trim(temp.value)!=Trim(temp2.value)){
			alert(msg087);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
	
	}
	//---------
	
	//Check email not null
	var temp = document.getElementById("txtEmail");	
	if(Trim(temp.value)==""){
		alert(msg082);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check valid email
	if(!isEmail(temp.value)){
		alert(msg083);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
		
	//Check picture
	temp=document.getElementById("txtPicture");
	if(temp.value!=""){
		if(!Checkimage(temp.value)){
			alert(msg085);		
			temp.focus();
			return false;
		}
	}
	//Check picture
	temp=document.getElementById("txtEssayPdf");
	if(temp.value!=""){
		if(!CheckPdf(temp.value)){
			alert(msg091);		
			temp.focus();
			return false;
		}
	}
	//neu la add thi them doan nay
	if(flag==1){
	
		//Check code have to equal 6 character
		var temp = document.getElementById("code");	
		if(Trim(temp.value).length!=6){
			alert(msg084);
			temp.focus();
			temp.className="bg_color_01";
			return false;	
		}temp.className="bg_color_02";
	}
	
	if(flag==0){
		var temp = confirm(msg004);
		if(!temp)
			return false;
	}
		
}

//=================CHeck forgot pass===========================
function CheckForgot(){
	//Check email not null
	var temp = document.getElementById("txtEmail");	
	if(Trim(temp.value)==""){
		alert(msg082);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
	
	//Check valid email
	if(!isEmail(temp.value)){
		alert(msg083);
		temp.focus();
		temp.className="bg_color_01";
		return false;	
	}temp.className="bg_color_02";
}


//=================CHeck Search===========================
function CheckSearch(){
	
	//Check keyword not null
	var temp = document.getElementById("txtKeyword");	
	if(Trim(temp.value)==""){
		alert(msg011);
		temp.focus();
		return false;	
	}
	
	//Check keyword at least 3 char

	if(Trim(temp.value).length <3){
		alert(msg012);
		temp.focus();
		return false;	
	}
}


//////////////////////////////////////////////////////////////////////////////////
//Check user management: INSERT (flag==1); UPDATE (flag!=1)
///////////////////////////////////////////////////////////////////////////////////
function CheckUserManagement(flag){
	
	
	//Truong hop update
	if(flag==0){
		//check username
		var temp = document.getElementById("txtUserName");
		if(Trim(temp.value)== ""){
			alert(msg015);
			temp.focus();
			return false;	
		}
		
		//Check valid email
		var temp = document.getElementById("txtEmail");	
		if(Trim(temp.value)!=""){
			if(!isEmail(temp.value)){
				alert(msg083);
				temp.focus();
				temp.className="bg_color_01";
				return false;	
			}temp.className="bg_color_02";
		}
		
		if(flag==0){
			var temp = confirm(msg004);
			if(!temp)
				return false;
		}
		
		
		
	// Truong hop insert	
	}else{
		//check id
		var temp = document.getElementById("txtID");	
		if(Trim(temp.value)==""){
			alert(msg013);
			temp.className="bg_color_01";
			temp.focus();
			return false;	
		}temp.className="bg_color_02";
		
		//check id equal 7 chars
		if(Trim(temp.value).length!=7){
			alert(msg014);
			temp.className="bg_color_01";
			temp.focus();
			return false;	
		}temp.className="bg_color_02";
	
	
	
		//check username
		var temp = document.getElementById("txtUserName");
		if(Trim(temp.value)== ""){
			alert(msg015);
			temp.className="bg_color_01";
			temp.focus();
			return false;	
		}temp.className="bg_color_02";
		
	
	    var temp = document.getElementById("txtPassword");	
		if(Trim(temp.value)==""){
			alert(msg081);
			temp.focus();
			temp.className="bg_color_01";
			return false;	
		}temp.className="bg_color_02";
		
		// Check confirm password not null
		var temp2 = document.getElementById("txtConfirm");	
		if(Trim(temp2.value)==""){
			alert(msg086);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
		
		//confirm pass have to equal password
		if(Trim(temp.value)!=Trim(temp2.value)){
			alert(msg087);
			temp2.focus();
			temp2.className="bg_color_01";
			return false;	
		}temp2.className="bg_color_02";
		
	
		//Check valid email
		var temp = document.getElementById("txtEmail");	
		if(Trim(temp.value)!=""){
			if(!isEmail(temp.value)){
				alert(msg083);
				temp.focus();
				temp.className="bg_color_01";
				return false;	
			}temp.className="bg_color_02";
		}
	}
}




