// Remove o conteúdo de um elemento

function clearBoxValueOnFocus(box, defaultValue) {

	if(document.getElementById(box).value == defaultValue) {

		document.getElementById(box).value = '';

	}

}



// Restaura o conteúdo de um elemento

function restoreBoxValueOnBlur(box, defaultValue) {

	if(document.getElementById(box).value == defaultValue || document.getElementById(box).value == '') {

		document.getElementById(box).value = defaultValue;

	}

}



// Iniciar a interface XMLHttpRequest. Retorna false se não for instanciada.

function createRequest() {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...

		http_request = new XMLHttpRequest();

	} else if (window.ActiveXObject) { // IE

		try {

			http_request = new ActiveXObject("Msxml2.XMLHTTP");

		} catch (e) {

			try {

				http_request = new ActiveXObject("Microsoft.XMLHTTP");

			} catch (e) {

			}

		}

	}

	return http_request;

}



// Recupera informação de um documento ou URL passado como argumento

function makeRequest(baseURL, scriptRelativePath, MSGElement) {

	var ajaxMethod = createRequest();



	if(ajaxMethod == false) {

		alert('Não foi possível iniciar o processo assíncrono de comunicação. Encerrando.');

		return 0;

	}



	ajaxMethod.open(baseURL + scriptRelativePath, true);



	if(ajaxMethod.readyState == 1) {

		document.getElementById(MSGElement).innerHTML = '<img src="' + baseURL + 'modules/mod_bonuscortesias/img/ajax-loader.gif" />Carregando...';

	}



	ajaxMethod.setRequestHeader('Content-Type','text/html; charset=ISO-8859-1');

	ajaxMethod.setRequestHeader('Cache-Control','no-cache, no-store, must-revalidate');



	ajaxMethod.onreadystatechange = contentListener;

	

	ajaxMethod.send(null);

	

	function contentListener() {

		if(ajaxMethod.readyState == 4 && ajaxMethod.status == 200) {

			document.getElementById(MSGElement).innerHTML = ajaxMethod.responseText;

		}

	}

}



// Envia os dados do formulário passado como argumento e retorna o resultado de seu processamento

function loadDataRequested(baseURL, scriptRelativePath, dataForm, MSGElement) {

	var ajaxMethod = createRequest();



	if(ajaxMethod == false) {

		alert('Não foi possível iniciar o processo assíncrono de comunicação. Encerrando.');

		return 0;

	}

	

	ajaxMethod.open('POST',baseURL + scriptRelativePath, true);



	if(ajaxMethod.readyState == 1) {

		document.getElementById(MSGElement).innerHTML = '<img src="' + baseURL + 'modules/mod_bonuscortesias/img/ajax-loader.gif" />Carregando...';

	}



	ajaxMethod.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	ajaxMethod.setRequestHeader('Cache-Control','no-cache, no-store, must-revalidate');



	ajaxMethod.onreadystatechange = contentListener;

	

	var data_form = createPostString(document.getElementById(dataForm));

	ajaxMethod.send(data_form);

	

	function contentListener() {

		if(ajaxMethod.readyState == 4 && ajaxMethod.status == 200) {

			document.getElementById(MSGElement).innerHTML = ajaxMethod.responseText;

		}

	}

}



// Organiza os dados do formulário para serem enviados ao servidor

function createPostString(form) {

	var postString = '';

	var formElements = '';

	formElements = form.getElementsByTagName('input');

	for(i=0;i < formElements.length;i++) {

		if(formElements[i].type == 'radio' && formElements[i].checked == 'checked') {

			postString += formElements[i].name + "=" + formElements[i].value + "&";

			}

		else if (formElements[i].type == 'checkbox' && formElements[i].checked) {

			postString += formElements[i].name + "=" + formElements[i].value + "&";

			}

		else {

			postString += formElements[i].name + "=" + formElements[i].value + "&";		

			}

		}



	formElements = form.getElementsByTagName('select');	

	for(i=0;i < formElements.length;i++) {

		postString += formElements[i].name + "=" + formElements[i].options[formElements[i].selectedIndex].value + "&";

		}



	formElements = form.getElementsByTagName('textarea');	

	for(i=0;i < formElements.length;i++) {

		postString += formElements[i].name + "=" + formElements[i].value + "&";

		}

	postString = url_encode(postString);

	return postString;

}



// Decodifica os dados do formulário para serem enviados ao servidor

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;  

}