///////////////////////////////////////////////////////////////////////
//////////////////////General JavaScript Functions///////////////////////
///////////////////////////////////////////////////////////////////////

function switchid(id,myClassName){
	// if (myClassName == 'previewDiv') {
		// document.getElementById('previewTabID').value = id;
	// }
	hideallids(myClassName);
	showdiv(id);
}

function hideallids(myClassName){
	//find all of the divs on the page
	var divs = document.getElementsByTagName("div");
	var i=0;
	for (i=0; i < divs.length; i++) {

		//check for the correct class
		if ((divs[i].className) && (divs[i].className == myClassName)) {

			//hide the div
			hidediv(divs[i].id);
		}
	}
}


function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function hidetab(){
    hidediv('TabsDiv');
}

function hidegroups(){
    for (var i = 1; i <= 8; i++){
            hidediv("Group"+i+"Div");
            for (var z = 1; z <= 5; z++){
                hidediv("Group"+i+"Sub"+z+"Div");
            }
    }
}

function hidechildren(parent,subLevel){
    for (var z = subLevel; z <= 3; z++){
		var obj = document.getElementById("group"+parent+"sub"+z);
		obj.options.length = 0;
        hidediv("Group"+parent+"Sub"+z+"Div");
    }
}

function hidelocation(){
    hidediv('SearchByDiv');
    hidediv('CitiesDiv');
    hidediv('CountiesDiv');
    hidediv('StatesDiv');
    hidediv('SearchDiv');
    hidediv('MMGroupsDiv');
	hidediv('RegionsDiv');
}

///////////////////////////////////////////////////////////////////////
/////////////////////////////AJAX Functions////////////////////////////
///////////////////////////////////////////////////////////////////////

var ajax = new sack();

