﻿
function GetString(s,_default)
{
    if(s==null|| s=="")
    {
        return _default;
    }
    return s;
}

var Javascript={
     formCheck: function(theform)
        {
        /// <summary>Kiểm tra form trước khi submit</summary>
        /// <param name="theform">id form, default id='this'</param>
           var alertMsg = "Bạn phải nhập thông tin bắt buộc quy định bởi dấu (*).";
            if (document.all||document.getElementById)
            {
                for (i=0;i<theform.length;i++)
                {
                    var __obj=theform.elements[i];
                    if(__obj)
                    {
                        switch(__obj.type.toLowerCase())
                        {
                            case "text":
                            case "password":
                            case "textarea":
                            {
                                    var isnull=__obj.getAttribute? __obj.getAttribute("isnull") : "";
                                     if(isnull && isnull=="False")
                                    {
                                       if(__obj.value=="" || __obj.value==null)
                                        {
                                            alert(alertMsg);
                                            __obj.focus();
                                            __obj.style.color='red';
                                            return false;                       
                                        }
                                    }
                                    var compare=__obj.getAttribute? __obj.getAttribute("compare") : "";/*create compare */
                                   if (compare)
                                   {
                                        var array=compare.split("|");
                                        var id0=array[0];
                                        var id1=array[1];
                                        if(id0 && id1)
                                        {
                                            if(document.getElementById(id0).value!=document.getElementById(id1).value)/* compare between 2 value*/
                                            {
                                               alert("Xác nhận mật khẩu không chính xác.");
                                               document.getElementById(id1).focus();
                                               return false;
                                            }
                                        }
                                   }
                                  var __mode=__obj.getAttribute? __obj.getAttribute("mode") : "";
                                   if(__mode && __mode=="0-9")/*only number*/
                                    {
                                        var validcode=/^[0-9]$/i;
                                        if(__obj && validcode.test(__obj.value)==false)
                                        {
                                            alert("Kiểm tra kiểu số nhập vào.");
                                            __obj.focus();
                                            __obj.style.color='red';
                                            return false;                       
                                        }
                                    }
                                    if(__mode && __mode=="a-zA-Z0-9")/*a-z*/
                                    {
                                        var validstring=/^[a-zA-Z0-9]+$/;
                                        if(__obj && validstring.test(__obj.value)==false)
                                        {
                                            alert("Kiểm tra kiểu  chuỗi nhập vào. Chuổi phải nằm trong tập hợp='a-z,A-Z,0-9'");
                                            __obj.focus();
                                            __obj.style.color='red';
                                            return false;                       
                                        }
                                    }
                                    if(__mode && __mode=="number")/*number*/
                                    {
                                        if(isNaN(__obj.value)|| __obj.value==null)
                                        {
                                            alert("Kiểm tra kiểu số nhập vào, mode='numeric'");
                                            __obj.onkeypress =function(e)
                                            {
                                                e = e || window.event;
                                                if(e.keyCode < 48 || e.keyCode > 57)
                                                {
                                                    e.returnValue = false;
                                                    obj.focus();
                                                }
                                            }
                                            obj.focus();
                                            obj.style.color='red';
                                            return false;
                                        }
                                    }
                                   if(__mode && __mode=="email")/*email*/
                                    {
                                       var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
                                       if (__obj && emailfilter.test(__obj.value)==false)
		                                {
		                                    alert("Email không hợp lệ.");
			                                __obj.focus();
			                                __obj.style.color='red';
			                                return false;
		                                }	
                                    }
                                    if(__mode && __mode=="date")
                                    {
                                        var _date=/^((\d|[1-2][0-9]|3[0-1])-(\d|1[0-2])-(\d{2}|19\d{2}|[2-9]\d{3})|(\d|[1-2][0-9]|3[0-1])\\(\d|1[0-2])\\(\d{2}|19\d{2}|[2-9]\d{3})|(\d|[1-2][0-9]|3[0-1])\/(\d|1[0-2])\/(\d{2}|19\d{2}|[2-9]\d{3}))$/i;
                                        if (__obj && _date.test(__obj.value)==false)
		                                    {
		                                       alert("Ngày tháng không hợp lệ. Ngày phải là: dd/mm/yyyy.");
			                                   __obj.focus();
			                                   __obj.style.color='red';
			                                   return false;
		                                    }	
                                    }
                                   
                            }
                            break;
                             case "select-one":
                               var ___isnull=__obj.getAttribute? __obj.getAttribute("isnull") : "";
                              if(___isnull && ___isnull=="False")
                               {
                                if ( __obj.options[__obj.selectedIndex].text == "")
                                {
                                    __obj.focus();
                                    alert(alertMsg);
                                    return false;
                                }
                              }
  				            break;
                           case "select-multiple":
                             var ___isnull1=__obj.getAttribute? __obj.getAttribute("isnull") : "";
                            if(___isnull1 && ___isnull1=="False")
                            {  
                                if (__obj.selectedIndex == -1)
                                {
					                alert(alertMsg);
 					                return false;
  				                }
                            }
  				            break;
  				            case "checkbox":
  				             var __isnull=__obj.getAttribute? __obj.getAttribute("isnull") : "";
  				              if(__isnull && __isnull=="False")
  				              {
		                        if(__obj.checked==false)
		                        {
			                      alert("Bạn phải đồng ý với điều khoản");
			                      return false;
			                    }
  				              }
  				              break;
                        }
                    }
                }
            }
        },
     /*Show DateTime*/
     ShowDateTime: function(s)
        {
            var mydate;
            if (s)
            mydate = new Date(s);
            else
            mydate = new Date();
            var year = mydate.getYear();
            if (year < 1000)
            year += 1900;
            var month = mydate.getMonth() + 1;
            if (month < 10)
            month = "0" + month;
            var day = mydate.getDate();
            if (day < 10)
            day = "0" + day;
            var dayw = mydate.getDay();
            var hour = mydate.getHours();
            if (hour < 10)
            hour = "0" + hour;
            var minute=mydate.getMinutes();
            if (minute < 10)
            minute = "0" + minute;
            var dayarray=new Array("Chủ nhật","Thứ hai","Thứ ba","Thứ tư","Thứ năm","Thứ sáu","Thứ bảy");
            document.write(dayarray[dayw]+", "+day+" / "+month+" / " + year);
        },
     /*ReSizeIframe*/
     ReSizeIframe: function(obj)
        {
             if(obj)
            {
                obj.width="99%";
                if(navigator.userAgent.indexOf("Opera")!=-1)
                 {
                     obj.height=obj.contentWindow.document.body.scrollHeight;
                 }
               else  obj.height=obj.contentWindow.document.body.scrollHeight+40;
            }
        },
     /*Bookmarksite*/
     Bookmarksite: function(title,url)
         {
            /// <summary>Tạo  boookmark</summary>
            /// <param name="title">Name</param>
            /// <param name="url">Đường dẫn</param>
            if (window.sidebar) /* firefox*/
                window.sidebar.addPanel(title, url, "");
            else if(window.opere && window.print){ /* opera*/
                var elem = document.createElement('a');
                elem.setAttribute('href',url);
                elem.setAttribute('title',title);
                elem.setAttribute('rel','sidebar');
                elem.click();
            } 
            else if(document.all)/* ie*/
                window.external.AddFavorite(url, title);
        },
    /*Simble tab*/
    ActiveTab: function()
        {
            for(var i = 0; i <  arguments.length; i++)
            {
                var tab = document.getElementById(arguments[i]);
                if(tab)
                {
                    tab.className = "";
                    var body = document.getElementById(tab.getAttribute("rel"));
                    if(body){ body.style.display = "none";
                 
                    }
                }
            }
            if(arguments.length > 0)
            {
                 tab = document.getElementById(arguments[0]);
                if(tab)
                {
                    tab.className = "Selected";
                    body = document.getElementById(tab.getAttribute("rel"));
                    if(body) body.style.display = "block";
                }
            }
        },
     /*Getvideo player*/
     PlayVideoLoading : function(obj)
        {
                /// <summary>Tạo  một loading</summary>
                /// <param name="obj">Id iframe</param>
               if(!window.showModalDialog)
               {
                document.getElementById(obj).style.display = "none";
                }
				function setFlashvar_WMP_status()
				{  
					if(WMP.playstate ==2 || WMP.playstate ==3)
					{
					     document.getElementById(obj).style.display = "none";
					}
				}
				WMP=document.getElementById("MediaPlayer");
				if(WMP)
				{
					if(WMP.addEventListener)
						WMP.addEventListener("playstatechange",setFlashvar_WMP_status, false);
					else
						WMP.attachEvent("playstatechange",setFlashvar_WMP_status);
				}
        },
     PlayVideo: function(v_file,auto_play,w,h)
        {
            /// <summary>Tạo video file</summary>
            /// <param name="v_file">Đường dẫn file</param>
            /// <param name="auto_play">Tự động play</param>
            /// <param name="w">Độ rộng của frame</param>
            /// <param name="h">Chiều cao</param>
           if(navigator.userAgent.indexOf("Safari") != -1 || navigator.userAgent.indexOf("MSIE") != -1)
            {
                var show_txt ;
                show_txt="<object  style='width:"+ w +"; height:"+ h +"' id='MediaPlayer'  classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'>";
                show_txt +="<param name='URL' value='"+ v_file+"'/><param name='ShowControls' value='false'/>";
                show_txt +="<param name='InvokeURLs' value='true' /><param name='windowlessVideo' value='true' />";
                show_txt +="<param name='uiMode' value='full' /><param name='ShowPositionControls' value='true'/>";
                show_txt +="<param name='ShowAudioControls' value='true'/><param name='SendPlayStateChangeEvents' value='true'/>";
                show_txt +="<param name='ShowTracker' value='true'/><param name='AutoStart' value='"+ auto_play+"' />";
                show_txt +="<param name='enableContextMenu' value='true' /><param name='enableErrorDialogs' vaue='true' />";
                show_txt +="<param name='volume' value='50' /><param name='mute' value='false' /></object>";
            }
            else show_txt+="<embed id='FlashPlayer' style='width:"+ w +"; height:"+ h +"' src='"+ v_file +"' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' type='application/x-mplayer2'/>";
            return show_txt;
        },
        TrimWhiteSpace: function(strtext)
        {
            strtext = strtext.replace(/^\s+/g,"");/*emove heading whitespace*/
            strtext = strtext.replace(/\s+$/g,"");/*remove trailing whitespace*/
            return strtext;
        },
        /*popup window*/
         win:null,
        NewWindow: function(url,name,w,h,scroll,pos)
        {
            /// <summary>Popup new window and random position</summary>
            /// <param name="url">Link url</param>
            /// <param name="name">Name display</param>
            /// <param name="w">width popup</param>
            /// <param name="h">Height popup</param>
            /// <param name="scroll">status of newwindow</param>
            /// <param name="pos">Postion</param>
            if(pos=="random")
             {
                LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
                TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
             }
            if(pos=="center")
            {
                LeftPosition=(screen.width)?(screen.width-w)/2:100;
                TopPosition=(screen.height)?(screen.height-h)/2:100;
            }
            else if((pos!="center" && pos!="random") || pos==null)
            {
                LeftPosition=0;
                TopPosition=20
            }
            settings='width='+ w +',height='+ h +',top='+ TopPosition +',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=no,menubar=yes,toolbar=no,resizable=no';
            win=window.open(url,name,settings);
        }
 };
/*
MyClass = function()
    {
    };
MyClass.__class = true;
MyClass.__typeName = "MyClass";
MyClass.prototype.name = "hung";
MyClass.prototype.get_name = function()
    {
        
    };
MyClass.prototype.set_name = function()
    {
    };
MyClass.prototype.add_onclick = function()
    {
    };
MyClass.prototype.remove_onclick = function()
    {
    };
  */
