var VALUE_FOR_NULL  = "***";
var TEXT_FOR_NULL   = "---";

function clearDDL(ddl)
{
    var k;
    var lngth;
    
    if(null == ddl)
        return;
    
    lngth = ddl.length;
    for(k = 0; k < lngth ; k++)
        ddl.remove(0);        
}

function getItemValue(ddl)
{
    if (null == ddl)
        return null;
    
    return ddl.options.item(ddl.selectedIndex).value;
}

function setValue(ddl, v)
{
    var k;
    var lngth;
    var ops;
    
    if (null == ddl)
        return null;

    ops = ddl.options;
    lngth = ops.length;
    if (0 == lngth)
        return false;
        
    for (k = 0; k < lngth; k++)
    {
        if (ops.item(k).value == v)
        {
            ddl.selectedIndex = k;
            return true;
        }
    }
    return false;
}

function addNullValue(ddl)
{
    addItem(VALUE_FOR_NULL, TEXT_FOR_NULL, ddl);
}

function addItem(sValue, sText, ddl)
{
    var newitem; // new ddl item
    
    newitem = document.createElement("OPTION");
    newitem.value   = sValue;
    newitem.text    = sText;
    
    ddl.options.add(newitem);
    return newitem;
}

function addInUseObject(o, ddl)
{
    addItem(new String(o.ID), o.Text, ddl);
    applyStyle(ddl.options.item(ddl.options.length - 1), o.InUse);
}


function addComplexObject(o, ddl, asOffice)
{
    addItem(new String(o.ID), o.Text, ddl);
    //applyStyle(ddl.options.item(ddl.options.length - 1), (asOffice && o.ExistsInOffice) || (!asOffice && o.ExistsInAccountant));
}
	
function applyStyle(o, inUse)
{
	//o.style.fontWeight = "bold";
	//o.style.color = "#006699";
	
	if (inUse){
		o.style.color = "black";		
		//o.style.backgroundColor = "#C7E2E2";
		}
	else{
		o.style.color = "#999999";		
		//o.style.backgroundColor = "#D7EBEB";
		}
}

function ApplyComplexObjectStyle(arr,ddl,asOffice){

    var k;
    var lngth;
    
    lngth = arr.length;
    if (0 == lngth)
        return;
        
        
    //Mark empty items
    for (k = 0; k < lngth; k++)
    {
		applyStyle(ddl.options.item(k+1), (asOffice && arr[k].ExistsInOffice) || (!asOffice && arr[k].ExistsInAccountant));
    }
}

function processComplexObjects(arr, ddl, asOffice)
{
	if (ddl.length==0){
	    clearDDL(ddl);
		addNullValue(ddl);    
	    addComplexObjects(arr, ddl, asOffice);
	    }
	ApplyComplexObjectStyle(arr,ddl,asOffice);
}


function processInUseObjects(arr, ddl)
{
    clearDDL(ddl);
    addNullValue(ddl);    
    addInUseObjects(arr, ddl);
}


var lastAreaCode;
function processComplexCities(arr, ddl, asOffice)
{
	if (ddl.length==0 || xddlAreaCodes.selectedIndex!=lastAreaCode){
		lastAreaCode=xddlAreaCodes.selectedIndex;
		clearDDL(ddl);
		addNullValue(ddl);    
	    addComplexCities(arr, ddl, asOffice);
	 }
	//ApplyComplexObjectStyle(arr,ddl,asOffice); //currently marked because Ofer canceled the SQL query to improve performances
	    
}

function addComplexCities(arr, ddl, asOffice)
{
    var k;
    var lngth;
    var idx;
    var areaCode;
    
    lngth = arr.length;
    if (0 == lngth)
        return;
        
    if (xddlAreaCodes.options.length == 0)
        return;
    
    // get the selected index
    idx = xddlAreaCodes.selectedIndex;
    
    // is it the null value?    
    if (0 == idx)
    {
        // it is. fill all the cities
        for (k = 0; k < lngth; k++)
        {
            addComplexObject(arr[k], ddl, asOffice);
        }
        return;
    }
    else
    {
	    // it's not a null value.
		// get the real value
    
		// first, get the item object
		areaCode = xddlAreaCodes.options.item(idx);
		if (null == areaCode)
	        return;
	    // then, get the value itself
	    areaCode = areaCode.value;

		// now add the city only if 
		// its area code is equal to the selected one
		for (k = 0; k < lngth; k++)
		{
	        if (arr[k].AreaCodeID == areaCode)
	            addComplexObject(arr[k], ddl, asOffice);
	    }
    }
}

function addInUseObjects(arr, ddl)
{
    var k;
    var lngth;
    
    lngth = arr.length;
    if (0 == lngth)
        return;
        
    for (k = 0; k < lngth; k++)
    {
        addInUseObject(arr[k], ddl);
    }
}

function addComplexObjects(arr, ddl, asOffice)
{
    var k;
    var lngth;
    
    lngth = arr.length;
    if (0 == lngth)
        return;
        
    for (k = 0; k < lngth; k++)
    {
        addComplexObject(arr[k], ddl, asOffice);
    }
}

function ComplexItem(ID, text, ExistsInOffice, ExistsInAccountant)
{
    this.ID                 = ID;
    this.Text               = text;
    this.ExistsInOffice     = ExistsInOffice;
    this.ExistsInAccountant = ExistsInAccountant;
} 

function InUseItem(ID, text, inUse)
{
    this.ID		= ID;
    this.Text	= text;
    this.InUse	= inUse;
} 


function ComplexCity(ID, text, ExistsInOffice, ExistsInAccountant, AreaCodeID)
{
    this.ID                 = ID;
    this.Text               = text;
    this.ExistsInOffice     = ExistsInOffice;
    this.ExistsInAccountant = ExistsInAccountant;
    this.AreaCodeID         = AreaCodeID;
}

function SimpleCity(ID, text, inUse, AreaCodeID)
{
    this.ID         = ID;
    this.Text       = text;
    this.InUse      = inUse;
    this.AreaCodeID = AreaCodeID;
}

function ComplexPracticeArea(ID, text, ExistsInOffice, ExistsInAccountant)
{
    this.ID						= ID;
    this.Text					= text;
    this.ExistsInOffice			= ExistsInOffice;
    this.ExistsInAccountant		= ExistsInAccountant;
}

function SimplePracticeArea(ID, text, inUse)
{
    this.ID						= ID;
    this.Text					= text;
    this.InUse					= inUse;
}

function getElemByName(name)
{
    var a;
    var o;
    
    o = document.getElementById(name);
    if (o != null)
        return o;
    
    a = document.getElementsByName(name);
    if (a.length == 0)
        return null;
    
    o = a.item(0);
    
    return o;
}

Array.prototype.has = function(value) {
	for (var i = 0, loopCnt = this.length; i < loopCnt; i++)
	{
		if (this === value) {
			return true;
		}
	}
	return false;
};

