//define the Exception class
var NEW_EXCEPTION_ID_PRE = "newException_";

//define the sep for policy
var SEP_ACCOUNTS_DISPLAY = ";";
var SEP_EXCEPTION = "||";
var SEP_SP_LOCATION = ",";
var SEP_EXCEPTION_INSIDE = "*";

// for Account String join
var lenLength = 6;
var lenType = 2;
var STR_ANYONE = "ANYONE";
var STR_ANYSITE = "ANYSITE";

function CException(id)
{
    this.id = id?id:"new";     //store the id in database
    
    this.vcGuidStore = new Array;
    this.strSiteDisplay = "";

    this.strAccountGuids = "";     //AccountStrAccountStrAccountStr
    this.strAccountDisplay = ""; //account.Name;account.Name;account.Name;account.Name;  according with accountList

    this.bIsChecked = false;  //for the exceptionList show, prepare for delete
    
    //str = id & strAccountGuids & strSitesGuid
    this.ToString = function()
    {
        var str = this.id + SEP_EXCEPTION_INSIDE + this.strAccountGuids + SEP_EXCEPTION_INSIDE;
        if (this.IsAnySite())str += STR_ANYSITE;
        else
        {
            for (var i=0; i<this.vcGuidStore.length; i++)
            {
                str += this.vcGuidStore[i]
                str += SEP_SP_LOCATION;
            }
        }
        return str;
    }
    this.getCurrentSiteId = function()
    {
        if (this.IsAnySite() || this.vcGuidStore.length == 0)return "";
        var lastGuidStore = this.vcGuidStore[this.vcGuidStore.length -1];
        var start = 1;
        var end = lastGuidStore.indexOf("}");
        return lastGuidStore.substring(start, end);
    }
    this.IsAnyone = function()
    {
        return this.strAccountGuids == STR_ANYONE;
    }
    this.IsAnySite = function()
    {
        return this.vcGuidStore[0] == STR_ANYSITE;
    }
    this.getAccountArray = function(vecAccountStr, vecAccountDisplay)
    {       
        if (this.strAccountGuids == "" || this.strAccountGuids == STR_ANYONE)
        {
            alert("unsupported erro for getAccountArray in policy.js");
            return 
        }
        var start = 0;

        var nLen = this.strAccountGuids.length;
        if (nLen > lenLength+lenType)
        {
            while(start + lenLength + lenType < nLen)
            {
                var strItemlen = setTrim(this.strAccountGuids.substr(start,lenLength));
                var nItemLen = parseInt(strItemlen);
                var end = start + lenLength + lenType + nItemLen -1;
                var strAccount = this.strAccountGuids.substr(start, end- start + 1);
                
                vecAccountStr.push(strAccount);
                start = end + 1;
            }            
        }
         
        var vecTemp = this.strAccountDisplay.split(SEP_ACCOUNTS_DISPLAY);        
        for(var i=0; i<vecTemp.length; i++)
        {
            if(vecTemp[i] != "")vecAccountDisplay.push(vecTemp[i]);
        }
    }

    //str = id & strAccountGuids & strSitesGuid & strAccountDisplay & strSiteDisplay
    this.serializeFromString = function(str)
    {
        var arrayOfString = str.split(SEP_EXCEPTION_INSIDE);
        this.id = arrayOfString[0];
        this.strAccountGuids = arrayOfString[1];
        
        var vcTemp = arrayOfString[2].split(SEP_SP_LOCATION);
        for (var i = 0; i < vcTemp.length; i++)
        {
            if (vcTemp[i] != "")
            {
                this.vcGuidStore.push(vcTemp[i]);
            }
        }
        this.strAccountDisplay =  arrayOfString[3];
       
        this.strSiteDisplay =  arrayOfString[4];
    }
	this.deserializeString = function(str)
	{
	    var str = this.id + SEP_EXCEPTION_INSIDE + this.strAccountGuids + SEP_EXCEPTION_INSIDE;
        if (this.IsAnySite())str += STR_ANYSITE;
        else
        {
            for (var i=0; i<this.vcGuidStore.length; i++)
            {
                str += this.vcGuidStore[i]
                str += SEP_SP_LOCATION;
            }
        }
		str+=SEP_EXCEPTION_INSIDE+this.strAccountDisplay+SEP_EXCEPTION_INSIDE;
		if(this.IsAnySite())
		   str += STR_ANYSITE;
		else
          str += this.strSiteDisplay;		
        return str;
	}
    /*  check the guidStore wether in the vcGuidStore
     *  return: 1,"P" partial checked  2, "A" checked 3, "N" not in as not checked 
     */
    this.checkedItem = function(strGuidStore)
    {
        for (var i = 0; i < this.vcGuidStore.length; i++)
        {
            if (this.vcGuidStore[i].indexOf(strGuidStore) != -1)
            {
                if(this.vcGuidStore[i].indexOf(strGuidStore+"P") != -1)
                    return "P";
                return "A"
            }
        }
        return "N";
    }
    this.isNewException = function()
    {
        if (this.id.indexOf(NEW_EXCEPTION_ID_PRE) == -1)return false;
        return true;
    }
  
}

