var old_menu="menu_1";
var old_people=-1;

function change_class(i)
{
	document.getElementById(old_menu).className='menu';
	document.getElementById(i).className='menu_1';
	document.getElementById('menu_div').innerHTML= '<table cellpadding="0"  cellspacing="0" width="100%" height="100%">' + document.getElementById('table_'+i).innerHTML + '</table>';

	document.getElementById('menu_div').style.display='block';
	old_menu=i;
}

function return_class(i)
{
	
	
	check_mouse_in();

}



function check_mouse_in()
{


	var width = document.getElementById('menu_div').offsetWidth;
	var height =document.getElementById('menu_div').offsetHeight;

	if( (event.x<(document.getElementById('main_table').offsetWidth-width)) || (height+69)< event.y)
	{
		document.getElementById('menu_div').style.display='none';
		document.getElementById(old_menu).className='menu';
	}
}

function show_people(i)
{
	if(old_people!=-1)
	document.getElementById('people_'+old_people).style.display ='none';	
	
	document.getElementById('people_'+i).style.display ='block';	
	old_people = i;
}






function prepare_buttons()
{
    var dd = document.getElementsByTagName("BUTTON");
    var i = 0;
    while(dd.item(i) != null)
    {
        dd.item(i).onmouseover = function(){this.className = "clicked_button";}
        dd.item(i).onmouseout  = function(){this.className = "";}
        dd.item(i).onmousedown  = function(){this.className = "mouseover_button";}
        dd.item(i).onmouseup  = function(){this.className = "clicked_button";}
        i++;
    }
    var dd = document.getElementsByTagName("INPUT");
    var i = 0;
    while(dd.item(i) != null)
    {
        if(dd.item(i).className == "btn_3point")
        {
            dd.item(i).onmouseover = function(){this.className = "clicked_btn_3point";}
            dd.item(i).onmouseout  = function(){this.className = "btn_3point";}
        }
        i++;
    }
}

function initial_page()
{
    prepare_buttons();
}

function change_my_status(tmp)
{
    cb_temp  = document.getElementById("cb_"  + tmp);
    img_temp = document.getElementById("img_" + tmp);

    cb_temp.checked = !cb_temp.checked;
    img_temp.src = document.getElementById("img_cb_" + cb_temp.checked).src;
}

function select_item(tmp)
{
    if(old_tmp != "")
        cancel_item(old_tmp);
    cancel_add_tr();
    document.getElementById("tr_view_" + tmp).style.display = "none";
    document.getElementById("tr_edit_" + tmp).style.display = "block";
    document.getElementById("tr_view").style.display = "none";
    document.getElementById("tr_edit").style.display = "block";
    old_tmp = tmp;
}

function delete_item(tmp, mess, page)
{
    if(confirm(mess))
    {
        document.getElementById("job").value = page + "_delete";
        document.getElementById("selected_item").value = tmp;
        document.forms[0].submit();
    }
}

function cancel_item(tmp)
{
    document.getElementById("tr_view_" + tmp).style.display = "block";
    document.getElementById("tr_edit_" + tmp).style.display = "none";
    document.getElementById("tr_view").style.display = "block";
    document.getElementById("tr_edit").style.display = "none";
    document.forms[0].reset();
}

function view_add_tr()
{
    if(old_tmp != "")
        cancel_item(old_tmp)
    document.getElementById("tr_add_header").style.display = "block";
    document.getElementById("tr_add").style.display = "block";
}

function cancel_add_tr()
{
    document.getElementById("tr_add_header").style.display = "none";
    document.getElementById("tr_add").style.display = "none";
    document.forms[0].reset();
}

function submit_add_tr(page)
{
    document.getElementById("job").value = page + "_add";
    check_form(document.forms[0])
}

function submit_edit_tr(tmp, page)
{
    document.getElementById("job").value = page + "_edit";
    document.getElementById("selected_item").value = tmp;
    check_form(document.forms[0]);
}

function show_news(obj, title, body)
{
    document.getElementById("news_title").innerHTML = title;
    document.getElementById("news_body").innerHTML = body;

    document.getElementById("dv_news").style.visibility = "visible";
}

function hide_news()
{
    document.getElementById("news_title").innerHTML = "";
    document.getElementById("news_body").innerHTML = "";

    document.getElementById("dv_news").style.visibility = "hidden";
}

function check_changed()
{
	if(submitting == false)
	{
	    for(i = 0; i < document.forms.length; i++)
	    {
	        for(j = 0; j < document.forms[i].length; j++)
	        {
	            if(document.forms[i][j].getAttribute("defaultValue") != null && document.forms[i][j].defaultValue != document.forms[i][j].value && document.forms[i][j].value != "on")
	                return ">>  Are you sure you want to exit without save?  <<";
	        }
	    }
	}
}

function view_hide_menu(tmp)
{
    if(tmp == "")
        return;
    for(i = 1; i < 11; i++)
    {
        try
        {
            x = document.getElementById("tr_" + tmp + "_" + i);
            if(x.style.display == "none")
               x.style.display = "block";
            else
               x.style.display = "none";
        }
        catch(e)
        {
            //Do Nothing
        }
    }
}

function hide_menu(tmp)
{
    if(tmp == "")
        return;
    for(i = 1; i < 11; i++)
    {
        try
        {
            x = document.getElementById("tr_" + tmp + "_" + i);
            x.style.display = "none";
        }
        catch(e)
        {
            //Do Nothing
        }
    }
    try
    {
        x = document.getElementById("tr_" + tmp + "_0");
        x.className = "header_menu";
    }
    catch(e)
    {
    }
}

