﻿//
//  main.js
//
//  Created by RMG Digital on 2009-03-10.
//  Copyright (c) 2009 RMG Connect MI. All rights reserved.
//

var sIdOld;
var strData , strCerca, currUrl, currPage, currCat;
var iBack=0;
var arrBack = new Array();
var swfHead;

function privacy(){
    window.open('privacy.htm','privacy','width=350, height=200, scrollbars=no, resizable=yes');
}

function pressKey() {    
    if (typeof window.event != 'undefined')
        document.onkeydown = function(){
            if (event.keyCode == 13)
                return false
        }
    else
        document.onkeypress = function(e){
            if (e.which == 13)
                return false     
        }
}


function indirizzoEmailValido(indirizzo) {
    if (window.RegExp) {
        var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
        var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        var regnv = new RegExp(nonvalido);
        var regv = new RegExp(valido);
        if (!regnv.test(indirizzo) && regv.test(indirizzo))
            return true;
        return false;
    }
    else {
        if(indirizzo.indexOf("@") >= 0)
            return true;
        return false;
    }
};

function apriPan(pan){
    if($(pan).getStyle('display')=="none")    
        $(pan).show();
    else
        $(pan).hide();
}

function azzeraMenu(){
    $('imgNews').update('<img src="IMG/news.jpg" alt="" name="News" id="News" onload="" />');          
    $('imgClienti').update('<img src="IMG/clienti.jpg" alt="" name="Clienti" id="Clienti" onload="" />');          
    $('imgCase').update('<img src="IMG/case.jpg" alt="" name="Case" id="Case" onload="" />');          
    $('imgPeople').update('<img src="IMG/people.jpg" alt="" name="People" id="People" onload="" />');          
    $('imgPreami').update('<img src="IMG/premi.jpg" alt="" name="Premi" id="Premi" onload="" />');
}

function settaMenu(strId){ 
    if(strId=='1')
      $('imgNews').update('<img src="IMG/news_over.jpg" alt="" name="News" id="News" onload="" />'); 
    else if(strId=='2')
      $('imgClienti').update('<img src="IMG/clienti_over.jpg" alt="" name="Clienti" id="Clienti" onload="" />');
    else if(strId=='3')
      $('imgCase').update('<img src="IMG/case_over.jpg" alt="" name="Case" id="Case" onload="" />');       
    else if(strId=='4')
      $('imgPeople').update('<img src="IMG/people_over.jpg" alt="" name="People" id="People" onload="" />'); 
    else if(strId=='5')
      $('imgPreami').update('<img src="IMG/premi_over.jpg" alt="" name="Premi" id="Premi" onload="" />'); 
    
}

function azzeraPulsFlash(){
	if(swfHead != undefined)
    	swfHead.setPulsUp(); 
    hideSocialDialog();	
}

function homePage(){
    sIdOld=0;    
    currUrl = 'home.aspx';
    new Ajax.Request(currUrl, {  
        method: 'post',                
        onCreate: function() {
             $('load').show(); 
             $('panAjax').hide();
        }
        ,onComplete: function(transport) {            
             $('load').hide();
             $('panAjax').update(transport.responseText).show();
             azzeraMenu();         
             
             //alert($('head'));
             swfHead.setPulsDown(1);    
                     
             arrBack[iBack]=currUrl;
             iBack++;
        }        
    });
} 

function sezPage(strURL, strId, curPage){
    if(strId!=sIdOld){    
        strData="";
        strCerca="";
        currUrl=strURL;
        currCat=strId;
        new Ajax.Request(currUrl, {  
            method: 'post',                
            onCreate: function() {              
                $('load').show(); 
                $('panAjax').hide(); 
            }
            ,onComplete: function(transport) {            
                $('load').hide();                
                $('panAjax').update(transport.responseText).show();                                                                                       
                azzeraMenu();   
                
                azzeraPulsFlash();
                settaMenu(strId);  
                
                arrBack[iBack]=currUrl;
                iBack++;            
             }
            ,parameters:{cat:strId, page:curPage}      
        });    
        sIdOld=strId;
    }
}

