$(document).ready(function() {

	  $(document).pngFix(); 

});

var ajaxObjects = new Array();

function showStates(value,path){
	var serverSideFile = path + "/include/request.php";
	var ajaxIndex = ajaxObjects.length;
	var country = document.getElementById(value);

	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile;
	ajaxObjects[ajaxIndex].setVar('item',country.options[country.selectedIndex].value);
	ajaxObjects[ajaxIndex].setVar('command',"state");
	ajaxObjects[ajaxIndex].onCompletion = function(){ showStateResults(value,ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();	
}

function showStateResults(value,ajaxIndex){ 
	
	var request = ajaxObjects[ajaxIndex].response.split("###");
	var objCont = eval("document.getElementById('" + value.replace("country","state") + "_container');");
	var objState = eval("document.getElementById('" + value.replace("country","state") + "');");
	
	if(request != 0){
		
		if(objState.tagName == "INPUT"){

			var objState = document.createElement("select");
			objState.name = value.replace("country","state");
			objState.id = objState.name;
			objState.className = "regRegInput";
			
			objCont.innerHTML = "";
			objCont.appendChild(objState);	
			
		}
		
		removeOption(objState);
		if(request.length > 0){
			for(var i = 0; i < request.length; i++){
				var optionsArr = request[i].split("|||");
				objState.options[i] = new Option(optionsArr[1],optionsArr[0]);
			}
		}
	}else{
		
		var textBox = document.createElement("input");
		textBox.type = "text";
		textBox.name = value.replace("country","state");
		textBox.id = textBox.name;
		textBox.className = "regRegInput";
		textBox.disabled = true;
		textBox.style.backgroundColor = "#F0F1EC";
		
		objCont.innerHTML = "";
		objCont.appendChild(textBox);	
	}
	
	ajaxObjects[ajaxIndex] = false;
}

function removeOption(objState)
{
	for (var i = objState.length; i > 0; i--) {
		objState.remove(i);
	}
}

function showDistributors(){
	var countryObj = document.getElementById("d_country");
	var stateObj = document.getElementById("d_state");
	
	var country = (countryObj) ? countryObj.options[countryObj.selectedIndex].value : 0;

	if(stateObj.tagName == "INPUT")
		var state = (stateObj) ? stateObj.value : 0;	
	else
		var state = (stateObj) ? stateObj.options[stateObj.selectedIndex].value : 0;
	
	openPopWindow(country,state);
}

function openPopWindow(country,state)
{
	dhtmlwindow.open("ajaxbox", "ajax", '../include/distributors.php?country=' + country + '&state=' + state, "Distributors", "width=600px,height=430px,center=1,resize=0,scrolling=0");
}
