var obj;
var field_0;
var field_1;
var field_2;
var field_loading;
var this_host;
var this_fk_value;
var this_form;
var doPopulateCity;
var doPopulateState;

var arrResultState;
var arrResultCity;
var autoPopulate = false;


function fillSelect(host, obj, fk_value, form) {
	this.obj = obj;
	this.field_0 = form.country_id;
	this.field_1 = form.state_id;
	this.field_2 = form.region_id;	
	resetSelect(obj);

	this_host = host;
	this_fk_value = fk_value;
	this_form = form;
	
	if(obj.name == 'comp_state_id')
	{
		this.field_0 = form.comp_country_id;
		this.field_1 = form.comp_state_id;
		this.field_2 = form.comp_region_id;	
				
		//Company country has changed now.
		objCur = document.getElementById('comp_country_id');
		changedCountry = objCur.options[objCur.selectedIndex].text;
		if(document.getElementById('phone_validation_type_contact_company')) {
			objCompanyPhoneValidator = document.getElementById('phone_validation_type_contact_company');
		}
		else {
			objCompanyPhoneValidator = document.getElementById('phone_validation_type_contact');
		}
			
		
		if((changedCountry=='United States' || changedCountry=='Canada')){
			if(objCompanyPhoneValidator){
				objCompanyPhoneValidator.value='usValidation';

				document.getElementById('cotactPhoneLabel_company').innerHTML = '(Format: xxx-xxx-xxxx/ x-xxx-xxx-xxxx)';
				document.getElementById('cotactPhoneLabel_company2').innerHTML = '(Format: xxx-xxx-xxxx/ x-xxx-xxx-xxxx)';
			}
		}else{
			if(objCompanyPhoneValidator){
				objCompanyPhoneValidator.value='international';
				document.getElementById('cotactPhoneLabel_company').innerHTML = '(Numerals and hyphens only. Length must be 7-20 characters.)';
				document.getElementById('cotactPhoneLabel_company2').innerHTML = '(Numerals and hyphens only. Length must be 7-20 characters.)';
			}
		}


		
		if(autoPopulate){
			document.getElementById('country_id').selectedIndex = document.getElementById('comp_country_id').selectedIndex;
			
			objDest = document.getElementById('state_id');
			resetSelect(objDest);

			objDest = document.getElementById('region_id');
			resetSelect(objDest);

			document.getElementById('comp_city').value='';
			document.getElementById('city').value='';
		}
		
	}
	// check if object exisits.
	if (typeof obj != 'object') return false;

	if (fk_value > 0) {

		if(obj.name == 'comp_state_id'){
			form.comp_country_id.disabled=true;
			form.comp_state_id.disabled=true;
			form.comp_region_id.disabled=true;
		}
	}

	if (obj.name == "state_id") {

		//Contact country has changed now.
		objCur = document.getElementById('country_id');
		changedCountry = objCur.options[objCur.selectedIndex].text;
		objContactPhoneValidator = document.getElementById('phone_validation_type_contact');
		if((changedCountry=='United States' || changedCountry=='Canada')){
			if(objContactPhoneValidator){
				objContactPhoneValidator.value='usValidation';
				if(document.getElementById('cotactPhoneLabel')) {
					document.getElementById('cotactPhoneLabel').innerHTML = '(Format: xxx-xxx-xxxx/ x-xxx-xxx-xxxx)';				
					document.getElementById('cotactPhoneLabel2').innerHTML = '(Format: xxx-xxx-xxxx/ x-xxx-xxx-xxxx)';				
				}
			}
		}else{
			if(objContactPhoneValidator){
				objContactPhoneValidator.value='international';
				if(document.getElementById('cotactPhoneLabel')) {
					document.getElementById('cotactPhoneLabel').innerHTML = '(Numerals and hyphens only. Length must be 7-20 characters.)';				
					document.getElementById('cotactPhoneLabel2').innerHTML = '(Numerals and hyphens only. Length must be 7-20 characters.)';				
				}
			}
		}


		resetSelect(form.region_id);
		if (fk_value > 0) {
			form.state_id.options[1] = new Option("Loading...", -1);
			form.state_id.options[1].selected=true;
			this.field_loading = form.state_id;
		}
		url = host + '/location.php?country_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "comp_state_id") {
		resetSelect(form.comp_region_id);
		if (fk_value > 0) {
			form.comp_state_id.options[1] = new Option("Loading...", -1);
			form.comp_state_id.options[1].selected=true;
			this.field_loading = form.comp_state_id;
		}
		url = host + '/location.php?country_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "region_id") {		

		if (fk_value > 0) {
			form.region_id.options[1] = new Option("Loading...", -1);
			form.region_id.options[1].selected=true;
			this.field_loading = form.region_id;
		}
		url = host + '/location.php?state_id=' + fk_value;

		if (fk_value > 0) loadResult(url, '');
	}
	
	if (obj.name == "comp_region_id") {
		
		this.field_0 = form.comp_country_id;
		this.field_1 = form.comp_state_id;
		this.field_2 = form.comp_region_id;

		//Change the selected state on the contact also
		if(autoPopulate){			
			document.getElementById('state_id').selectedIndex = document.getElementById('comp_state_id').selectedIndex;			

			objDest = document.getElementById('region_id');
			resetSelect(objDest);

			document.getElementById('comp_city').value='';
			document.getElementById('city').value='';
		}

		if (fk_value > 0) {
			form.comp_region_id.options[1] = new Option("Loading...", -1);
			form.comp_region_id.options[1].selected=true;
			this.field_loading = form.comp_region_id;
		}
		url = host + '/location.php?state_id=' + fk_value;
		
		if (fk_value > 0) loadResult(url, '');
	}

}

