// openWindow('urlPage', 'namePopup', 'width', 'height', 'yes/no', 'yes/no');
function openWindow(mypage,myname,w,h,resizable,scrollbars){ 
	w = (Number(w) + 1);
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0; 
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0; 
	settings = 'scrollbars='+scrollbars+',resizable='+resizable+',status=no,height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition;
	win = window.open(mypage,myname,settings);
}

// preload images
// simplePreload( '01.gif', '02.gif' );
function simplePreload(){
  	 var args = simplePreload.arguments;
  	 document.imageArray = new Array(args.length);
  	 for(var i=0; i<args.length; i++)
  	 {
    	 document.imageArray[i] = new Image;
    	 document.imageArray[i].src = args[i];
  	 }
}
	 
// switch images
// switchImage( 'nomeImg', '01.gif' );
function switchImage(imgName, imgSrc){
	if (document.images){
		if (imgSrc != ''){
		 document.images[imgName].src = imgSrc;
		}
	}
}

function addOption(Sel, theText, theValue, alt, key){
	var theSel = document.getElementById(Sel+'');
	var check = true;
	for( var i=theSel.length-1; i>=0; i-- ){
		 if( theSel.options[i].value == theValue ){
			 if( alt ){
			 	 alert("Elemento già presente nella lista!");
			 }else{
			 	 theSel.options[i] = null;
				 win.getId(key).style.color='#000000';
			 }
		 	 check = false;
			 return false;
		 }
	}
	if( check ){
		var newOpt = new Option(theText, theValue);
		theSel.options[theSel.length] = newOpt;
		if( key )
			win.getId(key).style.color='#CC0000';
		
		return true;
	}
}
function deleteOption(Sel, theIndex){
	var theSel = document.getElementById(Sel+'');
	if( theSel.length>0 )
		theSel.options[theIndex] = null;
}
function deleteOptions(From){
	var theSelFrom = document.getElementById(From+'');
	for( var i=theSelFrom.length-1; i>=0; i-- ){
		 if( theSelFrom.options[i].selected )
			 deleteOption(From, i);
	}
}
function checkGoto(Sel, theValue, key){
	var theSel = window.opener.document.getElementById(Sel+'');
	for( var i=theSel.length-1; i>=0; i-- ){
		 if( theSel.options[i].value == theValue )
			 getId(key).style.color='#CC0000';
	}
}
function selectAll(Sel){
	var theSel = document.getElementById(Sel+'');
	if( !theSel.options.length && document.getElementById('st1').checked == false ){
		document.getElementById('dv1').checked = 1;
		document.getElementById('sd1').checked = 1;
	}
	
	for( i=0; i<theSel.options.length; i++ ){
		 theSel.options[i].selected = 1;
	}
	return true;
}

function getCookie(NameOfCookie){
	if (document.cookie.length > 0) { 
		begin = document.cookie.indexOf(NameOfCookie+"="); 
	if (begin != -1){ 
		begin += NameOfCookie.length+1; 
		end = document.cookie.indexOf(";", begin);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(begin, end)); } 
	}
	return null; 
}
function setCookie(NameOfCookie, value){
	document.cookie = NameOfCookie + "=" + escape(value) + ";";
}

function confirm_action(){
	if( !confirm('Sei sicuro di voler continuare?') )
		return false;
}

function toggle_id(element_id,cookie_var) {
    if( getId(element_id).style.display=='' ){
		getId(element_id).style.display='none';
        
        if( getId("img_"+element_id) ) {
            getId("img_"+element_id).src="templates/images/ico_espandi.png";
        }
        
        setCookie(cookie_var,2);
	} else {
        getId(element_id).style.display='';
        
        if( getId("img_"+element_id) ) {
            getId("img_"+element_id).src="templates/images/ico_riduci.png";
        }
        
        setCookie(cookie_var,1);
	}
}

