﻿ var isDebug = window.location.href.indexOf('debug') > 0;
 //Ham cat ky tu trang  
    function trim (str) {
	    var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	    for (var i = 0; i < str.length; i++) {
		    if (whitespace.indexOf(str.charAt(i)) === -1) {
			    str = str.substring(i);
			    break;
		    }
	    }
	    for (i = str.length - 1; i >= 0; i--) {
		    if (whitespace.indexOf(str.charAt(i)) === -1) {
			    str = str.substring(0, i + 1);
			    break;
		    }
	    }
	    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
    }
 
 //Ham cat ky tu trang- ben trai   
    function lTrim(sString)
    {
        while (sString.substring(0,1) == ' ')
        {
            sString = sString.substring(1, sString.length);
        }
        return sString;
    }
 
 //Ham cat ky tu trang- ben phai   
    function rTrim(sString)
    {
        while (sString.substring(sString.length-1, sString.length) == ' ')
        {
            sString = sString.substring(0,sString.length-1);
        }
        return sString;
    }
 
 //Ham cat ky tu trang- ca 2 ben   
    function lrTrim(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;
    }
    
    //Ham kiem tra text co chua the HTML ko?
    function validateHTMLTag(text)
    {
        if(text.match(/([\<])([^\>]{1,})*([\>])/i)==null)
            return true;
        else
            return false;
    }

    //Ham kiem tra do dai cua text
    function validateTextLength(text, min, max)
    {
        if(lrTrim(text).toString().length <= min || lrTrim(text).toString().length > max)
            return false;
        else
            return true;   
    }
    
    //Ham kiem tra do dai toi da cua text
    function validateTextMax(text, max)
    {
        
        if(lrTrim(text).toString().length > max)
            return false;
        else
            return true;   
    }
    //Ham kiem tra trong chuoi text co chu nao dai qua ko?
    function validateWordLength(text, maxLength)
    {
         if(text.length == 0)
        {
            return "";
        }
        else
        {
            //Replace Enter key by = ' '
            var newText = "";
            for (var i = 0; i < text.length; i++) 
            {
                if ((text.charCodeAt(i) == 10)) 
                {
                    newText += " ";
                } 
                else 
                {
                    newText += text.charAt(i);
                }
            }
            //End 
            
            var temp = new Array();
            temp = newText.split(' ');
            
            for(i = 0; i < temp.length; i++)
            {
                if(temp[i].length >= maxLength)
                {
                    return temp[i];
                }            
            }
            
            return "";
        }
        
    }
    
    //Ham nay su dung de chi cho phep cac o nhap, nhap toi da maxLen ky tu.
    function chkLen(ObjectId,maxLen)
    {
        //alert(ObjectId);
        try{
            var code = document.getElementById(ObjectId);
            if(code.value.length > maxLen)
            {
                var limitCode = code.value.substring(0,maxLen);
                code.value = limitCode;
            }
        }catch(ex)
        {
            if(isDebug) alert("chkLen: " + ex.message);
        }
    }
    
    function isNumeric(val) {
        return val.length > 0 && isFinite(val);
//tuannq commented: this method was wrong in algorithm and purpose
//        var numeric = true;
//        var chars = "0123456789";
//        var len = val.length;
//        var char = "";
//        for (i=0; i<len; i++) { char = val.charAt(i); if (chars.indexOf(char)==-1) { numeric = false; } }
//        return numeric;
    }

/////////////////////////

//TDictionary Begin
///
TDictionary = function()
{
	this._Key = new Array();
	this._Item = new Array();
	this.Count = 0;
	
	this.Exists = function(key)
	{
	    try{
		    for(var i = 0; i < this._Key.length; i++)
		    {
			    if(this._Key[i] == key)
			    {
				    return true;
			    }
		    }
		    return false;
		}catch(ex)
		{
		    if(isDebug) alert("TDictionary.Exists: " + ex.message);
		}
	}
	this.Add = function(key, value)
	{
	    try{
		    var exists = false;
		    for(var i = 0; i < this._Key.length; i++)
		    {
			    if(this._Key[i] == key)
			    {
				    exists = true;
				    break;
			    }
		    }
		    if(!exists)
		    {
			    this._Key[this._Key.length] = key;
			    this._Item[this._Item.length] = value;
			    this.Count = this._Key.length;
		    }
		}catch(ex)
		{
		    if(isDebug) alert("TDictionary.Add: " + ex.message);
		}
	}
	this.Remove = function(key)
	{
	    try{
		    for(var i = 0; i < this._Key.length; i++)
		    {
			    if(this._Key[i] == key)
			    {
				    this._Key.splice(i, 1);
				    this._Item.splice(i, 1);
				    this.Count--;
			    }
		    }
		}catch(ex)
		{
		    if(isDebug) alert("TDictionary.Remove: " + ex.message);
		}
	}
	this.RemoveAll = function()
	{
	    try{
		    this._Key.splice(0, this._Key.length);
		    this._Item.splice(0, this._Item.length);
		    this.Count = 0;
		}catch(ex)
		{
		    if(isDebug) alert("TDictionary.RemoveAll: " + ex.message);
		}
	}	
	this.Item = function(key)
	{
	    try{
	        for(var i = 0; i < this._Key.length; i++)
	        {
	            if(this._Key[i] == key)
	            {
	                return this._Item[i];
	            }
	        }
	        return null;
	    }catch(ex)
	    {
	        if(isDebug) alert("TDictionary.Item: " + ex.message);
	    }
	}
	this.Key = function(key)
	{
	    try{
	        for(var i = 0; i < this._Key.legnth; i++)
	        {
	            if(this._Key[i] == key)
	            {
	                return this._Key[i];
	            }
	        }
	    }catch(ex)
	    {
	        if(isDebug) alert("TDictionary.Key: " + ex.message);
	    }
	}
	this.Items = function()
	{
		return this._Item;
	}	
	this.Keys = function()
	{
		return this._Key;
	}
}
/**
TDictionary End*/