function setPage(strURL, strId, curPage){
    currPage=curPage;
    new Ajax.Request(strURL, {  
        method: 'post',                
        onCreate: function() {              
            $('load').show().setStyle({ margin:'5px 0 0 495px'}); 
            //$('panAjax').hide();             
        }
        ,onComplete: function(transport) {            
            $('load').hide().setStyle({ margin:'230px 0 0 250px'});                
            $('panAjax').update(transport.responseText).show(); 
         }
        ,parameters:{cat:strId, page:curPage, str:strCerca, data:strData}      
    }); 
}

function cercaPage(cercaStr, cercaData){   
    if($('txtCerca').value=="" && cercaData==""){
        alert("Inserisci il termine di ricerca !!!");
        return;
    }    
    strData=cercaData;                  
    if(cercaStr=="x" && cercaData=="")
        strCerca=$('txtCerca').value;
    else  
        strCerca=""; 
    currCat=""; 
    currUrl = 'posts.aspx';
    new Ajax.Request(currUrl, {  
        method: 'post',                
        onCreate: function() {
             $('load').show(); 
             $('panAjax').hide();
        }
        ,onComplete: function(transport) {            
             $('load').hide();
             $('panAjax').update(transport.responseText).show();
             azzeraMenu();
             azzeraPulsFlash();  
             sIdOld=0;    
        }
        ,parameters:{str:strCerca, data:strData}      
    });     
}

function detPage(strId){     
    currUrl= 'post.aspx';        
    new Ajax.Request(currUrl, {  
        method: 'posts',                
        onCreate: function() {              
             $('load').show(); 
             $('panAjax').hide();
        }
        ,onComplete: function(transport) {            
          $('load').hide();
          $('panAjax').update(transport.responseText).show();
         
		 //***** Carico Player Multimediale ******
		 var flashvarsDet = {"id": strId };
		 var paramsDet = {allowFullScreen : true, allowScriptAccess: "always", scale:"noscale", salign:"tl", wmode: "transparent"};
	     var attributesDet = {};
		 swfobject.embedSWF("SWF/player.swf", "player", "530", "425", "9.0.0", "flashviral/assets/swf/playerProductInstall.swf", flashvarsDet, paramsDet, attributesDet);
          //***************************************
          azzeraMenu();
          azzeraPulsFlash();              
        }
        ,parameters:{id:strId}      
    });
}

function backPage(){   
    hideSocialDialog();
    if(arrBack[iBack-1]!=undefined)
      backURL=arrBack[iBack-1];
    else
      backURL='home.aspx';
    
    new Ajax.Request(backURL, {  
        method: 'posts',                
        onCreate: function() {              
             $('load').show(); 
             $('panAjax').hide();
        }
        ,onComplete: function(transport) {            
          $('load').hide();
          $('panAjax').update(transport.responseText).show();
          azzeraMenu();
                              
          if(backURL=="home.aspx")    
            swfHead.setPulsDown(1);  
            
          settaMenu(currCat);
        }
        ,parameters:{cat:currCat, page:currPage, str:strCerca, data:strData  }       
    });
}

function staticPage(strURL, iPuls){ 
    sIdOld=0;         
    new Ajax.Request(strURL, {  
        method: 'posts',                
        onCreate: function() {              
             $('load').show(); 
             $('panAjax').hide();
        }
        ,onComplete: function(transport) {            
          $('load').hide();
          $('panAjax').update(transport.responseText).show();
          azzeraMenu();           
         if(iPuls!=undefined){
            azzeraPulsFlash();   
            
            swfHead.setPulsDown(iPuls);
            
          }
        }
    });
}

function contattiForm(){
    if($('nome').value==""){
        alert("Inserisci il nome !!!");
        return;
    }else if($('cognome').value==""){
        alert("Inserisci il cognome !!!");
        return;
    }else if($('societa').value==""){
        alert("Inserisci la società !!!");
        return;
    }else if(!indirizzoEmailValido($('email').value)){
        alert("Email non corretta !!!");
        return;
    }else{  
        new Ajax.Request('contatti.aspx', {  
            method: 'posts',                
            onCreate: function() {              
                 $('load').show(); 
                 $('panAjax').hide();                 
            }
            ,onComplete: function(transport) {            
              $('load').hide();
              $('panAjax').update(transport.responseText).show();                    
            } 
            ,parameters:{
                cmd:"ins",
                nome:$('nome').value,
                cognome:$('cognome').value,
                societa:$('societa').value,
                ruolo:$('ruolo').value,
                email:$('email').value,
                note:$('note').value,
                privacy:$('checkbox').checked
            }        
        });
    }
}

