function initApp(base_url, aLabel){

	this.base_url = base_url;
	
	this.aLabel = aLabel;
	
	this.waiting_label = this.aLabel["JS_WAITING_LABEL"];
	
	
};

function goToLink(path){
	
	//$(document).ready(function() {

		$.blockUI('<div id="messageBox">'+config.waiting_label+'</div>');

		if(path == 'history'){

			history.go(-1);

		}else{

			location.href = config.base_url+path;

		}

	//});

}

function formLoading(){

	$(document).ready(function() {

		$.blockUI('<div id="messageBox">'+config.waiting_label+'</div>');
		//document.forms[formName].submit();

	});

}

function confirmDelete(msg, formName, value){
	
	$(document).ready(function() {

		$.blockUI('<div id="confirmMsgSiNo"><br />' + msg + '<br /><input type="button" class="jsbutton" id="si" value="'+config.aLabel["JS_SI"]+'"/><input type="button" class="jsbutton" id="no" value="'+config.aLabel["JS_NO"]+'" /><br /><br /> </div>');

	});
	
	$(function() {


		$('#si').click(function() {
			
			document.forms[formName].elements["id"].value = value;
			document.forms[formName].submit();

		});

		$('#no').click($.unblockUI);
	});
	
}

function alertMsg(msg){
	
	$(document).ready(function() {

		$.blockUI('<div id="confirmMsgSiNo"><br />' + msg + '<br /><input type="button" class="jsbutton" id="ok" value="'+config.aLabel["JS_OK"]+'"/><br /><br /> </div>');

	});
	
	$(function() {

		$('#ok').click($.unblockUI);
		
	});
	
}

function backToForm(){
	formLoading();
	document.forms["backform"].submit();
}


function show_hide_home_box(id){
	
	if(id != 'box_fast_text'){
		document.getElementById('box_fast_text').style.display = "none";
	}
	if(id != 'box_secure_text'){
		document.getElementById('box_secure_text').style.display = "none";
	}
	if(id != 'box_save_text'){
		document.getElementById('box_save_text').style.display = "none";
	}
	if(id != 'box_results_text'){
		document.getElementById('box_results_text').style.display = "none";
	}
	
	stato_div_attuale = (document.getElementById(id).style.display == "block") ? "aperto" : "chiuso";

	if(stato_div_attuale == "chiuso") {

		document.getElementById(id).style.display = "block";
		scroll(0,document.body.clientHeight);		

	}else{

		document.getElementById(id).style.display = "none";

	}
}

function show_hide(id){

	stato_div_attuale = (document.getElementById(id).style.display == "block") ? "aperto" : "chiuso";

	if(stato_div_attuale == "chiuso") {

		document.getElementById(id).style.display = "block";		

	}else{

		document.getElementById(id).style.display = "none";

	}
}

var divSearchLabelState = 0;
function changeSearchLabel(id){
	
	var divSearchLabel = document.getElementById(id);
	
	if(divSearchLabelState == 0){
		
		divSearchLabel.innerHTML = config.aLabel["JS_CLOSE_SEARCH_BOX"];
		divSearchLabelState = 1;
		
	}else{
		
		divSearchLabel.innerHTML = config.aLabel["JS_OPEN_SEARCH_BOX"];
		divSearchLabelState = 0;
		
	}
	
}

function addAndSendVideos(){
		
	tot=document.add_video_form.id_video.length;

	var aSelected = new Array();

	if(isNaN(tot)){

		ogg_id_contact=document.forms.add_video_form.id_video;

		if(ogg_id_contact.checked == true){
			aSelected[0] = ogg_id_contact.value;
		}

	}else{


		for(cont=0;cont<tot;cont++){

			ogg_id_contact=document.forms.add_video_form.id_video[cont];

			if(ogg_id_contact.checked == true){
				aSelected[cont] = ogg_id_contact.value;
			}

		}
	}

	if(aSelected.length < 1){
		
		alertMsg(config.aLabel["JS_SELECT_A_VIDEO"]);

	}else{

		strOut = "";
		for(cont=0;cont<aSelected.length;cont++){
			strOut = strOut + aSelected[cont] + ",";
		}

		document.video_backform.id_videos.value = strOut;
		formLoading();
		document.video_backform.submit();

	}

}