function fillSelectSearchPage(host, obj, fk_value, form) {
	this.obj = obj;
	this.field_0 = form.search_country;
	this.field_1 = form.search_state;
	this.field_2 = form.search_region;

	resetSelect(obj);

	// check if object exisits.
	if (typeof obj != 'object') return false;

	if (fk_value > 0) {
		form.search_country.disabled=true;
		form.search_state.disabled=true;
		form.search_region.disabled=true;
	}

	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		if (fk_value > 0) {
			form.search_state.options[1] = new Option("Loading...", -1);
			form.search_state.options[1].selected=true;
			this.field_loading = form.search_state;
		}
		url = host + '/location.php?country_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "search_region") {
		if (fk_value > 0) {
			form.search_region.options[1] = new Option("Loading...", -1);
			form.search_region.options[1].selected=true;
			this.field_loading = form.search_region;
		}
		url = host + '/location.php?state_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}

}

function resetSelect(obj) {
	while (obj.options.length>1) {
		deleteIndex=obj.options.length-1;
		obj.options[deleteIndex]=null;
	}
}

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if(req.overrideMimeType){
			req.overrideMimeType('text/xml');
		}
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here
			var response;
			response  = req.responseXML;
			if (response) {
				var result = new Array();
				for (i=0; i < response.getElementsByTagName('id').length; i++) {
					
					if(obj.name == 'comp_state_id')
					{
						response.getElementsByTagName('obj_name')[i].firstChild.data='comp_state_id';
					}
					if(obj.name == 'comp_region_id')
					{
						response.getElementsByTagName('obj_name')[i].firstChild.data='comp_region_id';
					}
					result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data, 'obj_name': response.getElementsByTagName('obj_name')[i].firstChild.data};
				}

				if(obj.name == 'comp_state_id'){
					doPopulateState = true;
				}

				if(obj.name=='comp_region_id'){
					doPopulateCity = true;
				}

				loadResult('',result);
				unlockLocation();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function loadResult(url, result) {

	if (result != '') {
		// Response mode
		for (i=0; i < result.length; i++) {
			if(field_loading.name=='region_id' || field_loading.name=='comp_region_id'){
				combo_value = result[i].name;
			}else{
				combo_value = result[i].id;
			}
			document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(result[i].name, combo_value);
		}

		if(doPopulateState){
			doPopulateState = false;	
			arrResultState = result;			
			
			
			if(autoPopulate){
				obj_name = 'state_id';
				arrLen = arrResultState.length;
				objDest = document.getElementById('state_id');
				resetSelect(objDest);
				
				
				for (i=0; i < arrLen; i++) {		
					combo_value = arrResultState[i].id;				
					objDest.options[objDest.options.length] = new Option(arrResultState[i].name, combo_value);
				}
					
			}
		}

		if(doPopulateCity){
			doPopulateCity = false;
			arrResultCity = result;		
			
			if(autoPopulate){
				obj_name = 'region_id';
				arrLen = arrResultCity.length;
				objDest = document.getElementById('region_id');
				resetSelect(objDest);
				
					
				for (i=0; i < arrLen; i++) {		
					combo_value = arrResultCity[i].name;				
					objDest.options[objDest.options.length] = new Option(arrResultCity[i].name, combo_value);
				}
				
			}
		}

	} else if (url != '') {		
		// Input mode

		//Anto: url may not be look only for cached pages, since users also updating state and city values
		randVal=Math.random();
		url += '&randVal=' + randVal;
		return (loadXMLDoc(url));
	}
}

function unlockLocation() {	
	if (this.field_loading) {
		this.field_loading.options[1]=null;
		this.field_loading.options[0].selected=true;
	}
	this.field_0.disabled=false;
	this.field_1.disabled=false;
	this.field_2.disabled=false;
	
	if(document.createCompanyProfile) {
		if(document.createCompanyProfile.comp_state_id) {
			document.createCompanyProfile.comp_state_id.value = document.getElementById('state_id').value;
		}
	}
}