// Ham chi cho fep nhap so
// calculate the ASCII code of the given character
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkNumber(val) { 
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);

  /* Check if the keyed in character is a number
     do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective
     codes and replace the 48 and 57 */

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

////////// Validate Datetime

function check_date(val)
{
   
    var checkstr = "0123456789";
    var DateField = val;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    var txtErr="";
   
    DateValue = DateField.value;
    
    /* Delete all chars except 0..9 */
    for(i = 0; i < DateValue.length; i++)
    {
        if(checkstr.indexOf(DateValue.substr(i,1)) >= 0) 
        {
        if(!DateValue.substr(i,1)==seperator)
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    DateValue = DateTemp;

    if(DateValue.length<8)
    {
        err = 20;
        txtErr = "Bạn phải nhâp ngày tháng đủ 8 ký tự!";
         
    }
    
    /* Validation of Year*/
    
    year = DateValue.substr(4,4);
    
    if((year < 1701) || (year > 9999))
    {
        err = 19;
        txtErr = "Bạn phải nhập năm giữa 1701 và 9999!";
        
    }
    

    /* Validation of month*/
    month = DateValue.substr(2,2);
    if ((month < 1) || (month > 12))
    {
     err = 21;
     txtErr ="Bạn nhập sai định dạng của tháng!";
      
    }
    
    /* Validation of day*/
    day = DateValue.substr(0,2);    
    if((day < 1) || (day > 31))
    {
        err = 22;
        txtErr ="Bạn nhập sai định dạng của ngày!";
        
    }
    
    /* Validation leap-year / february / day */
    if((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
    {
        leap = 1;
    }
    if((month == 2) && (leap == 1) && (day > 29))
    {
        err = 23;
        txtErr="Ngày của tháng này không hợp lệ!";
       
    }
    if((month == 2) && (leap != 1) && (day > 28))
    {
        err = 24;
        txtErr="Ngày của tháng này không hợp lệ!";
      
    }
    
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
    err = 25;
    txtErr="Ngày của tháng này không hợp lệ!";
   
    }
    if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
    err = 26;
    txtErr="Ngày của tháng này không hợp lệ!";
    
    }
    
    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
      day = ""; month = ""; year = ""; seperator = "";
    }
    
    /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
    if (err == 0) 
    {
        DateField.value = day + seperator + month + seperator + year;
        return true;
        
    }
    /* Error-message if err != 0 */
    else 
    {     
     alert("Bạn nhập ngày tháng không đúng định dạng!"); 
     alert(txtErr);
     return false;
    }
}
// End -->

//// Check khoang thoi gian
function validate_Time(todate,fromdate)
{    
    var checkstr = "0123456789";
    var check = true;
    var DateTemp = "";
   
    var ToDateValue = "";
     ToDateValue = todate.value;
     
    var FrmDateValue = "";
     FrmDateValue = fromdate.value;
     
    /* Delete all chars except 0..9 */
    for(i = 0; i < ToDateValue.length; i++)
    {
        if(checkstr.indexOf(ToDateValue.substr(i,1)) >= 0) 
        {
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    ToDateValue = DateTemp;
    DateTemp = "";
    for(i = 0; i < FrmDateValue.length; i++)
    {
        if(checkstr.indexOf(FrmDateValue.substr(i,1)) >= 0) 
        {
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    FrmDateValue = DateTemp;    
    if(FrmDateValue.substr(4,4) > ToDateValue.substr(4,4))
    {
        check = false;
    }
    if(FrmDateValue.substr(4,4) == ToDateValue.substr(4,4))
    {
        if(FrmDateValue.substr(2,2) > ToDateValue.substr(2,2))
        {
            check = false;
        }
        if(FrmDateValue.substr(2,2) == ToDateValue.substr(2,2))
        {
            if(FrmDateValue.substr(0,2) > ToDateValue.substr(0,2))
            {
                check = false;
            }
        }
    }   
    return check;
}
////////////////

function open_teaser(id_text, id_nut_open, id_nut_close)
{
document.getElementById(id_text).className = "detail_product_teaser_v2";
document.getElementById(id_nut_open).style.display= "none";
document.getElementById(id_nut_close).style.display= "inline";
}
function close_teaser(id_text, id_nut_open, id_nut_close)
{
document.getElementById(id_text).className = "detail_product_teaser_close_v2";;
document.getElementById(id_nut_close).style.display= "none";
document.getElementById(id_nut_open).style.display= "inline";
}