/*
la funzione modifica i valori all'interno della contact_id_list
a partire dalla lista dei contatti nella pagina:

- se id presente nella lista, selezionato e presente nella textarea: nulla
- se id presente nella lista, selezionato e non presente nella textarea: aggiungi alla textarea
- se id presente nella lista, non selezionato e presente nella textarea: elimina dalla textarea


*/
function prepareIdContactList(){
	
	var contactIdList = document.addrress_book_form.contact_id_list;
	
	var stringIdContact = contactIdList.value;
	
	if(document.addrress_book_form.id_contact){
		
		tot=document.addrress_book_form.id_contact.length;
		
		if(isNaN(tot)){
		
			ogg_id_contact=document.forms.addrress_book_form.id_contact;
			
			//contatto selezionato
			if(ogg_id_contact.checked == true){
				
				//la stringa non è presente
				if(stringIdContact.indexOf(ogg_id_contact.value+";") == -1){
					stringIdContact = stringIdContact + ogg_id_contact.value+"; ";
				}
				
			}else{
				
				//la stringa è presente ma il contatto non è selezionato
				if(stringIdContact.indexOf(ogg_id_contact.value+";") != -1){
					stringIdContact = stringIdContact.replace(ogg_id_contact.value+";", "");
				}
				
				
			}
			
		}
		else{
			
			for(cont=0;cont<tot;cont++){
	
				ogg_id_contact=document.forms.addrress_book_form.id_contact[cont];
	
				if(ogg_id_contact.checked == true){				
					
					//la stringa non è presente
					if(stringIdContact.indexOf(ogg_id_contact.value+";") == -1){					
						stringIdContact = stringIdContact + ogg_id_contact.value+"; ";
					}
					
				}else{				
					
					//la stringa è presente ma il contatto non è selezionato				
					if(stringIdContact.indexOf(ogg_id_contact.value+";") != -1){
						stringIdContact = stringIdContact.replace(ogg_id_contact.value+";", "");
					}
					
					
				}
	
			}
			
		}
		
	}else{
		
	}
	
	
	
	return stringIdContact;
	
}

/*
la funzione seleziona i checkbox dei contatti presenti 
*/
function makeChekedFromContactIdList(){
	
	var contactIdList = document.addrress_book_form.contact_id_list;
	
	var stringIdContact = contactIdList.value;
	
	if(document.addrress_book_form.id_contact){
		
		tot=document.addrress_book_form.id_contact.length;
	
		if(isNaN(tot)){
			
			ogg_id_contact=document.forms.addrress_book_form.id_contact;
			
			if(stringIdContact.indexOf(ogg_id_contact.value+";") != -1){
				ogg_id_contact.checked = true;
			}
			
		}else{
			
			for(cont=0;cont<tot;cont++){
				
				ogg_id_contact=document.forms.addrress_book_form.id_contact[cont];
				
				if(stringIdContact.indexOf(ogg_id_contact.value+";") != -1){
					ogg_id_contact.checked = true;
				}
				
			}
			
		}
		
	}else{
		
	}
	
}

function addAndSendContacts(){
		
	stringIdContact = prepareIdContactList();
	
	document.contacts_backform.contact_id_list.value = stringIdContact;	
	formLoading();
	document.contacts_backform.submit();
	
}

function goToAddressBookPage(i){
	
	formLoading();
	
	stringIdContact = prepareIdContactList();
	document.addrress_book_form.contact_id_list.value = stringIdContact;
	
	document.addrress_book_form.pagenum.value = i;
	document.addrress_book_form.submit();
	
}

function editContact(i){
	
	formLoading();
	
	stringIdContact = prepareIdContactList();
	document.edit_contact.contact_id_list.value = stringIdContact;
	
	document.edit_contact.id_contact.value = i;
	document.edit_contact.submit();
	
}


function OLD_addAndSendContacts(){

	tot=document.addrress_book_form.id_contact.length;

	var aSelected = new Array();
	var aUnSelected = new Array();

	if(isNaN(tot)){

		ogg_id_contact=document.forms.addrress_book_form.id_contact;

		if(ogg_id_contact.checked == true){
			aSelected[0] = ogg_id_contact.value;
		}else{
			aUnSelected[0] = ogg_id_contact.value;
		}

	}else{
		
		contSel = 0;
		contUnSel = 0;
		
		for(cont=0;cont<tot;cont++){

			ogg_id_contact=document.forms.addrress_book_form.id_contact[cont];

			if(ogg_id_contact.checked == true){				
				aSelected[contSel] = ogg_id_contact.value;
				contSel++;
			}else{				
				aUnSelected[contUnSel] = ogg_id_contact.value;
				contUnSel++;
			}

		}
	}

	strSelected = "";
	for(cont=0;cont<aSelected.length;cont++){
		strSelected = strSelected + aSelected[cont] + ",";
	}

	strUnSelected = "";
	for(cont=0;cont<aUnSelected.length;cont++){
		strUnSelected = strUnSelected + aUnSelected[cont] + ",";
	}

	document.contacts_backform.id_contacts_to_add.value = strSelected;
	document.contacts_backform.id_contacts_to_delete.value = strUnSelected;
	formLoading();
	document.contacts_backform.submit();

	
}