function load_toggle_state(element_id,cookie_to_check) {
    if(!getCookie(cookie_to_check) || getCookie(cookie_to_check)==1) {
        getId(element_id).style.display='';
        
        if(getId("img_"+element_id)) {
            getId("img_"+element_id).src="templates/images/ico_riduci.png";
        }
    }
    else if(getCookie(cookie_to_check)==2) {
        getId(element_id).style.display='none';
        
        if(getId("img_"+element_id)) {
            getId("img_"+element_id).src="templates/images/ico_espandi.png";
        }
    }
}

function open_view(){
	if( getId('w_cont').style.display=='' ){
		getId('w_cont').style.display='none';
		setCookie('view', 0);
		getId('w_img').src='templates/images/ico_espandi.png';
	}else{
		getId('w_cont').style.display='';
		setCookie('view', 1);
		getId('w_img').src='templates/images/ico_riduci.png';
	}
}

function reset_icon(key, tot){
	for( i=0; i<=tot; i++ ){
		 getId(key+'_ico'+i).style.borderColor='#FFFFFF';
	}
}

function show_view(){
	if( !getCookie('view') || getCookie('view') == 1 ){
		getId('w_img').src='templates/images/ico_riduci.png';
		getId('w_cont').style.display='';
	}else{
		getId('w_img').src='templates/images/ico_espandi.png';
		getId('w_cont').style.display='none';
	}
}

function goto(url){
	location.href=url;
}

function redirect(redirect){
	setTimeout("location.href='"+redirect+"'", 2000);
}
function check_ip( ip ){
	var reg_exp = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ip_split = ip.split(".");

	if( !reg_exp.test(ip) )
		 return false;
	else if( ip_split[0] > 255 || ip_split[1] > 255 || ip_split[2] > 255 || ip_split[3] > 255 )
		 return false;
	else
		 return true;
}
function returnSelection(radioButton) {
	var selection=null;
	for(var i=0; i<radioButton.length; i++) {
		 if(radioButton[i].checked) {
		   selection=radioButton[i].value;
		   return selection;
		 }
	}
	return selection; 
} 

function getId(id){
	var element = document.getElementById(id+"");	
	return element;
}

function setField(form, field, val){
	document.forms[form].elements[field].value = val;		
}

function saveForm(form){		
	document.forms[form].submit();
}

function stampDate(){
	var months = new Array(12);
	months[0]  = "gennaio";
	months[1]  = "febbraio";
	months[2]  = "marzo";
	months[3]  = "aprile";
	months[4]  = "maggio";
	months[5]  = "giugno";
	months[6]  = "luglio";
	months[7]  = "agosto";
	months[8]  = "settembre";
	months[9]  = "ottobre";
	months[10] = "novembre";
	months[11] = "dicembre";
	
	var days = new Array(6);
	days[0]  = "domenica";
	days[1]  = "lunedì";
	days[2]  = "martedì";
	days[3]  = "mercoledì";
	days[4]  = "giovedì";
	days[5]  = "venerdì";
	days[6]  = "sabato";
	
	var timestamp = new Date();
	
	var lday      = days[timestamp.getDay()];
	var day       = timestamp.getDate();
	var lmonth    = months[timestamp.getMonth()];
	var year      = timestamp.getYear();
	
	if ( navigator.appName == "Netscape" )
		 year=1900 + year;
	
	document.write(lday+", "+day+" "+lmonth+" "+year);
}

function stampClock(){
	var clock_time = new Date();
	var clock_hours = clock_time.getHours();
	var clock_minutes = clock_time.getMinutes();
	var clock_seconds = clock_time.getSeconds();
	
	if (clock_hours < 10){
		clock_hours = "0" + clock_hours;
	}
	if (clock_minutes < 10){
		clock_minutes = "0" + clock_minutes;
	}
	if (clock_seconds < 10){
		clock_seconds = "0" + clock_seconds;
	}
	
	var clock_div = document.getElementById('clock');
	clock_div.innerHTML = clock_hours + ":" + clock_minutes + ":" + clock_seconds;
	
	setTimeout("stampClock()", 1000);
}
