// JavaScript Document



/*function getState(MA_CountryId)
{
//new Ajax.Updater('stateDiv','cityState.php?MA_CountryId='+MA_CountryId,{asynchronous:true, evalScripts:true});

new Ajax.Request('cityState.php?action=getstate&MA_CountryId='+MA_CountryId, {

  method: 'get',onSuccess: handleOnSuccessState,onInteractive : handleOnInteractive,onLoading: handleOnLoading,onLoaded: handleOnLoaded,onFailure:handleOnFailure,onException:handleOnException
 
});

}

function getCity(MA_StateId)
{

//new Ajax.Updater('stateDiv','cityState.php?MA_CountryId='+MA_CountryId,{asynchronous:true, evalScripts:true});

new Ajax.Request('cityState.php?action=getcity&MA_StateId='+MA_StateId, {

  method: 'get',onSuccess: handleOnSuccessCity,onInteractive : handleOnInteractive,onLoading: handleOnLoading,onLoaded: handleOnLoaded,onFailure:handleOnFailure,onException:handleOnException
 
});

}*/
function handleOnSuccessState(transport)
{
   var result=transport.responseText;
	document.getElementById('stateDiv').innerHTML=result;
	document.getElementById('MA_CityId').value='';
}

function handleOnSuccessCity(transport)
{
   var result=transport.responseText;
	document.getElementById('cityDiv').innerHTML=result;
}

function handleOnLoading(transport)
{
    
}

function handleOnLoaded(transport)
{
  //alert('handleOnLoaded');

}
function handleOnInteractive(transport)
{
 
 //alert('handleOnInteractive');
}

function handleOnFailure(transport)
{
 alert("oops! try later");
 
}
function handleOnException(transport,headerResult)
{
  alert("oops! try later");
}


/* 
Folowing function remove all the options from a select dd
parameter: select object
*/
function RemoveAll(select)
{
	select.options.length = 0;
}

/* 
Following function  find the index  of a option in the select dd with the specified value
parameter1: select object
parameter1: value of the required option
*/

function findIndexOf(select,OptionValue){

var index=-1;
for(i=0; i<select.length; i++)
    {
    if(select.options[i].value==OptionValue)
       {
         index=i;
		 break;
	   }
	}
return index;
}


/* 
Following function  slect an option in the select dd with the specified value
parameter1: select object
parameter1: value of the option to be select
*/

function selectOptionValue(select,OptionValue)
{

  index=findIndexOf(select,OptionValue);
  if(index!=-1)
   {
       select.options[index].selected = true;

   }else{
    //alert('OptionValue='+OptionValue+' Not found');
   }

}

/* 
Following function Add an option In the specified select dd
parameter1: select object
parameter1: option Text 
parameter1: option Value 
parameter1: pos index 
*/

function  addOption(select,OptionText,OptionValue,pos){
	 select.options[pos] = new Option(OptionText, OptionValue);
}


/* 
Following function  Populate the State Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a State ID And Value as sate name
Param1:Country id 
Param1:Name of state Input Tag
Param1:default selected option value 
*/

function getStateDD(MA_CountryId,select_dd,selected_value)
{
var select_id=document.getElementById(select_dd);
new Ajax.Request('cityState.php?action=getstate&MA_CountryId='+MA_CountryId, {

  method: 'get',onSuccess:function (transport){
 var result=transport.responseText;
 pos=0;
 eval(result);
RemoveAll(select_id);
for(i in states) {  
  addOption(select_id,states[i],i,pos++);
}
selectOptionValue(select_id,selected_value);
}
 
});


}


/* 
Following function  Populate the City Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a City ID And Value as City name
Param1:State ID
Param1:Name of City Input Tag
Param1:default selected option value 
*/

function getCityDD(MA_StateId,select_dd,selected_value)
{
  var select_id=document.getElementById(select_dd);

	new Ajax.Request('cityState.php?action=getcity&MA_StateId='+MA_StateId, {
	
	  method: 'get',onSuccess:function (transport){
	//alert(select_dd);
	 var result=transport.responseText;
	 pos=0;
	// alert(result);
	 eval(result);
	RemoveAll(select_id);
	for(i in cities) {  
	  addOption(select_id,cities[i],i,pos++);
	}
	selectOptionValue(select_id,selected_value);
	}
	 
	});

}


