var statecode="";
function bindCity() { 
	
	//disable child select list
	$("#city").attr("disabled", true); 
	$("#disponivel").attr("disabled", true);
	//clear error message if it exists
	clearMessage("#city");
	clearMessage("#disponivel");  
	   
	//check selection
	if(checkOption("#state")) {
		//clear child select list's options
		$("#city").html('');
		
		//querystring value is selected value of parent drop down list
		var qs = $("#state").val();
		//show message indicating we're getting new values
		$("#city").append(new Option('Listando...'));
		//declare options array and populate
		var cityOptions = new Array();
		$.get("citylist.php?statecode=" + qs, function(data) {
				eval(data);
				if(cityOptions.length > 0) {
					addOptions(cityOptions);
				}
			}
		);
	}
	else {
		$("#city").val() = '';
	}  
	
   

}   


function bindCity2() { 
	 
if(checkOption2("#city")) {
   //clear child select list's options
	    $("#disponivel").html('');

		//querystring value is selected value of parent drop down list
		var qs2 = $("#city").val();
		var qs = $("#state").val();   
		
		//show message indicating we're getting new values
	   $("#disponivel").append(new Option('Listando ...'));
	   //declare options array and populate
	   var disponivelOptions = new Array();
	   $.get("citylist.php?getDisponibilidade=1&statecode=" + qs + "&statecode2=" + qs2 , function(data) {
	   eval(data);
	   if(disponivelOptions.length > 0) {
	   addOptions2(disponivelOptions);
	   }
	  }
	 );
	}
	else {
	$("#disponivel").val() = '';
	} 
	
}


function addOptions(cl) {
	//enable child select and clear current child options
	$("#city").removeAttr("disabled");
    $("#city").html('');
	//repopulate child list with array from helper page
	var city = document.getElementById('city');  
	for(var i = 0; i < cl.length; i++) {
		city.options[i] = new Option(cl[i].text, cl[i].value); 
	}
}

function addOptions2(cl2) {
		//enable child select and clear current child options
		
		$("#disponivel").removeAttr("disabled");
	    $("#disponivel").html('');
		//repopulate child list with array from helper page
		
		var disponivel = document.getElementById('disponivel');   
		for(var i = 0; i < cl2.length; i++) {
			
		    disponivel.options[i] = new Option(cl2[i].text, cl2[i].value); 
		}
}


			
function checkOption(s) {
	clearMessage(s);
	//invalid choice
	if($(s).val() == "") {
		//place error message, colorize select list, disable submit button
		$(s).css("background-color", "pink");
		$(s + "msg").html("&laquo; Selecione");
		$("#submit").attr("disabled", true);
		return false;
	}
	//element OK, enable submit button
	$("#submit").removeAttr("disabled");
	return true;
}

function checkOption2(s) {
		clearMessage(s);
		//invalid choice
		if($(s).val() == "") {
			//place error message, colorize select list, disable submit button
			$(s).css("background-color", "pink");
			$(s + "msg").html("&laquo; Selecione");
			$("#submit").attr("disabled", true);
			return false;
		}
		//element OK, enable submit button
		$("#submit").removeAttr("disabled");
		return true;
}

function clearMessage(s) {
	//clear status message for element
	$(s + "msg").html('');
	$(s).css("background-color", "white");
}

function checkForm() {
	//check both select lists for valid values
	var ok1 = checkOption('#state');
	var ok2 = checkOption('#city');
	var ok3 = checkOption('#disponivel'); 
	if(ok1 && ok2 && ok3) {
		return true;
	}
	else {
		//bad option in one or both lists, disable submit button
		$("#submit").attr("disabled", true);
		return false;
	}
}