function view_menu(tmp)
{
    if(tmp == "")
        return;
    for(i = 1; i < 11; i++)
    {
        try
        {
            x = document.getElementById("tr_" + tmp + "_" + i);
            x.style.display = "block";
        }
        catch(e)
        {
            //Do Nothing
        }
    }
    try
    {
        x = document.getElementById("tr_" + tmp + "_0");
        x.className = "selected_header_menu";
    }
    catch(e)
    {
    }
}

function remove_address()
{
    tbl = document.getElementById("address_table");
    tbd = tbl.getElementsByTagName("tbody").item(0);
    tr_count = tbd.getElementsByTagName("tr").length;
    if(tr_count > 14)
        for(i = 1; i < 19; i++)
        {
            tbd.removeChild(tbd.lastChild);
        }
}

function add_address()
{
    tbl = document.getElementById("address_table");
    tbd = tbl.getElementsByTagName("tbody").item(0);

    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 3;
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);
    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 1;
    temp_td.bgColor = "#B8BCBF";
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);
    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 3;
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);

    for(i = 0; i < 6; i++)
    {
        temp_tr = document.createElement("tr");


        temp_td = document.createElement("td");
        node = document.createTextNode(what_to_add[i]);
        temp_td.appendChild(node);
        temp_td.className = "label_td";
        temp_tr.appendChild(temp_td);


        temp_td = document.createElement("td");

        temp_tr.appendChild(temp_td);


        if(i == 4)
        {
            temp_td = document.createElement("td");
            node = document.createElement("select");
            node.name = what_name[i];
            countries = document.getElementById("countries");
            for(j = 0; j < countries.options.length; j++)
            {
                op = document.createElement("option");
                op.text = countries.options[j].text;
                op.value = countries.options[j].value;
                node.options.add(op);
            }
            temp_td.appendChild(node);
            temp_tr.appendChild(temp_td);
        }
        else
        {
            temp_td = document.createElement("td");
            node = document.createElement("input");
            node.type = "text";
            node.name = what_name[i];
            temp_td.appendChild(node);
            temp_tr.appendChild(temp_td);
        }


        tbd.appendChild(temp_tr);
    }

    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 3;
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);
    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 1;
    temp_td.bgColor = "#B8BCBF";
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);
    temp_tr = document.createElement("tr");
    temp_td = document.createElement("td");
    temp_td.colSpan = 3;
    temp_td.height = 3;
    temp_tr.appendChild(temp_td);
    tbd.appendChild(temp_tr);

    for(i = 6; i < what_to_add.length; i++)
    {
        temp_tr = document.createElement("tr");


        temp_td = document.createElement("td");
        node = document.createTextNode(what_to_add[i]);
        temp_td.appendChild(node);
        temp_td.className = "label_td";
        temp_tr.appendChild(temp_td);


        temp_td = document.createElement("td");

        temp_tr.appendChild(temp_td);


        temp_td = document.createElement("td");
        node = document.createElement("input");
        node.type = "text";
        node.name = what_name[i];
        temp_td.appendChild(node);
        temp_tr.appendChild(temp_td);


        tbd.appendChild(temp_tr);
    }
}

var what_to_add = new Array();
what_to_add[ 0] = "Address 1 : ";
what_to_add[ 1] = "Address 2 : ";
what_to_add[ 2] = "City : ";
what_to_add[ 3] = "State : ";
what_to_add[ 4] = "Country : ";
what_to_add[ 5] = "Zip Code : ";
what_to_add[ 6] = "Phone : ";
what_to_add[ 7] = "Mobile : ";
what_to_add[ 8] = "Fax : ";
what_to_add[ 9] = "P.O.Box : ";
what_to_add[10] = "eMail : ";
what_to_add[11] = "Website : ";

var what_name = new Array();
what_name[ 0] = "address_1[]";
what_name[ 1] = "address_2[]";
what_name[ 2] = "city[]";
what_name[ 3] = "state[]";
what_name[ 4] = "country[]";
what_name[ 5] = "zip_code[]";
what_name[ 6] = "phone[]";
what_name[ 7] = "mobile[]";
what_name[ 8] = "fax[]";
what_name[ 9] = "po_box[]";
what_name[10] = "email[]";
what_name[11] = "website[]";

function delete_activity(tmp)
{
    document.getElementById("what").value = "delete";
    document.getElementById("a_id").value = tmp;
    
    document.client_activities.submit();
}

function edit_activity(tmp)
{
    AjaxRequest.get(
    {
        'url':'./get_activity.php?a_id=' + tmp,
        'onSuccess':update_activity,
        'onLoading':start_loading
    });
}

function update_activity(req)
{
    end_loading();
    //alert(req.responseText);
    eval(req.responseText);
    
    document.getElementById("date_activity").value = my_activity.date_activity;
    document.getElementById("time_activity").value = my_activity.time_activity;
    document.getElementById("activity_type").value = my_activity.id_type;
    document.getElementById("activity_way").value  = my_activity.id_way;
    document.getElementById("subject").value       = my_activity.subject;
    a                                              = my_activity.details.replace("<br>", "\n");
    b = a.replace("<br>", "\n");
    while(a != b)
    {
        a = b;
        b = a.replace("<br>", "\n");
    }
    document.getElementById("details").value = a;
    document.getElementById("a_id").value          = my_activity.id_activity;
    document.getElementById("what").value          = "edit";
}

function start_loading()
{
    document.getElementById("img_loading").style.visibility = "visible";
}

function end_loading()
{
    document.getElementById("img_loading").style.visibility = "hidden";
}
