
// Submit Form...
function submit_(form,type) {

	if (type == 'save') {
		document.forms[form].menu_action_click.value='save';
	} else if (type == 'save_goback') {
		document.forms[form].menu_action_click.value='save_goback';
	} else if (type == 'delete') {
		document.forms[form].menu_action_click.value='delete';
	} else if (type == 'cancel') {
		document.forms[form].menu_action_click.value='cancel';
	} else if (type == 'new') {
		document.forms[form].menu_action_click.value='new';
	} else if (type == 'done') {
		document.forms[form].menu_action_click.value='done';
	} else if (type == 'redir') {
		document.forms[form].menu_action_click.value='redir';
	} else if (type == 'sort') {
		document.forms[form].menu_action_click.value='sort';
	} else if (type == 'basket') {
		document.forms[form].menu_action_click.value='basket';
	} else if (type == 'opc_atualizar') {
		document.forms[form].menu_action_click.value='opc_atualizar';
	} else if (type == 'btn2_go') {
		document.forms[form].menu_action_click.value='btn2_go';
	} else if (type == 'btn3_go') {
		document.forms[form].menu_action_click.value='btn3_go';
	} else if (type == 'btn4_go') {
		document.forms[form].menu_action_click.value='btn4_go';
	}

	document.forms[form].submit();

}

/***********************************************
* Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var isNav4, isNav, isIE;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
	isNav = (navigator.appName=="Netscape") ? true : false;
	isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
}
if (navigator.appName=="Netscape") {
	isNav4 = (parseInt(navigator.appVersion.charAt(0))==4);
}

function toprint() {
	parent.print();
}

function selectmenu() {
document.location = document.form_selectmenu.category_name.options[document.form_selectmenu.category_name.selectedIndex].value
}

function PararTAB(quem) {
   VerifiqueTAB=false;
}

function ChecarTAB() {
   VerifiqueTAB=true;
}

VerifiqueTAB=true;

function Mostra(quem, tammax) {
	if ( (quem.value.length == tammax) && (VerifiqueTAB) ) {
		var i=0,j=0, indice=-1;
		for (i=0; i<document.forms.length; i++) {
			for (j=0; j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].name == quem.name) {
					indice=i;
					break;
				}
			}

			if (indice != -1)
		         break;
		}

		for (i=0; i<=document.forms[indice].elements.length; i++) {
			if (document.forms[indice].elements[i].name == quem.name) {
				while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
						(i < document.forms[indice].elements.length) ) {
							i++;
				}

				document.forms[indice].elements[(i+1)].focus();
				VerifiqueTAB=false;
				break;
			}
		}
	}
}

function date_format_mask(src, mask) {
	var i = src.value.length;
  	var output = mask.substring(0,1);
  	var text = mask.substring(i)

	if (text.substring(0,1) != output) {
        src.value += text.substring(0,1);
  	}
}

// Bloquear caracteres nao numericos - onKeyPress=onlynum()
function onlynum(e) {
	var keyNumber = (isIE) ? event.keyCode : e.which;
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		if (isIE) event.keyCode=0;
		return false;
	}
}
/* Function Onlynum to Firefox...
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (((keyCode<48)||(keyCode>57)) && (keyCode!=13) && (keyCode!="0") && (keyCode!=8)) {
	if (document.getElementById("cep").value.length > 0) {
		document.getElementById("cep").value = '';
		alert('Digite apenas números!');
		return false;
	}
}
*/

// E-mail Exist?
function emailExist() {
    emailUrl = "action.php?send=personal_email_exist&email=";
	var emailValue = String(document.getElementById("sce_personal_email").value);
    top.frames[0].location = emailUrl + escape(emailValue);
}

// Name Exist?
function nameExist() {
    nameUrl = "action.php?send=personal_name_exist&name=";
	var nameValue = String(document.getElementById("sce_personal_name").value);
    top.frames[0].location = nameUrl + escape(nameValue);
}

var req;

function loadXMLDoc(url,valor)
{
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url+'?estado='+valor, true);
        req.send(null);

    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url+'?estado='+valor, true);
            req.send();
        }
    }
}

var req2;

function loadXMLDocBairro(url,valor)
{
	req2 = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
		req2 = new XMLHttpRequest();
        req2.onreadystatechange = processReqChange2;
        req2.open("GET", url+'?cidade='+valor, true);
        req2.send(null);

    // Procura por uma versao ActiveX (IE)
    } else if (window.ActiveXObject) {
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req2) {
            req2.onreadystatechange = processReqChange2;
            req2.open("GET", url+'?cidade='+valor, true);
            req2.send();
        }
    }
}

function processReqChange()
{
    // apenas quando o estado for "completado"
    if (req.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req.status == 200) {
            // procura pela div id="atualiza" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById('cidades').innerHTML = req.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
        }
    }
}

function processReqChange2()
{
    // apenas quando o estado for "completado"
    if (req2.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req2.status == 200) {
            // procura pela div id="atualiza" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById('bairros').innerHTML = req2.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req2.statusText);
        }
    }
}

function Atualiza(valor)
{
    loadXMLDoc("cidades.php",valor);
	loadXMLDocBairro("bairros.php",valor);
}

function AtualizaBairro(valor)
{
	loadXMLDocBairro("bairros.php",valor);
}

