﻿// JScript File

Comment = function(publishID,handler,imageUrl,nothingUrl,imageID)
{
    this.Initialize.apply(this,arguments);
}
Comment.prototype = 
{
    Initialize : function(publishID,handler,imageUrl,imageID)
    {
        this.CommenterName = document.getElementById("txtCommenterName") ;
        this.Email = document.getElementById("txtCommenterEmail");
        this.Adress = document.getElementById("txtTinh_ThanhPho");
        this.Content = document.getElementById("txtContent");
        this.ImageText = document.getElementById("txtGenerator");
        this.image = document.getElementById(imageID);
        this.message = document.getElementById("lblMessage") ;
        this.HandlerUrl = handler;
        this.imageUrl = imageUrl;
        this.ArticleID = publishID;

    }
    
    ,ValidateEmail : function()
    {
        if(this!=null)
    	{
	        if ((this.Email.value==null)||(this.Email.value=="")){
		        this.message.innerHTML="Hãy nhập địa chỉ Email của bạn."
		        this.Email.focus()
		        return false
	        }
	        if (this.echeck(this.Email.value)==false){
		        this.Email.value=""
		        this.Email.focus()
		        return false
	        }
	    }
	    this.message.innerHTML="" ;
	    return true
	    
    },
    
    echeck : function(str) 
    {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   this.message.innerHTML="E-mail không đúng định dạng";
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   this.message.innerHTML="E-mail không đúng định dạng";
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   this.message.innerHTML="E-mail không đúng định dạng";
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    this.message.innerHTML="E-mail không đúng định dạng";
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   this.message.innerHTML="E-mail không đúng định dạng";
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    this.message.innerHTML="E-mail không đúng định dạng";
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   this.message.innerHTML="E-mail không đúng định dạng";
		    return false
		 }

 		 return true					
	},
	
	ChekName : function()
	{
	   if ((this.CommenterName.value==null)||(this.CommenterName.value==""))
	   {
	        this.message.innerHTML="Hãy nhập họ tên của bạn."
	        this.CommenterName.focus();
	        return false
	    }
	    this.message.innerHTML="";
	    return true; 
	},
	CheckContent : function()
	{
	   if ((this.Content.value==null)||(this.Content.value==""))
	   {
	        this.message.innerHTML="Hãy nhập ý kiến đánh giá của bạn."
	        this.Content.focus();
	        return false
	    }
	    this.message.innerHTML="";
	    return true; 
	},
	
	CheckImageText : function()
	{
	   if ((this.ImageText.value==null)||(this.ImageText.value==""))
	   {
	        this.message.innerHTML="Hãy nhập các ký tự xác nhận."
	        this.ImageText.focus();
	        return false
	    }
	    this.message.innerHTML="";
	    return true; 
	},
	
	Save : function( )
	{
	    var options = 
		{
			parameters:{action:"get",PublishID:this.ArticleID,Name:this.CommenterName.value ,Email:this.Email.value ,Content:this.Content.value,imageText:this.ImageText.value,adress:this.Adress.value  },
			onSuccess : this.SaveOnSuccess,
			onFailure :this.SaveFailure
		};
		new MX.XHR(options).send(this.HandlerUrl); 
	},
	
	SaveOnSuccess : function(r)
    {
        comment.SetMessageText(r.text);
        comment.SetImageUrl();  

    },
    SaveFailure : function()
    {
         comment.SetMessageText("Kết nối không thành công, xin hãy thử lại."); 
         comment.SetImageUrl();
    },
    OnSubmit : function()
    {
        if(!this.ChekName()) return;
        if(!this.ValidateEmail()) return;
        if(!this.CheckContent()) return;
        if(!this.CheckImageText()) return;
        this.Save();        
    }
    ,SetMessageText : function( text)
    {   
        switch(text)
        {
            case 0:
                this.message.innerHTML = "Ký tự kiểm tra không chính xác, xin hãy nhập lại.";
                this.ImageText.value="";
                this.ImageText.focus();
                break;
            case "0":
                this.message.innerHTML = "Ký tự kiểm tra không chính xác, xin hãy nhập lại.";
                this.ImageText.value="";
                this.ImageText.focus();
                break;
            case 1:
                this.message.innerHTML = "Cám ơn bạn đã gửi phản hồi cho chúng tôi.";
                this.Content.value="";
                this.Content.focus()
                this.ImageText.value="";
                this.ImageText.focus();
                break;
                case "1":
                this.message.innerHTML = "Cám ơn bạn đã gửi phản hồi cho chúng tôi.";
                this.Content.value="";
                this.Content.focus()
                this.ImageText.value="";
                this.ImageText.focus();
                break;
            default:
                this.message.innerHTML = text;
                break;
        }
    }
    ,
    SetImageUrl:function()
    {
         window.setTimeout(MX.Delegate(this,this.RenderImageText),100);
    }
    ,RenderImageText:function()
    {
         this.image.src=this.imageUrl +"?key=rnd_"+Math.random();
    }
    
}

