// Isaac Roca - iroca at pragmapublicitat dot cat

/*
EXEMPLE DE SECCIÓ AMB CALENDARI 

FnLoad.push({ 
	sec:'admInmuebles',
	fn: function () {
			if (!calendariIncluded) {
				csslink('/lib/calendari/calendar-blue.css');
				include('/lib/calendari/calendar_stripped.js');
				include('/lib/calendari/lang/calendar-es.js');
				include('/lib/calendari/calendar-setup_stripped-inm.js');
				calendariIncluded=true;
			} else {
				setupcalendar();
			}
	}
});


function setupcalendar() {
		Calendar._C = null;
		window._dynarch_popupCalendar = null;
		calendari=Calendar.setup({ 
				inputField:'flimit',
				ifFormat:'%Y-%d-%m', 
				button:'lanzador' 
		});
}

*/


FnLoad.push({ 
	sec:'application-form',
	fn: function () {
			desti=wwwroot.replace("http:","https:")+'/application-form';
			if (document.location!=desti) document.location=desti;
	}
});

FnLoad.push({ 
	sec:'order-form',
	fn: function () {
			desti=wwwroot.replace("http:","https:")+'/order-form';
			if (document.location!=desti) document.location=desti;
	}
});

FnLoad.push({ 
	sec:'underconstruction',
	fn: function () {
			underConstruction();
			document.location='/';
	}
});

function underConstruction() {
	alert('Under construction');	
}

function onEnter(ev,formu) {  
	if(ev==13) {
		loginMembers('members',formu);
   	} 
}

/* Funcions lightbox */

function showBox(){
    $('overlay').show();
    center('box');
    return false;
}

function hideBox(){
    $('box').hide();
    $('overlay').hide();
    return false;
}

function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.zIndex   = 99;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}


/* FI -- Funcions lightbox */

function mostrar(ref)
{
	
	if (_img_grande.width<20) 
	_img_grande.width = 300;
	if (_img_grande.height<20) 
	_img_grande.height = 400;
	
	//alert(_img_grande.width);
	//alert(_img_grande.height);
	var ops = "top=" + ((screen.height - _img_grande.height) / 2);
	ops += ",left=" + ((screen.width - _img_grande.width) / 2);
	ops += ",width=" + _img_grande.width + ",height=" + _img_grande.height;
	var contenido = "<html>\n<head>\n<title>Picture</title>\n<body style='background-image: url(" + _img_grande.src + ")'></body></html>";
	var ventana = window.open("", "", ops);
	
	ventana.document.open();
	ventana.document.write(contenido);
	ventana.document.close();  //caricatos
}

function cargando(ref) {
	if (_img_grande.complete) mostrar(ref);
	else setTimeout("cargando()", 100);
}

function abrir(urlfoto,ref)	{
	_img_grande = new Image();
	_img_grande.src = urlfoto;
	cargando(ref);
}

function getRadioButtonSelectedValue(ctrl) {
    for(i=0;i<ctrl.length;i++)
        if(ctrl[i].checked) return ctrl[i].value;
}



function enviarform(archivo)
{
	
	if(archivo == '/china2010-questionnaire')
	{
			if (document.formulario.nom.value==''){alert("Name required.");return false;}
			if (document.formulario.email.value==''){alert("E-mail required.");return false;}
	}
	if(archivo == 'chile2008-questionnaire')
	{
			if (document.formulario.nom.value==''){alert("Name required.");return false;}
			if (document.formulario.email.value==''){alert("E-mail required.");return false;}
	}
	if(archivo == 'monaco2009-questionnaire')
	{
			if (document.formulario.nom.value==''){alert("Name required.");return false;}
			if (document.formulario.email.value==''){alert("E-mail required.");return false;}
	}
	if(archivo == '/applicationform-ok')
	{
			if (!document.formulario.validterms.checked){alert("Sorry, you have to agree the privacy statement in order to continue.");return false;}
			if (document.formulario.empresa.value==''){alert("Company name required.");return false;}
			if (document.formulario.pais.value==''){alert("Country required.");return false;}
			
			if (getRadioButtonSelectedValue(document.formulario.Business)!='MembershipRenewal') {
				if (document.formulario.direccion.value==''){alert("Street required.");return false;}
				if (document.formulario.telefono.value==''){alert("Phone required.");return false;}	
			} 
	}
	if(archivo == 'orderform-ok')
	{		if (!document.formulario.validterms.checked){alert("Sorry, you have to agree the privacy statement in order to continue.");return false;}
			if (document.formulario.name.value==''){alert("Name required.");return false;}
			if (document.formulario.direccion.value==''){alert("Street required.");return false;}
			if (document.formulario.telefono.value==''){alert("Phone required.");return false;}
			
	}
	if(archivo == 'advertising-order-form-ok')
	{		if (!document.formulario.validterms.checked){alert("Sorry, you have to agree the privacy statement in order to continue.");return false;}
			if (document.formulario.name.value==''){alert("Name required.");return false;}
			if (document.formulario.address.value==''){alert("Street required.");return false;}
			if (document.formulario.phone.value==''){alert("Phone required.");return false;}
			
			
	}
	if(archivo == 'contact-ok')
	{		
			if (!document.formulario.validterms.checked){alert("Sorry, you have to agree the privacy statement in order to continue.");return false;}
			if (document.formulario.nombre_de_la_empresa.value==''){alert("Company required");return false;}
			if (document.formulario.pais.value==''){alert("Country required");return false;}
			
	}
	
	document.formulario.action = archivo;
	document.formulario.submit();

}


function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


function checkCopyright (urltogo) {
	myLightWindow.activateWindow({
			href: '/php/copyright.php?urltogo='+escape(urltogo),
			title: 'Copyright',
			author: 'nutfruit.org',
			caption: 'Copyright nutfruit.org',
			width: 400,
			height: 260
			
		/*	href			The hyperlink for the window
			title 			Title of window
			author 			Author of window
			caption 		The Caption for the window
			rel 			Set the rel tag
			top 			Top position of the window
			left 			Left position of the window
			type 			The type of the window you want to change it too
			showImages 		How many images to show
			height 			The height of the window
			width 			The width of the window
			loadingAnimation 	Make the Loading Cover skip the animation
			iframeEmbed 		 Emebed the Media into an iframe instead of a div
			form 			 The name of the form
		*/
	});
}

jQuery(document).ready(function () {
								 
	jQuery('.checkcopyright').each(function (i,o) {
		
		var urlok = o.href;
		
		jQuery(o).click(function () {
			
			if (getCookie('copychk')=='OK') {
				document.location = urlok;
			} else {
				checkCopyright(urlok);
			}	 
			
			return false;
		
		});
		
		o.href = '#';
		
	});
	
});

function openDiv(codi) {
	//alert('codi:'+codi);
	jQuery(".cos"+codi).each(function () {
			if (jQuery(this).css("display")=="none") {
				jQuery(this).slideDown("slow");
				jQuery("#"+codi+" .btnNews img").attr('src', '/img/detallenews.jpg');
			} else {
				jQuery(this).slideUp("slow");
				jQuery("#"+codi+" .btnNews img").attr('src', '/img/detallenews2.jpg');
			}
	});
}