function emailJob(){     
    if(indirizzoEmailValido($('email').value))
        return $('email').value;
    else
        return;
}

function jobForm(){        
    var strFileName=$('upload').fileName();     
  
    if($('nome').value==""){
        alert("Inserisci il nome !!!");
        return;
    }else if($('cognome').value==""){
        alert("Inserisci il cognome !!!");
        return;
    }else if($('giorno').value=="x"){
        alert("Seleziona il giorno !!!");
        return;
    }else if($('mese').value=="x"){
        alert("Seleziona il mese !!!");
        return;
    }else if($('anno').value=="x"){
        alert("Seleziona l'anno !!!");
        return;
    }else if($('comune').value==""){
        alert("Inserisci il comune !!!");
        return;
    }else if(!indirizzoEmailValido($('email').value)){
        alert("Email non corretta !!!");
        return;                
    }else if(strFileName==null){    
        alert("Seleziona il documento da caricare !!!");
        return;        
    }else if(!$('checkbox').checked){    
        alert("Acconsenti il trattamento dei dati personali !!!");
        return;
    }else{        
        var strDs="";   
        //alert("Controlli passati");
        //alert(strFileName);
                
        if($('Checkbox1').checked)
            strDs+="Freelance,";            
        if($('Checkbox2').checked)
            strDs+="Stage,";
        if($('Checkbox3').checked)
            strDs+="Collaboratore,";
        if($('Checkbox4').checked)
            strDs+="Contratto,";            
        if($('Checkbox5').checked)
            strDs+="Indifferente,";        
        new Ajax.Request('jobs.aspx', {  
            method: 'posts',                
            onCreate: function() {              
                 $('load').show(); 
                 $('panAjax').hide();                 
            }
            ,onComplete: function(transport) {            
              $('load').hide();
              $('panAjax').update(transport.responseText).show();                    
            } 
            ,parameters:{
                cmd:"ins",
                rif:$('rif').value,
                nome:$('nome').value,                
                cognome:$('cognome').value,                
                data:$('giorno').value+"/"+$('mese').value+"/"+$('anno').value,
                comune:$('comune').value,
                telefono:$('telefono').value,
                email:$('email').value,                
                sito:$('sito').value,
                impiego:$('impiego').value,
                azienda:$('azienda').value,
                esp:$('esp').value,                                        
                disp:strDs,  
                File1:strFileName,
                note:$('note').value,                
                privacy:$('checkbox').checked
            }        
        });
    }        
}

function delayFbBridgeInit(){
	//alert("delayFbBridgeInit");
	swfHead = swfobject.getObjectById("headObj");

    FBFlashBridgeInit("176ae376747c362394ce127bb5a7dbe5", "http://www.rmgconnect.it/stage/xd_receiver.htm",  swfHead);

	swfHead.setPulsDown("1");
}

function start(){
	var idPost = swfobject.getQueryParamValue("id");
	
    $('load').update("<img src=\"img/loading.gif\" />");
    
    
    
    
    
	var flashvars = { };
	var params = {allowFullScreen : false, allowScriptAccess: "always", scale:"noscale", salign:"tl", "wmode": "transparent"};
	var attributes = {id: "headObj"};
	swfobject.embedSWF("swf/header.swf", "topHead", "763", "280", "9.0.0", "flashviral/assets/swf/playerProductInstall.swf", flashvars, params, attributes);
	
    if(idPost!="") detPage(idPost);
    else homePage();
    
    pressKey();

}

jQuery.noConflict();

jQuery(document).ready(function(){
    setTimeout(delayFbBridgeInit, 2000);
    externalLinks();
	start();
});

//window.onload = start;

function externalLinks() {
      //delayFbBridgeInit()


    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
      for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "blank") {
anchor.target = "_blank";
if (anchor.title) anchor.title += " (Il link apre una nuova finestra)";
if (!anchor.title) anchor.title = "Il link apre una nuova finestra";
}
}
}
//window.onload = externalLinks;