// getCEP
var url = "getCep.php?param="; // The server-side script
function handleHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
      // Use the XML DOM to unpack the city and state data
      var xmlDocument = http.responseXML;
	  var addresscategory_idfk = xmlDocument.getElementsByTagName('addresscategory_idfk').item(0).firstChild.data;
	  var personal_address = xmlDocument.getElementsByTagName('personal_address').item(0).firstChild.data;
	  var burgh_idfk = xmlDocument.getElementsByTagName('burgh_idfk').item(0).firstChild.data;
      var city_idfk = xmlDocument.getElementsByTagName('city_idfk').item(0).firstChild.data;
      var state_initials = xmlDocument.getElementsByTagName('state_initials').item(0).firstChild.data;
	  document.getElementById('addresscategory_idfk').value = addresscategory_idfk;
	  document.getElementById('personal_address').value = personal_address;
	  document.getElementById('burgh_idfk').value = burgh_idfk;
      document.getElementById('city_idfk').value = city_idfk;

	  // State
	  var ic = 0;
	  var total_ic = document.getElementById('state_initials').length + 1;
	  while (ic < total_ic) {
	  	if (document.getElementById('state_initials').options[ic].value == state_initials) {
			document.getElementById('state_initials').options[ic].selected = true;
			loadXML_City("loadxml_city.php",state_initials);
			loadXML_Burgh("loadxml_burgh.php",city_idfk);
			break;
		}
	  ic++;
	  }

	  // AddressCateory
	  var ic2 = 0;
	  var total_ic2 = document.getElementById('addresscategory_idfk').length + 1;
	  while (ic2 < total_ic2) {
	  	if (document.getElementById('addresscategory_idfk').options[ic2].value == addresscategory_idfk) {
			document.getElementById('addresscategory_idfk').options[ic2].selected = true;
			break;
		}
	  ic2++;
	  }

      isWorking = false;
    }
	else {
	  document.getElementById('addresscategory_idfk').value = "";
	  document.getElementById('personal_address').value = "";
	  document.getElementById('burgh_idfk').value = "";
      document.getElementById('city_idfk').value = "";
      document.getElementById('state_initials').value = "";
	}
  }
}

function getHTTPObject() {
    if(window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    } else
    if(window.ActiveXObject)
    {
        var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];

        for(var i = 0; i < prefixes.length; i++)
        {
            try
            {
                return new ActiveXObject(prefixes[i] + ".XMLHTTP");
            } catch (e) {}
        }
    }
}

var http = getHTTPObject(); // We create the HTTP Object
var isWorking = false;

function updateCep() {
    var cepValue = String(document.getElementById("cep").value);
    http.open("GET", url + escape(cepValue), true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}

function auto_cep () {
	if (document.getElementById("cep").value.length == 8) {

		var str = document.getElementById("cep").value;
	    var re = /^[-]?\d*\.?\d*$/;
	    str = str.toString();
	    if (!str.match(re)) {
	        alert("Digite apenas números!");
	        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
	        return false;
	    }

	    updateCep();
	    return true;


	}
}

// City
var reqCity;
function loadXML_City(url,value) {

	if (http.readyState == 4) {
    	if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			var city_idfk = xmlDocument.getElementsByTagName('city_idfk').item(0).firstChild.data;
		}
	}

    reqCity = null;
    if (window.XMLHttpRequest) {
        reqCity = new XMLHttpRequest();
        reqCity.onreadystatechange = processReqChangeCity;
        reqCity.open("GET", url+'?uf='+value+'&city='+city_idfk, true);
        reqCity.send(null);

    } else if (window.ActiveXObject) {
        reqCity = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqCity) {
            reqCity.onreadystatechange = processReqChangeCity;
            reqCity.open("GET", url+'?uf='+value+'&city='+city_idfk, true);
            reqCity.send();
        }
    }
}

function processReqChangeCity() {
    if (reqCity.readyState == 4) {
        if (reqCity.status == 200) {
            document.getElementById('city').innerHTML = reqCity.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + reqCity.statusText);
        }
    }
}

function UpdateCity(value) {
    loadXML_City("loadxml_city.php",value);
}

function UpdateOnChange_City(value) {
    loadXML_City("loadxml_city.php",value);
	UpdateBurgh(0);
}

// Burgh
var reqBurgh;
function loadXML_Burgh(url,value) {

	if (http.readyState == 4) {
    	if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			var burgh_idfk = xmlDocument.getElementsByTagName('burgh_idfk').item(0).firstChild.data;
			var city_idfk = xmlDocument.getElementsByTagName('city_idfk').item(0).firstChild.data;
		}
	}

    reqBurgh = null;
    if (window.XMLHttpRequest) {
        reqBurgh = new XMLHttpRequest();
        reqBurgh.onreadystatechange = processReqChangeBurgh;
        reqBurgh.open("GET", url+'?city='+value+'&burgh='+burgh_idfk, true);
        reqBurgh.send(null);

    } else if (window.ActiveXObject) {
        reqBurgh = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqBurgh) {
            reqBurgh.onreadystatechange = processReqChangeBurgh;
            reqBurgh.open("GET", url+'?city='+value+'&burgh='+burgh_idfk, true);
            reqBurgh.send();
        }
    }

}

function processReqChangeBurgh() {
    if (reqBurgh.readyState == 4) {
        if (reqBurgh.status == 200) {
            document.getElementById('burgh').innerHTML = reqBurgh.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + reqBurgh.statusText);
        }
    }
}

function UpdateBurgh(value) {
    loadXML_Burgh("loadxml_burgh.php",value);
}
