$(document).ready(function($)
{
   $("#cep").mask("99999-999");
   $("#telefone").mask("99 9999.9999");
   $('#estado').change(function()
	{
	  listar_cidades( $("#estado").attr("selectedIndex") );
	});
   
   $("#form_contato").validationEngine();
   
   $('#submit').click(function()
	{
	  $("#form_contato").submit();
	});
   
});
function listar_cidades(estado_id)
{
	//$( '#cidade' ).load( 'list_cidades.php?estado='+estado_id );

	$.ajax({
		type: "GET",
		url: '/contato/list_cidades.php',
		data: 'estado='+estado_id,
		success: function(resposta)
		{
			$('#list_cidades').html(resposta);
		}
	});
}
function get(obj, form) 
{
	var FormInput 		= document.forms[form].getElementsByTagName("input");
	var FormSelect		= document.forms[form].getElementsByTagName("select");
	var FormTextArea	= document.forms[form].getElementsByTagName("textarea");

	var getstr = "";
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "text") {
		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";
		}
	}
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "hidden") {
		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";
		}
	}
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "password") {
		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";
		}
	}
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "file") {
		   getstr += FormInput[i].name + "=" + url_encode(FormInput[i].value) + "&";
		}
	}
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "checkbox") {
		   if (FormInput[i].checked) {
			  getstr += FormInput[i].name + "=" + FormInput[i].value + "&";
		   } else {
			  getstr += FormInput[i].name + "=&";
		   }
		}
	}
	for (i=0; i<FormInput.length; i++) {
		if (FormInput[i].type == "radio") {
		   if (FormInput[i].checked) {
			  getstr += FormInput[i].name + "=" + FormInput[i].value + "&";
		   }
		}
	}
	for (i=0; i<FormTextArea.length; i++) {
		if (FormTextArea[i]) {
			   getstr += FormTextArea[i].name + "=" + url_encode(FormTextArea[i].value) + "&";
		}
	}
	for (i=0; i<FormSelect.length; i++) {
		if (FormSelect[i]) {
			var sel = FormSelect[i];
			getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
		}
	}
	
	return getstr;
}
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
// url_encode version 1.0 
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 

    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
}