function metaAdminState(sel)
{
	var ID = sel.options[sel.selectedIndex].value;

	if (ID != 0) {
		//if (document.location.host == "74.40.170.142")
		//{
			//ajax.requestFile = "/mm3_single/ajax/set_meta_admin_state/"+ID;
                        ajax.requestFile = CI.base_url + "ajax/set_meta_admin_state/"+ID;

		//} else {
		//	ajax.requestFile = "/mm3/ajax/set_meta_admin_state/"+ID;
		//}
                
		ajax.onCompletion = metaState;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function metaState()
{
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
	//document.write("<pre>");
	//document.write(ajax.response);
	//document.write("</pre>");
}

function set_biz_consumer(user_value)
{
        /*
        if (document.location.host == "74.40.170.142")
        {
                ajax.requestFile = "/mm3_single/ajax/set_user_type/"+user_value;
        } else {
                ajax.requestFile = "/mm3/ajax/set_user_type/"+user_value;
        }
        */
        ajax.requestFile = CI.base_url + "ajax/set_user_type/"+user_value;

        ajax.onCompletion = metaState;	// Specify function that will be executed after file has been found
        ajax.runAJAX();		// Execute AJAX function
}

function getTabs(sel)
{
	if (sel.nodeName == "SELECT") {
            var ID = sel.options[sel.selectedIndex].value;
        } else {
            var ID = sel.value;
            //var ID = 2007;
            //var ID = document.getElementById(sel).value;
        }


        //var ID = 1742;

        hidelocation();
        hidegroups();
	if (ID != 0) {
                /*
                if (document.location.host == "74.40.170.142")
                {
                    ajax.requestFile = "/mm3_single/ajax/get_tabs/"+ID;
                } else {
                    ajax.requestFile = "/mm3/ajax/get_tabs/"+ID;
                }
                */
                ajax.requestFile = CI.base_url + "ajax/get_tabs/"+ID;
                
		ajax.onCompletion = createTabs;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createTabs()
{
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
	//document.write("<pre>");
	//document.write(ajax.response);
	//document.write("</pre>");
}

function getGroups(sel)
{
	//var ID = sel.options[sel.selectedIndex].value;
        if (sel.nodeName == "SELECT") {
            var ID = sel.options[sel.selectedIndex].value;
        } else {
            var ID = sel.value;
            //var ID = 2008;
            //var ID = document.getElementById(sel).value;
        }
        hidelocation();
        hidegroups();

	if (ID != 0) {
                //run the MMGroups code
                //show MMGroups, if there are any
                //showdiv('MMGroupsDiv');
                //ajax.requestFile = "/mm3_single/ajax/display_mmgroups_list/";
                //ajax.onCompletion = createMMGroupsDropDown;	// Specify function that will be executed after file has been found
                //ajax.runAJAX();		// Execute AJAX function

                showdiv('SearchByDiv');
		//check for value of SearchByDiv
		var searchByObj = document.getElementById("SearchBy");
		var searchByID = searchByObj.options[searchByObj.selectedIndex].value;
		getSearchBy(searchByObj);
		switch (searchByID) {
			case 0:
				showdiv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 1:
				hidediv('CitiesDiv');
				showdiv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 2:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 3:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				showdiv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 4:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				showdiv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 5:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				showdiv('RegionsDiv');
				break;
			default:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;				
		}
		showdiv('SearchDiv');
                /*
                if (document.location.host == "74.40.170.142")
                {
                    ajax.requestFile = "/mm3_single/ajax/get_groups/"+ID;
                } else {
                    ajax.requestFile = "/mm3/ajax/get_groups/"+ID;
                }
                */
                ajax.requestFile = CI.base_url + "ajax/get_groups/"+ID;
                
		ajax.onCompletion = createGroups;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function getGroupsConfig(sel, qtype)
{
	//var ID = sel.options[sel.selectedIndex].value;
        if (sel.nodeName == "SELECT") {
            var ID = sel.options[sel.selectedIndex].value;
        } else {
            var ID = sel.value;
            //var ID = 2008;
            //var ID = document.getElementById(sel).value;
        }
        hidelocation();
        hidegroups();

	if (ID != 0) {
                //run the MMGroups code
                //show MMGroups, if there are any
                //showdiv('MMGroupsDiv');
                //ajax.requestFile = "/mm3_single/ajax/display_mmgroups_list/";
                //ajax.onCompletion = createMMGroupsDropDown;	// Specify function that will be executed after file has been found
                //ajax.runAJAX();		// Execute AJAX function

                showdiv('SearchByDiv');
		//check for value of SearchByDiv
		var searchByObj = document.getElementById("SearchBy");
		var searchByID = searchByObj.options[searchByObj.selectedIndex].value;
		getSearchBy(searchByObj);
		switch (searchByID) {
			case 0:
				showdiv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 1:
				hidediv('CitiesDiv');
				showdiv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 2:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 3:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				showdiv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 4:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				showdiv('ZIPDiv');
				hidediv('RegionsDiv');
				break;
			case 5:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				showdiv('RegionsDiv');
				break;
			default:
				hidediv('CitiesDiv');
				hidediv('CountiesDiv');
				hidediv('StatesDiv');
				hidediv('ZIPDiv');
				hidediv('RegionsDiv');
				break;	
		}
		showdiv('SearchDiv');
                /*
                if (document.location.host == "74.40.170.142")
                {
                    ajax.requestFile = "/mm3_single/ajax/get_groups_config/"+ID+"/"+qtype;
                } else {
                    ajax.requestFile = "/mm3/ajax/get_groups_config/"+ID+"/"+qtype;
                }
                */
                ajax.requestFile = CI.base_url + "ajax/get_groups_config/"+ID+"/"+qtype;
		ajax.onCompletion = createGroups;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createGroups()
{
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
	//document.write("<pre>");
	//document.write(ajax.response);
	//document.write("</pre>");
}

function getNextSearchLevel(groupLevel, subLevel,sel)
{
	var ID = sel.options[sel.selectedIndex].value;
	var nextSubLevel = subLevel+1;

        if (subLevel == 0)
        {
            //probably not the right approach is we have deeper sub levels
            hidechildren(groupLevel,nextSubLevel);
        }

	if (ID != 0) {

                //ajax.requestFile = "http://74.40.170.142/mm_single/mm3_ajax/ajax_SearchLevel.php?ID="+ID+"&SearchLevel="+nextSearchLevel;
                //ajax.requestFile = "http://74.40.170.142/mm3_single/search_temp/ajax_test/"+nextSearchLevel+"/"+ID;
                /*
                if (document.location.host == "74.40.170.142")
                {
                    ajax.requestFile = "/mm3_single/ajax/generate_search_level/"+groupLevel+"/"+nextSubLevel+"/"+ID;
                } else {
                    ajax.requestFile = "/mm3/ajax/generate_search_level/"+groupLevel+"/"+nextSubLevel+"/"+ID;
                }
                */
                ajax.requestFile = CI.base_url + "ajax/generate_search_level/"+groupLevel+"/"+nextSubLevel+"/"+ID;
                
		ajax.onCompletion = createNextSearchLevel;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
	else
	{
		hidechildren(groupLevel,nextSubLevel);
	}
}

function createNextSearchLevel()
{
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
	//document.write("<pre>");
	//document.write(ajax.response);
	//document.write("</pre>");
}

function getSearchBy(sel)
{
	var ID = sel.options[sel.selectedIndex].value;


	if (ID == 0){ //city selected
		showdiv('CitiesDiv');
		hidediv('CountiesDiv');
		hidediv('StatesDiv');
		hidediv('ZIPDiv');
		hidediv('RegionsDiv');
                /*
		if (document.location.host == "74.40.170.142")
		{
			ajax.requestFile = "/mm3_single/ajax/display_city_list/";
		} else {
			ajax.requestFile = "/mm3/ajax/display_city_list/";
		}
                */
		ajax.requestFile = CI.base_url + "ajax/display_city_list/";
		ajax.onCompletion = createCityDropDown;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function

	}

	if (ID == 1){ //county selected
		showdiv('CountiesDiv');
		hidediv('CitiesDiv');
		hidediv('StatesDiv');
		hidediv('ZIPDiv');
		hidediv('RegionsDiv');
                /*
		if (document.location.host == "74.40.170.142")
		{
			ajax.requestFile = "/mm3_single/ajax/display_county_list/";
		} else {
			ajax.requestFile = "/mm3/ajax/display_county_list/";
		}
                */
		ajax.requestFile = CI.base_url + "ajax/display_county_list/";
		ajax.onCompletion = createCountyDropDown;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function

	}

	if (ID == 2){
		//hideAll();
		hidediv('CitiesDiv');
		hidediv('CountiesDiv');
		hidediv('StatesDiv');
		hidediv('ZIPDiv');
		hidediv('RegionsDiv');
	}

	 if (ID == 3){ //multi state
		showdiv('StatesDiv');
		hidediv('CountiesDiv');
		hidediv('CitiesDiv');
		hidediv('ZIPDiv');
		hidediv('RegionsDiv');
	 }

	if (ID == 4){
		//hideAll();
		//zip code
		hidediv('CitiesDiv');
		hidediv('CountiesDiv');
		hidediv('StatesDiv');
		showdiv('ZIPDiv');
		hidediv('RegionsDiv');
	}
	
	if (ID == 5){ //region selected
		hidediv('CountiesDiv');
		hidediv('CitiesDiv');
		hidediv('StatesDiv');
		hidediv('ZIPDiv');
		showdiv('RegionsDiv');
                /*
		if (document.location.host == "74.40.170.142")
		{
			ajax.requestFile = "/mm3_single/ajax/display_region_list/";
		} else {
			ajax.requestFile = "/mm3/ajax/display_region_list/";
		}
                */
		ajax.requestFile = CI.base_url + "ajax/display_region_list/";
		ajax.onCompletion = createRegionDropDown;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
	
	if (ID > 500){
		//specific region picked out of the search by drop down menu
		hidediv('CitiesDiv');
		hidediv('CountiesDiv');
		hidediv('StatesDiv');
		hidediv('ZIPDiv');
		hidediv('RegionsDiv');
	}
}

function createCountyDropDown()
{
	var obj = document.getElementById("Counties");
	document.getElementById("Counties").options.length = 0;
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}

function createCityDropDown()
{
	var obj = document.getElementById("Cities");
	document.getElementById("Cities").options.length = 0;
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}

function createRegionDropDown()
{
	var obj = document.getElementById("Regions");
	document.getElementById("Regions").options.length = 0;
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}

function getCityList(sel)
{
	var ID = sel.options[sel.selectedIndex].value;
	document.getElementById("FK_MailCityId").options.length = 0;	// Empty city select box
        if(ID==0)
            document.getElementById("FK_MailCityId").options[0]=new Option("Select State First", "0", true, false);

	if(ID != 0){
                /*
                if (document.location.host == "74.40.170.142")
                {
                    aajax.requestFile = "/mm3_single/ajax/get_city_list/"+ID;	// Specifying which file to get
                } else {
                    ajax.requestFile = "/mm3/ajax/get_city_list/"+ID;	// Specifying which file to get
                }
                */
		ajax.requestFile = CI.base_url + "ajax/get_city_list/"+ID;
		ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createCities()
{
	var obj = document.getElementById("FK_MailCityId");
	eval(ajax.response);	// Executing the response from Ajax as Javascript code
}