/*
Following function  Populate the City Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a City ID And Value as City name
Param1:State ID
Param1:Name of City Input Tag
Param1:default selected option value
*/

function getTotalCityDD(MA_CountryId,select_dd,selected_value)
{
    var select_id=document.getElementById(select_dd);
    new Ajax.Request('cityState.php?action=getTotalCity&MA_CountryId='+MA_CountryId, {

	  method: 'get',onSuccess:function (transport){
	//alert(select_dd);
	 var result=transport.responseText;
	 pos=0;
	// alert(result);
	 eval(result);
	RemoveAll(select_id);
	for(i in cities) {
	  addOption(select_id,cities[i],i,pos++);
	}
	selectOptionValue(select_id,selected_value);
	}

	});

}

/*
Following function  Populate the City Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a City ID And Value as City name
Param1:State ID
Param1:Name of City Input Tag
Param1:default selected option value
*/

function getCityWstate(MA_CountryId,select_dd,selected_value)
{
    var select_id=document.getElementById(select_dd);
    new Ajax.Request('cityState.php?action=getcitywstate&MA_CountryId='+MA_CountryId, {

	 method: 'get',onSuccess:function (transport){
	
	 var result=transport.responseText;
    // alert(result);
     pos=0;
	 var str='<table><tr>';
	
	 eval(result);
	// alert(result);
	//RemoveAll(select_id);
	var counter2    =   1;
	for(i in cities) {
      var val   =   i+"-"+cities[i];
      str = str + "<td><input type='checkbox' name='city_list[]' value='"+val+"'>"+cities[i]+"</td>";
	  //addOption(select_id,cities[i],i,pos++);
	  if(counter2 % 4==0){
        str =   str + "</tr><tr>";
      }
      counter2++;
	}
	str    =  str+ "</tr></table>"
	//selectOptionValue(select_id,selected_value);
	select_id.innerHTML    =   str;
	}

	});
}

/*
Following function  Populate the Sp Branch name select Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id as branch ID And Value as Branch name
Param1:SP ID
Param1:ID of Branch select Tag
Param1:default selected option value
*/

function getspbranch(SP_ID,select_dd,selected_value)
{
    var select_id=document.getElementById(select_dd);
    new Ajax.Request('cityState.php?action=getspbranch&sp_id='+SP_ID, {

	method: 'get',onSuccess:function (transport){
	
	var result=transport.responseText;
	pos=0;
	
	eval(result);
	RemoveAll(select_id);
	for(i in spbranch) {
	  addOption(select_id,spbranch[i],i,pos++);
	}
	selectOptionValue(select_id,selected_value);
	}

	});

}
/* 
Following function  Populate the State Input Box with values returned by Ajax
Depends On: Prototype
Assumes:Ajax Respone must contain an Associative Array with id a State ID And Value as sate name
Param1:Country id 
Param1:Name of state Input Tag
Param1:default selected option value 
*/

function getCompanyServices(companyId,divid)
{

new Ajax.Request('cityState.php?action=companytype&companyId='+companyId, {

	method: 'get',onSuccess:function (transport){
	var result=transport.responseText;
    document.getElementById(divid).innerHTML=result;
	document.getElementById(divid).style.display='inline';
        document.getElementById('btnsubmit').disabled=false;
         document.getElementById('btnreset').disabled=false;
}
 
});
}

function getCompanyType(CountryType,select_dd,selected_value)
{
    
var select_id=document.getElementById(select_dd);
new Ajax.Request('cityState.php?action=supercompanytype&companyId='+CountryType, {

	method: 'get',onSuccess:function (transport){
         var result=transport.responseText;
	pos=0;
        eval(result);
        RemoveAll(select_id);
        for(i in ctype) {
            addOption(select_id,ctype[i],i,pos++);
        }
       selectOptionValue(select_id,selected_value);
}

});
}

function getCompanyFromService(ServiceId,select_dd,selected_value)
{
    
var select_id=document.getElementById(select_dd);

new Ajax.Request('enquiry_list.php?action=ServiceCompany&serviceId='+ServiceId, {

	method: 'get',onSuccess:function (transport){
    var result=transport.responseText;
	RemoveAll(select_id);
	document.getElementById('MA_CompanyId').innerHTML=result;
		

}

});
}
