/*
function $(objID){
	return document.getElementById(objID);
}

function $$(objID){
	return document.getElementById(objID).style;
}
*/

function ajaxPostFinal(url, strParameters, div, form, formObserver, showWaiting){ 
    var met = 'get' 
    var params = ''; 

    if(form!=''){ 
        met = 'post' 
        params = $(form).serialize(); 
	} 

	if (strParameters!='') {
		params+=((params!='') ? '&' : '') + strParameters;
	}
	
	if (showWaiting) { // && formObserver==''
		$(div).innerHTML=waitMessage;	
	}

    ajaxReq = new Ajax.Request( url , { 
        method: met, 
        parameters: params, 
        evalScripts: true, 		
        onComplete: function(response) { 			
            if(response.status == 200){			
				/*if(formObserver=='') {*/
					Element.hide(div);
				/*}*/
                $(div).update(response.responseText); 
				if(formObserver=='') {
					new Effect.Appear(div);
				} else {
					Element.show(div);
					Xaprb.InputMask.setupElementMasks();
					eval('objSubmitButton = $(formObserver).' + $(formObserver).id + '_Submit')
					$(objSubmitButton.id).observe('click', formSubmit)
					if ($(formObserver)) {
						Form.focusFirstElement(formObserver);
					}
				}								
            } else { 
                $(div).update(request.responseText); 
            } 						
        }, 
		onFailure:function(request){
			$(div).update(request.responseText);
		}
    }); 
}

function ajaxPostDataGrid(url, strParameters, div, dataGridID) {
    var met = 'post' 
    var params = ''; 

	if (strParameters!='') {
		params+=((params!='') ? '&' : '') + strParameters;
	}
	
	$(div).innerHTML=waitMessage;	
	
    ajaxReq = new Ajax.Request( url , { 
        method: met, 
        parameters: params, 
        evalScripts: true, 		
        onComplete: function(response) { 			
            if(response.status == 200){ 				
				if ($('appSubmiter')) {
					$('appSubmiter').src = '../inc/blank.htm';
				} else {
					if (parent.$('appSubmiter')) {
						parent.$('appSubmiter').src = '../inc/blank.htm';
					}
				}
				Element.hide(div);
                $(div).update(response.responseText); 
				new Effect.Appear(div, {
					afterFinish: function() {
						eval(dataGridID + '_oncolor()');
					}});
            } else { 
                $(div).update(request.responseText); 
            } 						
        }, 
		onFailure:function(request){
			$(div).update(request.responseText);
		}
    }); 
}

function formSubmit(event){
	event.stop();
	if (Event.element(event).form.jsValidatorLoad.value=='0') {
		var jsValidator = document.createElement("script");
		document.getElementsByTagName('head')[0].appendChild(jsValidator);
		jsValidator.text=Event.element(event).form.jsValidator.value;
		Event.element(event).form.jsValidatorLoad.value='1';
	}
	var blnFormResult = false;
	eval('blnFormResult = ValidaForm_' + Event.element(event).form.id + '(Event.element(event).form)');
	if (blnFormResult) {
		Event.element(event).form.target = 'appSubmiter';
		Event.element(event).form.submit();
	}
}

function ajaxPost(url, strParameters, div, form){ 
	ajaxPostFinal(url, strParameters, div, form, '', true);
} 

function ajaxGetForm(url, strParameters, div, form){ 
	ajaxPostFinal(url, strParameters, div, '', form, true);
} 
/*
Ajax.Responders.register({ 
    onCreate: function() { 
        $(ajaxLoad).show(); 
    }, 
    onComplete: function() { 
        if(Ajax.activeRequestCount < 1){ 
            $(ajaxLoad).hide(); 
        } 
    } 
}); 
*/
/*
function ajaxPost(strUrl, strParameters, strDiv) {
	var eDate = new Date();
	$(strDiv).innerHTML=waitMessage;	
	var index = MakeNewAJAXCall(strUrl + '?' + strParameters, SimpleCallback, 'POST', ComplexCallback, strDiv, eDate.getTime(), TimeoutCallback, 10);
}
*/

function ajaxTutorial(id) {
		WindowPopup('../tutorial/index.asp?id=' + id, 'Tutorial', 840, 600);
	/*
	Lightview.show({ title: 'SPCVB - Tutorial', href: '../tutorial/index.asp?id=' + id, rel: 'iframe', options: { width: 840, height: 600 }});
	*/
}

function ajaxModalPostForm(strTitle, strUrl, strParameters, strWidth, strHeight, objForm) {
	objForm.target = 'modal';
	objForm.action = strUrl
	ajaxModal(strTitle, 'about.blank', strParameters, strWidth, strHeight);
	objForm.submit(); 
}

