// JavaScript Document
// JavaScript Document
/* background flicker ie6 hack */
/*@cc_on
   /*@if (@_win32)
   document.execCommand("BackgroundImageCache",false,true); 
   /*@end
@*/

function submitUser (action, silent, redirectUrl) {
	
   // Processar dados : AJAX
   // silent: para processar silenciosamente sem interacção
   if (silent == null) {
     Element.hide('cb_ajaxform'); 
     Element.show('cb_ajaxwait');
   }
	
	var params = Form.serialize('form_register');
	//var action = ($('iduser') != null)? 'update' : 'insert';
	var url  = '/includes/ajax_useraccount.inc.php';
	var pars = 'action='+action+'&'+params;

	//if(action=='update'){if($F('file')!=""){micoxUpload(document.form_register, '/includes/upload/filerecieve.php?id_user='+ $F('iduser'),'recebe_up_3','<img src=\'/images/waitstatus.gif\' id=\'fileuploading\' title=\'Upload in progress...\'>', 'Erro ao carregar');}}
	
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: pars, 
				onSuccess: function (res) {
				    //var response = res.responseText.split('|');
					$('cb_ajaxwait').update( res.responseText );
				 },
				onFailure: reportError
			});
}

function loginUser (button, refreshType) {
	
	if($('loginUsermsg')!=null) $('loginUsermsg').remove()
	new Insertion.After ($(button).id, '<div id="loginUsermsg" style="font-size:10px;"><img src="/images/waitstatus.gif" alt="Please wait..." width="16" height="16" style="vertical-align:middle" id="waitstatus" /></div>')

    var email = $F('login_email')
    var pass  = $F('login_password')

	var url   = '/includes/ajax_useraccount.inc.php';
	var pars  = 'action=validate&email='+email+'&password='+pass+ '&rememberMe=1';
	
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onSuccess: function (res) {
				
				alert ( res.responseText.substr(2).replace(/(<([^>]+)>)/ig,"") );
			  
			if (res.responseText.indexOf('T:') >= 0) { 

				//$('loginUsermsg').update( res.responseText.substr(2) );
				if (refreshType == null) { window.location='/index.php'; } else {  location.reload(true); }
			  
			} else if (res.responseText.indexOf('F:') >= 0) {

				//$('loginUsermsg').update( res.responseText.substr(2) );
			}
			
			$('loginUsermsg').remove();
			  
		},
		onFailure: reportError
		});	
}


function MM_jumpMenu(targ,selObj,restore,url){ //v3.0
  (url)? eval(targ+".location='"+url+selObj.options[selObj.selectedIndex].value+"'") : eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function submeterAmigo () {
	
 // Processar dados : AJAX
 Element.hide('cb_ajaxform'); 
 Element.show('cb_ajaxwait');
 
    var params = Form.serialize('form_contact');
	var url  = '/includes/ajax_sendform.inc.php';
	var pars = 'action=sendfriend&'+params;
		
	var myAjax = new Ajax.Updater(
			{success: 'cb_ajaxwait'},  
			url, 
			{
				method: 'post', 
				parameters: pars,
				onFailure: reportError
			});
	
 return false;
}
function reportError () { alert('Sorry. There was an error.'); }


function submeterComment () {
	
 // Processar dados : AJAX
    (!$('ajaxmsg'))? new Insertion.After ('form_comment', '<div id="ajaxmsg"> <img src="/images/waitstatus.gif" alt="Please wait..." width="16" height="16" style="vertical-align:middle" /> </div>') : '';
   
    Element.hide('form_comment');
 
	var idcontent = $F('idcontent');
	var idvideo = $F('idvideo');
	var title = $F('title');
	var txt = $F('txt');
 
	var url  = '/includes/ajax_comment.inc.php';
	var pars = 'action=add&idcontent='+idcontent+'&idvideo='+idvideo+'&title='+title+'&txt='+txt;
	
//	urchinTracker('/novo_registo_newsletter');
	
	var myAjax = new Ajax.Updater(
			{success: 'ajaxmsg'},  
			url, 
			{
				method: 'post', 
				parameters: pars,
				onFailure: reportError
			});
 
}

function submeterNewsletter () {
	
 // Processar dados : AJAX
   (!$('ajaxmsg'))? new Insertion.After ('form_news', '<div id="ajaxmsg"> <img src="/images/waitstatus.gif" alt="Please wait..." width="16" height="16" style="vertical-align:middle" /> </div>') : '';
   
   Element.hide('form_news');
 
    var idnews    = 1;
	var idcountry = 0;
//  var name      = $F('nome_news');
	var email     = $F('email_news');
 
	var url  = '/includes/ajax_newsletter.inc.php';
	var pars = 'newsletter_action=subscribe&idnews='+idnews+'&name='+name+'&email='+email+'&idcountry='+idcountry;
	
//	urchinTracker('/novo_registo_newsletter');
	
	var myAjax = new Ajax.Updater(
			{success: 'ajaxmsg'},  
			url, 
			{
				method: 'post', 
				parameters: pars,
				onFailure: reportError
			});
 
}


/* popup */
var newWindow = null;
function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
		
	type = type.toLowerCase();
	
	if (type == "fullscreen"){
		strWidth = screen.availWidth;
		strHeight = screen.availHeight;
	}
	var tools="";
	if (type == "standard") tools = "resizable,toolbar=no,location=yes,scrollbars=yes,menubar=no,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console" || type == "fullscreen") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}
function doPopUp(e)
{
//set defaults - if nothing in rel attrib, these will be used
var t = "standard";
var w = "600";
var h = "480";
//look for parameters
attribs = this.rel.split(" ");
if (attribs[1]!=null) {t = attribs[1];}
if (attribs[2]!=null) {w = attribs[2];}
if (attribs[3]!=null) {h = attribs[3];}
//call the popup script
popUpWin(this.href,t,w,h);
//cancel the default link action if pop-up activated
if (window.event) 
	{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
	} 
else if (e) 
	{
	e.stopPropagation();
	e.preventDefault();
	}
}
function findPopUps() {
var popups = document.getElementsByTagName("a");
for (i=0;i<popups.length;i++)
 {
 if (popups[i].rel.indexOf("popup")!=-1) {
  // attach popup behaviour
  popups[i].onclick = doPopUp;
  // add popup indicator

/*
  if (popups[i].rel.indexOf("noicon")==-1)
  //more of the 'no icon' thing in a moment ...
   {
   popups[i].style.backgroundImage = "url(pop-up.gif)";
   popups[i].style.backgroundPosition = "0 center";
   popups[i].style.backgroundRepeat = "no-repeat";
   popups[i].style.paddingLeft = "15px";
   }
*/

// add info to title attribute to alert fact that it's a pop-up window
  popups[i].title = popups[i].title + " [Opens in new window]";
  
 } else if (popups[i].rel.indexOf("external")!=-1) { 
   popups[i].target = '_blank';
   popups[i].title = popups[i].title + " [Opens in new window]";
 }
 }
}

function addEvent(elm, evType, fn, useCapture){if(elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;}else if (elm.attachEvent){var r = elm.attachEvent('on' + evType, fn);return r;}else{elm['on' + evType] = fn;}}

addEvent(window, 'load', findPopUps, false);
/* popup: fim */