/*------------------------------------------------------------------------------
 *     PROJECT: IdeiasX (ideiasx.com)
 *        FILE: main_code.js
 *     VERSION: 1.000.000
 *    CONTENTS: Ajax Functions
 *        DATE: 2008-11-10
 *      AUTHOR: Helder Beringuilho (HB)
 *   REVISIONS: 2008-11-10, HB, 1.000.000 - Creation of this module;
 *                                                                (c) IdeiasX
 * ---------------------------------------------------------------------------*/

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
var gHTTP = CreateRequestObject();
var gTimer;

/*------------------------------------------------------------------------------
 *     NAME: CreateRequestObject()
 * ALGORITM:
 *  RETURNS: N/A
 *   INPUTS:
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
function CreateRequestObject()
{
	var wReqObj = null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		wReqObj=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			wReqObj=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			wReqObj=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return wReqObj;

} // CreateRequestObject()

/*------------------------------------------------------------------------------
 *     NAME: SendRequest()
 * ALGORITM:
 *  RETURNS: N/A
 *   INPUTS:
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
function SendRequest(zona, code)
{
	//alert(code);
	gHTTP.open("get", "/cgi-bin/main_vtapa.cgi?action="+zona+"&"+code);
	gHTTP.onreadystatechange = HandleResponse;
	gHTTP.send(null);

} // sndReq()

/*------------------------------------------------------------------------------
 *     NAME: HandleResponse()
 * ALGORITM:
 *  RETURNS: N/A
 *   INPUTS:
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
function HandleResponse()
{

	if ( gHTTP.readyState == 4 )
	{

		var wResponse = gHTTP.responseText;
		var wUpdate = new Array();

		if ( wResponse.indexOf('|' != -1) )
		{
            //alert(wResponse);
			wUpdate = wResponse.split('|');
			document.getElementById(wUpdate[0]).innerHTML = wUpdate[1];
			//document.getElementById('zona').innerHTML = response;
			//gTimer = setTimeout("initContentSub()",0);
			Shadowbox.clearCache(); // <= clear Shadowbox's cache
			Shadowbox.setup(); // <= set up all Shadowbox links
		}

	} // if

} // HandleResponse()

function initContentSub()
{
	clearTimeout(gTimer);
	Shadowbox.init();
	Shadowbox.setup();
}

/*------------------------------------------------------------------------------
 *     NAME: PopInfo()
 * ALGORITM: Opens a popup window
 *  RETURNS: N/A
 *   INPUTS: pFile, pWinW, pWinH
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
var gInfoWindow;
function PopInfo(pFile, pWinW, pWinH)
{
	if (gInfoWindow)
    {
		gInfoWindow.close();
	}

	if (!pWinW) pWinW = screen.width;
	if (!pWinH) pWinH = screen.height;

    /*pWinW = screen.width;
    pWinH = screen.height-50;*/

	var w_win_x = (screen.width - pWinW) / 2;
	var w_win_y = (screen.height - pWinH) / 2;

	gInfoWindow = window.open(pFile, "infoWindow", "width="+pWinW+",height="+pWinH+",top="+w_win_y+",left="+w_win_x+",resize=yes,scrollbars=yes,status=yes");

} // PopInfo()

/*------------------------------------------------------------------------------
 *     NAME: PopInfo()
 * ALGORITM: Opens a popup window
 *  RETURNS: N/A
 *   INPUTS: pFile, pWinW, pWinH
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
function PopInfo2(pFile)
{
    var pWinW = screen.width;
    var pWinH = screen.height-50;

	var w_win_x = 0;//(screen.width - pWinW) / 2;
	var w_win_y = 0;//(screen.height - pWinH) / 2;

	window.open(pFile, "infoWindow", "width="+pWinW+",height="+pWinH+",top="+w_win_y+",left="+w_win_x+",resize=yes,scrollbars=yes,status=yes");

} // PopInfo()

/*------------------------------------------------------------------------------
 *     NAME: SendMail()
 * ALGORITM: Send mail
 *  RETURNS: N/A
 *   INPUTS: pArea
 *  OUTPUTS: N/A
 *----------------------------------------------------------------------------*/
function SendMail(pArea, pHtmlOK, pHtmlError)
{
    var wCode =	"zone="				+ pArea +
				"&op=90&verhtml="	+ pHtmlOK +
				"&errhtml="			+ pHtmlError;

	var wName = document.getElementById('fld_name').value;
	var wEmail = document.getElementById('fld_email').value;
	var wSubject = document.getElementById('fld_subject').value;
	var wMsg = document.getElementById('fld_area').value;

	if ( wName.length > 0 ) wCode += "&fld_name=" + wName;
	if ( wEmail.length > 0 ) wCode += "&fld_email=" + wEmail;
	if ( wSubject.length > 0 ) wCode += "&fld_subject=" + wSubject;
	if ( wMsg.length > 0 ) wCode += "&fld_area=" + wMsg;

	//alert(wCode);
    SendRequest(pArea, wCode);

} // SendMail()
