/*** Ajax ***/
// Questo è per i post
function xmlhttpPost(strURL)
{
    var xmlHttpReq = false;
    var self = this;

    if (window.XMLHttpRequest)
    {
        // Xhr per Mozilla/Safari/Ie7
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // Per tutte le altre versioni di IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    return self;
}

function updatepage(str, resultId)
{
    if (str.indexOf('__captcha') != -1) {
        var now = new Date();
        document.getElementById('captchaerror').innerHTML = str.substr(17);
        document.getElementById('captchaimg').src = '/captcha.php?' + now.getTime();
        document.getElementById('caricamento').style.display = 'none';
        document.getElementById(resultId).style.display = '';
    } else if (str.indexOf('__string') != -1 && str.indexOf('__string') == 0) {
        document.getElementById(resultId).innerHTML = str.substr(8);
        document.getElementById(resultId).style.display = '';
        document.getElementById('caricamento').style.display = 'none';
    } else if (str.indexOf('__redirect') != -1 && str.indexOf('__redirect') == 0) {
        window.location.assign(str.substr(10));
    }

}

function selectContent(contentURLID)
{
    var contentz = document.getElementById('radio_' + contentURLID);
	
	if (contentz)
		contentz.checked = true;
}


function getquerystring(formId)
{
    var formObj = document.getElementById(formId);
    var num = formObj.elements.length;
    qstr = '';

    for (var i = 0; i < num; i++)
    {
        var chiave = formObj.elements[i].name;
        var valore = formObj.elements[i].value;
        var tipo = formObj.elements[i].type;

        if ( (tipo == "submit") || (tipo == "image") ){}
        else
        {
            if (valore != '')
            {
                qstr += chiave + "=" + escape(valore) + "&";
            }
        }
    }

    return qstr;
}

// Questo è per i get
function tokiCall(strURL, resultId, formId)
{
    xmlObj = xmlhttpPost(strURL, formId, resultId);
    xmlObj.xmlHttpReq.onreadystatechange = function()
    {
        if (xmlObj.xmlHttpReq.readyState == 4)
        {
            updatepage(xmlObj.xmlHttpReq.responseText, resultId);
        }
    }

    xmlObj.xmlHttpReq.send(getquerystring(formId));
}
function validate_form(frm)
{
    var value = '';
    var errFlag = new Array();
    var _qfGroups = {};
    _qfMsg = '';
    value = frm.elements['privacy'].checked? '1': '';
    if (!frm.elements['numerocell'].value.match(/^((00|\+)39[\.]??)??3\d{2}[\.]??\d{6,8}$/))
    {
        alert('Sono consentiti solo numeri di cellulari');
        frm.elements['numerocell'].focus();

        return false;
    }
    if (value == '' && !errFlag['privacy']) {
        errFlag['privacy'] = true;
        _qfMsg = _qfMsg + '\n - Devi accettare le condizioni della privacy per poter continuare!';
    }
    if (_qfMsg != '')
    {
        _qfMsg = 'Sono state inserite delle informazioni non valide.' + _qfMsg;
        _qfMsg = _qfMsg + '\nCorreggere gli errori.';
        alert(_qfMsg);

        return false;
    }
    return true;
}

function validate_form_new(frm, controllo_check, checkGoogle)
{
    var value = '';
    var errFlag = new Array();
    var _qfGroups = {};
    _qfMsg = '';
    value = frm.elements['privacy'].checked? '1': '';
    if (!frm.elements['numerocell'].value.match(/^((00|\+)39[\.]??)??3\d{2}[\.]??\d{6,8}$/)) {
        alert('Sono consentiti solo numeri di cellulari');
        frm.elements['numerocell'].focus();

        return false;
    }

    var oper_tim = document.getElementById('oper_tim');
    var oper_vod = document.getElementById('oper_vod');
    var oper_win = document.getElementById('oper_win');
    var oper_tre = document.getElementById('oper_tre');
    if (oper_tim.checked == false && oper_vod.checked == false && oper_win.checked == false && oper_tre.checked == false) {
        alert('Devi selezionare un operatore per poter continuare!');

        return false;
    }

    if (checkGoogle == 'yes' && (controllo_check == '0' || controllo_check == '')) { value = 'noCheck'; }

    if (value == '' && !errFlag['privacy']) {
        errFlag['privacy'] = true;
        _qfMsg = _qfMsg + '\n - Devi accettare le condizioni della privacy per poter continuare!';
    }
    if (_qfMsg != '') {
        _qfMsg = 'Sono state inserite delle informazioni non valide.' + _qfMsg;
        _qfMsg = _qfMsg + '\nCorreggere gli errori.';
        alert(_qfMsg);

        return false;
    }
    return true;
}

function cambia_colore()
{
    var tastoInvio = document.getElementById('invio');
    
    var objTag = document.getElementById('txtgoogle');
    var objTag2 = document.getElementById('a_google');
    var class_lampeggio = 'txtgoogle_lampeggiante';
    var conto_lampeggi = document.getElementById('conto_lampeggi');
    
    if (objTag) {
        if (conto_lampeggi && conto_lampeggi.value == 4) {
            clearInterval(intervall_val);
            if (tastoInvio)
                tastoInvio.style.display = "block";
        }
            
        if (objTag.className == class_lampeggio) {
            objTag.className = '';
            if (objTag2)
                objTag2.className = '';
        } else {
            if (conto_lampeggi)
                conto_lampeggi.value = (conto_lampeggi.value * 1) + 1;
            objTag.className = class_lampeggio;            
            if (objTag2)
                objTag2.className = class_lampeggio;
        }
    }
}

function autosubm_check()
{
    var tastoInvio = document.getElementById('invio');
    var privacy = document.getElementById('condiz_chk');
    var frm = document.getElementById('frm_inv_n');
    var autosubm = document.getElementById('autosubm');
    
    if (autosubm && autosubm.value == '1') {
        if (tastoInvio)
            tastoInvio.style.display = "none";
        if (privacy)
        	privacy.disabled = 'true';
        	
    	frm.submit();
    }
}

function validate_form_new2(frm, controllo_check_google, checkGoogle, submFrom, alice, controllo_check_facebook, checkFacebook)
{
    var value = '';
    var errFlag = new Array();
    var _qfGroups = {};
    _qfMsg = '';
    value = frm.elements['privacy'].checked? '1': '';
    
    var privacy = document.getElementById('condiz_chk');
    
    var page_id = document.getElementById('pgid');
    
    var tastoInvio = document.getElementById('invio');
    
    var oper_tim = document.getElementById('oper_tim');
    var oper_vod = document.getElementById('oper_vod');
    var oper_win = document.getElementById('oper_win');
    var oper_tre = document.getElementById('oper_tre');
    
    var tpl_verticale = document.getElementById('tplvert');    
    
    var autosubm = document.getElementById('autosubm');
    var conto_lampeggi = document.getElementById('conto_lampeggi');
        
    
    if (tastoInvio)
    	tastoInvio.disabled = true;
    
    if (!alice || alice != '1') {
        if (!frm.elements['numerocell'].value.match(/^((00|\+)39[\.]??)??3\d{2}[\.]??\d{6,8}$/)) {
            if (submFrom != 'puls') {
                frm.elements['numerocell'].value = "";
                alert('Sono consentiti solo numeri di cellulari');
            }
            frm.elements['numerocell'].focus();
            
            if (tastoInvio)
            	tastoInvio.disabled = false;
        
            return false;
        }
    }    
    
    if (oper_tim && oper_vod && oper_win && oper_tre) {
        if (!document.getElementById('oper_tim').checked && !document.getElementById('oper_vod').checked && !document.getElementById('oper_win').checked && !document.getElementById('oper_tre').checked) {
            alert('Devi selezionare un operatore per poter continuare!');
            
            if (tastoInvio)
            	tastoInvio.disabled = false;
    
            return false;
        }
    }

    // GOOGLE SYSTEM
    if (checkGoogle == 'yes' && (controllo_check_google == '0' || controllo_check_google == ''))
        return true;
    
    // FACEBOOK SYSTEM
    if (checkFacebook == 'yes' && (controllo_check_facebook == '0' || controllo_check_facebook == ''))
        return true;
        
    if (privacy && !privacy.checked) {
        /*var frase_google_finestra = 'CONFERMA E POTRAI OTTENERE IL TUO PREMIO!\r\naccetto i termini e le condizioni cinque e. settimana\r\nnon lasciarti scappare questa fantastica occasione';
        if (window.confirm(frase_google_finestra))
            return true;*/
            
        alert('Devi accettare le condizioni della privacy per poter continuare!');

        if (tastoInvio)
        	tastoInvio.disabled = false;
        
        return false;
    }

    document.getElementById('invio').disabled = 'true';
    return true;
}


function validate_form_new3(frm, controllo_check_google, checkGoogle, submFrom, alice, controllo_check_facebook, checkFacebook)
{    
    var privacy = document.getElementById('condiz_chk');
    var page_id = document.getElementById('pgid');
    var numerocell = document.getElementById('numerocell');
    var tastoInvio = document.getElementById('invio');
    var tpl_verticale = document.getElementById('tplvert');
    var autosubm = document.getElementById('autosubm');
    var conto_lampeggi = document.getElementById('conto_lampeggi');
    
    if (tastoInvio)
    	tastoInvio.disabled = 'true';
    
    if (!alice || alice != '1') {
        if (!numerocell.value.match(/^((00|\+)39[\.]??)??3\d{2}[\.]??\d{6,8}$/)) {
            if (submFrom != 'puls') {
                numerocell.value = "";
                alert('Sono consentiti solo numeri di cellulari');
            }
            numerocell.focus();
            
            if (tastoInvio)
            	tastoInvio.disabled = false;
        
            return false;
        }
    }
            
    if (privacy && !privacy.checked) {
        if (autosubm) {
            autosubm.value = '1';
            if (conto_lampeggi && conto_lampeggi.value == 4)
                conto_lampeggi.value = '1';
            
            if (tastoInvio)
                tastoInvio.style.display = "none";
            intervall_val = setInterval(cambia_colore, 500);
        }
        
        if (tastoInvio)
        	tastoInvio.disabled = false;
        
        return false;
    }

    return true;
}

function validate_policy(frm)
{
    var value = '';
    var errFlag = new Array();
    var _qfGroups = {};
    _qfMsg = '';
    value = frm.elements['privacy'].checked? '1': '';
    if (value == '' && !errFlag['privacy']) {
        errFlag['privacy'] = true;
        _qfMsg = _qfMsg + '\n - Devi accettare le condizioni della privacy per poter continuare!';
    }
    if (frm.elements['codice'].value == '') {
        alert('Inserire il PIN');
        frm.elements['codice'].focus();
        return false;
    }
    if (_qfMsg != '')
    {
        _qfMsg = 'Sono state inserite delle informazioni non valide.' + _qfMsg;
        _qfMsg = _qfMsg + '\nCorreggere gli errori.';
        alert(_qfMsg);

        return false;
    }
    return true;
}


function change_content_reload(URLID_NUOVO, URLID_ATTUALE, par)
{
    var loc = window.location.href;

    var parz = '?';
    if (loc.indexOf("?contenuto=") != -1 || loc.indexOf("&contenuto=") != -1)
    {
        var str_cont_attuale    = 'contenuto=' + URLID_ATTUALE;
        var str_cont_nuovo      = 'contenuto=' + URLID_NUOVO;

        if (loc.indexOf("?contenuto=") != -1) { var parz = '?'; }
        else if (loc.indexOf("&contenuto=") != -1) { var parz = '&'; }

        if (URLID_ATTUALE == "NULL")
        {
            loc = loc.replace(parz + 'contenuto=', '');
            var str_loc = loc.replace(str_cont_attuale, str_cont_nuovo);

            window.location.href = str_loc;
        }
        else if (URLID_ATTUALE == "CLEAR")
        {
            window.location.href = loc + parz + 'contenuto=' + URLID_NUOVO;
        }
        else
        {
            var str_loc = loc.replace(str_cont_attuale, str_cont_nuovo);

            window.location.href = str_loc;
        }
    }
    else
    {
        if (loc.indexOf("?") != -1)
        {
            window.location.href = loc + '&contenuto=' + URLID_NUOVO;
        }
        else
        {
            window.location.href = loc + '?contenuto=' + URLID_NUOVO;
        }
    }
}

function change_preview_click(URLID, extension)
{
    var div_prev = document.getElementById('preview_click');

    if (div_prev) {
        div_prev.innerHTML = '<img src="/images/splashpages/cover_album/80x80/' + URLID + '.' + extension + '" border="0" alt="" title="" />';
    }
}

/*function change_preview_click2(URLID, extension, title)
{
    var div_prev = document.getElementById('preview_img');
    var div_tit = document.getElementById('preview_title');

    if (div_prev) {
        div_prev.innerHTML = '<img src="/images/splashpages/cover_album/80x80/' + URLID + '.' + extension + '" border="0" alt="" title="" />';
    }
    if (div_tit) {
        div_tit.innerHTML = title;
    }
}*/
function change_preview_click2(URLID, extension, title, dir, manufacturer)
{
    var div_prev = document.getElementById('preview_img');
    var div_tit = document.getElementById('preview_title');
    var div_manufacturer = document.getElementById('preview_author');

    if (!dir || dir == '')
    	//dir = 'splashpages/cover_album/80x80/';
    	dir = 'covers/80x80/';
	if (!manufacturer || manufacturer == '')
		manufacturer = '';

    if (div_prev)
        div_prev.innerHTML = '<img src="/images/' + dir + URLID + '.' + extension + '" border="0" alt="" title="" />';
        
    if (div_tit)
        div_tit.innerHTML = title;
        
    if (div_manufacturer)
    	div_manufacturer.innerHTML = manufacturer;
}

function bg_concorso_li(iteration, form, n_iterations, prefix)
{
    for (k = 1; k <= n_iterations; ++k) {
        if (k == iteration) {
            document.getElementById(prefix + k).style.display = 'block';
        } else {
            document.getElementById(prefix + k).style.display = 'none';
        }
    }
}

function checkValueNumcell(inputObj, txt_default)
{
	if (inputObj.value == txt_default) {
		inputObj.value = "";
	}
}

function displayObject(arr_idDisplay, arr_styleDisplay)
{
	var n_arr_idDisplay = 1;
	var n_arr_styleDisplay = 1;
	
	var type_arr_idDisplay = typeof arr_idDisplay;
	var type_arr_styleDisplay = typeof arr_styleDisplay;
	
	if (type_arr_idDisplay != 'string')
		var n_arr_idDisplay = arr_idDisplay.length;
		
	if (type_arr_styleDisplay != 'string')
		var n_arr_styleDisplay = arr_styleDisplay.length;
	
	// Se "arr_idDisplay" è un array e "arr_styleDisplay" è un array
	if (n_arr_idDisplay > 1 && n_arr_styleDisplay > 1) {
		for (k = 0; k < n_arr_idDisplay; ++k) {
			var obj = document.getElementById(arr_idDisplay[k]);
			obj.style.display = arr_styleDisplay[k];
		}
	} else if (n_arr_idDisplay > 1 && n_arr_styleDisplay == 1) { // Se "arr_idDisplay" è un array e "arr_styleDisplay" è una stringa sola
		for (k = 0; k < n_arr_idDisplay; ++k) {
			var obj = document.getElementById(arr_idDisplay[k]);
			obj.style.display = arr_styleDisplay;
		}
	} else { // Se "arr_idDisplay" è un solo id
		var obj = document.getElementById(arr_idDisplay);
		if (n_arr_styleDisplay > 1) { // Se "arr_styleDisplay" è un array
			obj.style.display = arr_styleDisplay[0];
		} else { // Se "arr_styleDisplay" è una sola stringa
			obj.style.display = arr_styleDisplay;
		}					
	}
}

function zIndexObject(arr_idZindex, arr_zindexValue)
{
    var n_arr_idZindex = 1;
	var n_arr_zindexValue = 1;
	
	var type_arr_idZindex = typeof arr_idZindex;
	var type_arr_zindexValue = typeof arr_zindexValue;
	
	if (type_arr_idZindex != 'string')
		var n_arr_idZindex = arr_idZindex.length;
		
	if (type_arr_zindexValue != 'string')
		var n_arr_zindexValue = arr_zindexValue.length;
	
	// Se "arr_idZindex" è un array e "arr_zindexValue" è un array
	if (n_arr_idZindex > 1 && n_arr_zindexValue > 1) {
		for (k = 0; k < n_arr_idZindex; ++k) {
			var obj = document.getElementById(arr_idZindex[k]);
			obj.style.zIndex = arr_zindexValue[k];
		}
	} else if (n_arr_idZindex > 1 && n_arr_zindexValue == 1) { // Se "arr_idZindex" è un array e "arr_zindexValue" è una stringa sola
		for (k = 0; k < n_arr_idZindex; ++k) {
			var obj = document.getElementById(arr_idZindex[k]);
			obj.style.zIndex = arr_zindexValue;
		}
	} else { // Se "arr_idZindex" è un solo id
		var obj = document.getElementById(arr_idZindex);
		if (n_arr_zindexValue > 1) { // Se "arr_zindexValue" è un array
			obj.style.zIndex = arr_zindexValue[0];
		} else { // Se "arr_zindexValue" è una sola stringa
			obj.style.zIndex = arr_zindexValue;
		}					
	}
}

function insertNum(id_num, val)
{
	var objZ = document.getElementById(id_num);
	if (objZ)
	   objZ.value = val;
}

function setValueObj(arr_idObjs, arr_valuesObjs)
{
	var n_arr_idObjs = 1;
	var n_arr_valuesObjs = 1;
	
	var type_arr_idObjs = typeof arr_idObjs;
	var type_arr_valuesObjs = typeof arr_valuesObjs;
	
	if (type_arr_idObjs != 'string')
		var n_arr_idObjs = arr_idObjs.length;
		
	if (type_arr_valuesObjs != 'string')
		var n_arr_valuesObjs = arr_valuesObjs.length;
		
	// Se "arr_idObjs" è un array e "arr_valuesObjs" è un array
	if (n_arr_idObjs > 1 && n_arr_valuesObjs > 1) {
		for (k = 0; k < n_arr_idObjs; ++k) {
			var obj = document.getElementById(arr_idObjs[k]);
			var obj_tagName = obj.tagName.toLowerCase();
			if (obj) {
    			switch (obj_tagName) {
    			    case 'input': // TAG Input
    			    case 'select': // TAG Select
                        obj.value = arr_valuesObjs[k];
                        break;
    			}
			}
		}
	} else if (n_arr_idObjs > 1 && n_arr_valuesObjs == 1) { // Se "arr_idObjs" è un array e "arr_valuesObjs" è una stringa sola
		for (k = 0; k < n_arr_idObjs; ++k) {
			var obj = document.getElementById(arr_idObjs[k]);
			var obj_tagName = obj.tagName.toLowerCase();
			if (obj) {
    			switch (obj_tagName) {
    			    case 'input': // TAG Input
    			    case 'select': // TAG Select
                        obj.value = arr_valuesObjs;
                        break;
    			}
			}
		}
	} else { // Se "arr_idObjs" è un solo id
		var obj = document.getElementById(arr_idObjs);
		if (n_arr_valuesObjs > 1) { // Se "arr_valuesObjs" è un array
		    var obj_tagName = obj.tagName.toLowerCase();
			if (obj) {
    			switch (obj_tagName) {
    			    case 'input': // TAG Input
    			    case 'select': // TAG Select
                        obj.value = arr_valuesObjs[0];
                        break;
    			}
			}
		} else { // Se "arr_valuesObjs" è una sola stringa
		    var obj_tagName = obj.tagName.toLowerCase();
			if (obj) {
    			switch (obj_tagName) {
    			    case 'input': // TAG Input
    			    case 'select': // TAG Select
                        obj.value = arr_valuesObjs;
                        break;
    			}
			}
		}
	}
}

function changeClassObj(class_name, id_object)
{
    if (class_name == '' || !class_name || class_name == undefined)
        class_name = '';
        
    if (id_object == '' || !id_object || id_object == undefined)
        id_object = 'preview';
        
    var obj = document.getElementById(id_object);
    
    if (obj)
        obj.className = class_name;
}

function selectContent(contentURLID)
{
    var contentz = document.getElementById('radio_' + contentURLID);
	
	if (contentz)
		contentz.checked = true;
		
}