function CRule()
{
    this.id = "new";              //store the id in database
    this.deleteExceptions = "";    
    this.exceptionList = new Array;
    this.AddException = function()  //return the index of new Excetpion
    {
        //make sure the new exception has the uinique new id
        var maxNewId = 0;
        for(var i=0; i<this.exceptionList.length; i++)
        {
            if (!this.exceptionList[i].isNewException())continue;
            var strId = this.exceptionList[i].id.substr(13);
            var nId = parseInt(strId);
            if (maxNewId < nId)maxNewId = nId;
        }
        maxNewId++;
        var strNewId = NEW_EXCEPTION_ID_PRE + maxNewId;
         
        var t = new CException(strNewId);
        var len = this.exceptionList.push(t);
        return len-1;
    }
    
    this.RemoveException = function(index)
    {
        if (!this.exceptionList[index].isNewException())
        {
            this.deleteExceptions += this.exceptionList[index].id + SEP_EXCEPTION;
        }
        this.exceptionList.splice(index,1);
    }

    this.getException = function(index)
    {
        return this.exceptionList[index];
    }

    this.searchException = function(id)
    {
        for(var i=0; i<this.exceptionList.length; i++)
        {
            if (this.exceptionList[i].id == id)
                return this.exceptionList[i]
        }
        return null;
    }

    this.length = function()
    {
        return this.exceptionList.length;
    }

    this.checked = function(id)
    {
        for(var i=0; i<this.exceptionList.length; i++)
        {
            if (this.exceptionList[i].id == id)
                this.exceptionList[i].bIsChecked = !(this.exceptionList[i].bIsChecked);
        }
    }
    this.allChecked = function()
    {
        if(this.exceptionList.length <=0)return false;
        for(var i=0; i<this.exceptionList.length; i++)
        {
            if(!this.exceptionList[i].bIsChecked)return false;
        }
        return true;
    }
    this.checkAll = function(bCheck)
    {
        for(var i=0; i<this.exceptionList.length; i++)
        {
            this.exceptionList[i].bIsChecked = bCheck;
        }
    }
    this.hasChecked = function()
    {
        for(var i=0; i<this.exceptionList.length; i++)
        {
            if(this.exceptionList[i].bIsChecked)return true;
        }
        return false;
    }

    this.actionDelete = function()
    {
        var i = 0;
        while( i < this.exceptionList.length)
        {
            if (this.exceptionList[i].bIsChecked)
                this.RemoveException(i);
            else
                i++;
        }   
    }
    this.parseString = function(str)
    {
        var arrayOfString = str.split(SEP_EXCEPTION);
        for (var i =0; i< arrayOfString.length-1; ++i)
        {
            var strException = arrayOfString[i];
            if (strException == "")continue;
            var t = new CException();
            t.serializeFromString(strException);
            this.exceptionList.push(t);
        }
    }
    this.ToString = function()
    {
        var str = "";
        for(var i=0; i<this.exceptionList.length; i++)
        {
            str += this.exceptionList[i].ToString();
            str += SEP_EXCEPTION;
        }
        return str;
    }
	this.DeserializeString = function()
    {
        var str = "";
        for(var i=0; i<this.exceptionList.length; i++)
        {
            str += this.exceptionList[i].deserializeString();
            str += SEP_EXCEPTION;
        }
        return str;
    }
	this.checkRange = function(start,end,bCheck)
	{
	   for(var i=start; i<end; i++)
	     this.exceptionList[i].bIsChecked = bCheck;
	}
    
}