function ajaxModal(strTitle, strUrl, strParameters, strWidth, strHeight) {
	WindowPopup(strUrl + '?' + strParameters, 'modal', strWidth, strHeight);
	/*
	Lightview.show({
		href: strUrl,
		rel: 'ajax',
		title: 'Ultrafitness - ' + strTitle,
		options: {
			width: strWidth, 
			height: strHeight, 
			ajax: {
				parameters: strParameters, 
				onComplete: function(){
					// once the request is complete we observe the form for a sumbit
					//$('ajaxForm').observe('submit', submitAjaxForm);
				} 
			}
		}
	});
	*/
}

function submitAjaxForm(event) {
  // block default form submit
  event.stop();
	  
  Lightview.show({
    href: '/projects/lightview/ajax_form.php',
    rel: 'ajax',
    options: {
      title: 'results',
	  menubar: false,
	  topclose: true,
	  autosize: true,
	  scrolling: auto, 
      ajax: {
        parameters: Form.serialize('ajaxForm') // the parameters from the form
      }
    }
  });
  return false;
}

/*****************************************************************
	This is the "simple" callback that is made at the completion of
	the HTTPRequest.
	
	Parameters:
		strResponse:		The request result text.
		strDiv:	This is the first of the two "extra" parameters
						that were supplied when the call was made.
		dtStart:	This is the second of the two parameters.
*/
function SimpleCallback(strResponse, strDiv, dtStart){
	$(strDiv).innerHTML=strResponse;
	Effect.Appear(strDiv);
	//alert($('modalPage').offsetWidth);
	//alert($('modalPage').offsetHeight);
	if (strDiv == 'modalBody') {

	}
	
	
	/*
	var	eDate = new Date();
	var time_spent = parseInt ((eDate.getTime() - dtStart) / 100) / 10;
	var log_string = "<strong>Simple Callback</strong><br />\n&nbsp;&nbsp;&nbsp;Response: '"+strResponse+"'<br />\n&nbsp;&nbsp;&nbsp;Param: '"+strDiv+"'<br />\n&nbsp;&nbsp;&nbsp;This request took "+time_spent+" seconds to complete.";
	MakeLogEntry(strDiv, log_string);
	*/
}

function getPosicaoElemento(elemID){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/*****************************************************************
	This is the "complex" callback. It is called for every stage
	of the request.
	
	For Stage 4 (complete), this callback is called prior to the
	"simple" callback.
	
	Parameters:
		in_stage:	This is the request stage (0-4)
		strResponse:		The request result text.
		strDiv:	This is the first of the two "extra" parameters
						that were supplied when the call was made.
		dtStart:	This is the second of the two parameters.
		intID:	This is the index of the request. This can be
						used to directly index the request array.
*/
function ComplexCallback(intStage, strResponse, strDiv, dtStart, intID){
/*
	var	eDate = new Date();
	var time_spent = parseInt ((eDate.getTime() - dtStart) / 100) / 10;
	var log_string = "<strong>Complex Callback</strong><br />\n&nbsp;&nbsp;&nbsp;Index: "+intID+"<br />\n&nbsp;&nbsp;&nbsp;Stage: "+in_stage+"<br />\n&nbsp;&nbsp;&nbsp;Response: '"+strResponse+"'<br />\n&nbsp;&nbsp;&nbsp;Param: '"+strDiv+"'<br />\n&nbsp;&nbsp;&nbsp;This request took "+time_spent+" seconds to get to this point.";
	MakeLogEntry(strDiv, log_string);
*/
}

/*****************************************************************
	This is the "timeout" callback.
	
	Parameters:
		strDiv:	This is the first of the two "extra" parameters
						that were supplied when the call was made.
		dtStart:	This is the second of the two parameters.
		intID:	This is the index of the request. This can be
						used to directly index the request array.
*/
function TimeoutCallback(strDiv, dtStart, intID){
/*
	SetPhase4 ( strDiv );
	var log_string = "<strong>Timeout Callback</strong><br />\n&nbsp;&nbsp;&nbsp;Index: "+intID+"<br />\n";
	//MakeLogEntry(strDiv, log_string);
*/
	alert ( "Excedido o tempo de resposta! ("+intID+")" );
	$(strDiv).innerHTML='';
}

/*****************************************************************
	This simply displays the given text in the log. It checks to
	see if there is supposed to be a displayed log. If not, then
	nothing happens. If this is the first entry, then a header is
	displayed.
	
	Parameters:
		strResponse:	The text to be displayed (raw HTML)
*/
/*
function MakeLogEntry ( strDiv, strResponse ){
	var old_val = $(objID).innerHTML;
	var newval;
	if (!old_val){
		newval = '<hr /><h3 style="margin:0;padding:0">Event Log</h3>'+"\n"+strResponse;
	} else {
		newval = old_val+"<br />\n"+strResponse;
	}
	$(objID).style.display = 'none'; 
	$(objID).innerHTML = newval;
	$(objID).style.display = 'block'; 
}
*/

var waitMessage = '';
waitMessage += '<div align=\"center\"><img src=\"../../include/loading.gif\"></div>';