var formEmailChanged = false;
function onFormChange(){
	
	//alert("onFormChange");
	formEmailChanged = true;
	
}


function addContactToEmail(msg){
	
	if(formEmailChanged){
		
		$(document).ready(function() {

			$.blockUI('<div id="confirmMsgSiNo"><br />'+msg+'<br /><input type="button" class="jsbutton" id="si" value="'+config.aLabel["JS_SI"]+'"/><input type="button" class="jsbutton" id="no" value="'+config.aLabel["JS_NO"]+'" /><br /><br /> </div>');

		});

		$(function() {


			$('#si').click(function() {

				document.form_message.flow_type.value = "EDIT_CONTACT";
				document.form_message.submit();

			});
			
			$('#no').click(function() {

				formLoading();
				document.contact_add.submit();

			});

			
		});
		
	}else{
		
		formLoading();
		document.contact_add.submit();
	
	}
		
}


function addRightToEmail(msg){
	
	if(formEmailChanged){
		
		$(document).ready(function() {

			$.blockUI('<div id="confirmMsgSiNo"><br />'+msg+'<br /><input type="button" class="jsbutton" id="si" value="'+config.aLabel["JS_SI"]+'"/><input type="button" class="jsbutton" id="no" value="'+config.aLabel["JS_NO"]+'" /><br /><br /> </div>');

		});

		$(function() {


			$('#si').click(function() {
				
				document.form_message.flow_type.value = "ADD_RIGHT";
				document.form_message.submit();

			});
			
			$('#no').click(function() {

				formLoading();
				document.right_add.submit();

			});

			
		});
		
	}else{
		
		formLoading();
		document.right_add.submit();
	
	}
		
}


function confirmSendEmail(msg, msg2){
	
	if(formEmailChanged){
		
		$(document).ready(function() {

			$.blockUI('<div id="confirmMsgSiNo"><br />'+msg2+'<br /><input type="button" class="jsbutton" id="si" value="'+config.aLabel["JS_SI"]+'"/><input type="button" class="jsbutton" id="no" value="'+config.aLabel["JS_NO"]+'" /><br /><br /> </div>');

		});

		$(function() {


			$('#si').click(function() {
				
				document.form_message.flow_type.value = "SEND_EMAIL";
				document.form_message.submit();

			});
			
			$('#no').click($.unblockUI);

			
		});
		
		
	}else{
		
		$(document).ready(function() {

			$.blockUI('<div id="confirmMsgSiNo"><br />' + msg + '<br /><input type="button" class="jsbutton" id="si" value="'+config.aLabel["JS_SI"]+'"/><input type="button" class="jsbutton" id="no" value="'+config.aLabel["JS_NO"]+'" /><br /><br /> </div>');

		});

		$(function() {


			$('#si').click(function() {

				document.email_send.submit();

			});

			$('#no').click($.unblockUI);
		});
		
	}
	
	
	
}

function postForm(formName){
	
	formLoading();
	
	$('#'+formName+'').submit();
	
}


function editRight(i){
		
	formLoading();
	document.right_edit.id_movie_right.value = i;
	document.right_edit.submit();		
}




function goToMarketPlacePage(i){
	
	formLoading();
	document.market_place_form.pagenum.value = i;
	document.market_place_form.submit();
	
}

function submitSearchForm(formId, i){
	
	formLoading();
	
	var form = document.getElementById(formId);
	form.pagenum.value = i;
	form.submit();
	
}

function goToEmailStat(i){
	
	formLoading();
	
	document.go_to_email_stat.id.value = i;
	document.go_to_email_stat.submit();
	
}

function submitSearchFormWithAction(formId, formAction, i){
	
	formLoading();
	
	var form = document.getElementById(formId);
	
	form.action = formAction+i;
	form.submit();
	
}

function submitFormWithParam(formId, formAction, paramName, i){
	
	formLoading();
	
	var form = document.getElementById(formId);
	var element = document.getElementById(paramName);
	
	form.action = formAction;
	element.value = i;

	form.submit();
	
}

function showCvvHelp(){
	
	$(document).ready(function() {

		$.blockUI('<div id="confirmMsgSiNo"><br /><br /><img src="img/cvv_card_sm.gif" /><br /><br /><input type="button" class="jsbutton" id="ok" value="'+config.aLabel["JS_OK"]+'"/><br /><br /> </div>');

	});

	$(function() {


		$('#ok').click($.unblockUI);


	});
		
}

