function deleteNews(id) {
	var result = confirm("Sei sicuro di voler cancellare la notizia con l'id (" + id + ") ?");
    if (result) {
		document.deleteForm.deleteNewsId.value = id;
		document.deleteForm.submit();
	}
}

function deleteMenu(id) {
	var result = confirm("Sei sicuro di voler cancellare il menu con l'id (" + id + ") ?");
    if (result) {
		document.deleteForm.deleteMenuId.value = id;
		document.deleteForm.submit();
	}
}
function deletePage(id) {
	var result = confirm("Sei sicuro di voler cancellare la pagina con l'id (" + id + ") ?");
    if (result) {
		document.deleteForm.deletePageId.value = id;
		document.deleteForm.submit();
	}
}


function checkNewsForm() {
	var result = true;
	if (document.newsForm.newsName.value == '') {
		result = false;
		alert("Name is mandatory field!");
		document.newsForm.newsName.focus();
	} else if (document.newsForm.newsHeadline.value == '') {
		result = false;
		alert("Headline is mandatory field!");
		document.newsForm.newsHeadline.focus();
	} else if (document.newsForm.date_date.value     == '') {
		result = false;
		alert("Date is mandatory field!");
		document.newsForm.date_date.focus();
	}else if (document.newsForm.newsAbstract.value == '') {
		result = false;
		alert("Abstract is mandatory field!");
		document.newsForm.newsAbstract.focus();
	}else if (document.newsForm.newsBody.value     == '') {
		result = false;
		alert("Body is mandatory field!");
		document.newsForm.newsBody.focus();
	}else if (checkDate(document.newsForm.newsDate.value)) {
		result = false;
		alert("Date is not in valid format!");
		document.newsForm.newsDate.focus();
	}
	
	return result;
}


function checkDate(dateString) {
	
	//yyyy-mm-dd hh:mm:ss

	var re;
    // Rules for the date regular expression:
    // The format of the date is:
    // dd/mm/yyyy
    // Days: Valid between 1 and 31
    // The first digit may either be a 3 followed by a 0 or a 1 
    // or it could be a 0 followed by 1 to 9, or a 1 or 2, 
    // followed by any digit.
    // Then follows a backslash
    // Months: Valid between 1 and 12
    // The month may be a 0 followed by 1 to 9, or a 1 followed by 
    // either a 0, 1 or 2. 
    // Then follows a backslash
    // The year must comprise of any 4 digits.
    //re = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
    re = /^(\d{4})/(0[1-9]|1[012])/(3[01]|0[1-9]|[12]);
    //re = /^(\d{4})-(0[1-9]|1[012])-(3[01]|0[1-9]|[12]\d)\b([01]\d|2[0-3]):([0-5]\d):([0-5]\d)/;
    //re = /^(\d{4})-(0[1-9]|1[012])-(3[01]|0[1-9]|[12]\d) ([01]\d|2[0-3]):([0-5]\d):([0-5]\d)/;
    //if (dateString.length != 19 || re.test(dateString) == false) {
   	if (re.test(dateString) == false) {
    	return false;
    } else {
        return true;
	}
}


function checkPageForm() {
	if (document.pageForm.dpageImg1 != undefined) {
		document.pageForm.pageImg1.value = document.pageForm.dpageImg1.value;
		updateRTE('pageContent1');
	}	

	if (document.pageForm.dpageImg2 != undefined) {
		document.pageForm.pageImg2.value = document.pageForm.dpageImg2.value;
		updateRTE('pageContent2');
	}	

	if (document.pageForm.dpageImg3 != undefined) {
		document.pageForm.pageImg3.value = document.pageForm.dpageImg3.value;
		updateRTE('pageContent3');
	}	

	if (document.pageForm.dpageImg4 != undefined) {
		document.pageForm.pageImg4.value = document.pageForm.dpageImg4.value;
		updateRTE('pageContent4');
	}	

	if (document.pageForm.dpageImg5 != undefined) {
		document.pageForm.pageImg5.value = document.pageForm.dpageImg5.value;
		updateRTE('pageContent5');
	}		
	return true;
}


function insertUploadImage(siteRoot, formName, fieldName, rootDir) {
	var imagePath = siteRoot + "/backend/include/addImage.php?field=" + fieldName + "&frmName=" + formName;
	if (rootDir != '') {
		imagePath += "&rootDir=" + rootDir;
	}
	imagewindow = window.open(imagePath, "image", "width=340,height=180");
}

function changeLayoutId(id) {

	if (document.pageForm.dpageImg1 != undefined) {
//		alert("page1 exists");
		document.pageForm.pageImg1.value = document.pageForm.dpageImg1.value;
		updateRTE('pageContent1');
	}	

	if (document.pageForm.dpageImg2 != undefined) {
//		alert("page2 exists");
		document.pageForm.pageImg2.value = document.pageForm.dpageImg2.value;
		updateRTE('pageContent2');
		
	}	

	if (document.pageForm.dpageImg3 != undefined) {
//		alert("page3 exists");
		document.pageForm.pageImg3.value = document.pageForm.dpageImg3.value;
		updateRTE('pageContent3');
		
	}	

	if (document.pageForm.dpageImg4 != undefined) {
//		alert("page4 exists");
		document.pageForm.pageImg4.value = document.pageForm.dpageImg4.value;
		updateRTE('pageContent4');
		
	}	

	if (document.pageForm.dpageImg5 != undefined) {
//		alert("page5 exists");
		document.pageForm.pageImg5.value = document.pageForm.dpageImg5.value;
		updateRTE('pageContent5');
		
	}	
	
	
	document.pageForm.action = "addPage.php";
	document.pageForm.pageTemplateId.value = id;
	document.pageForm.submit();
}

function checkMenuForm() {
	var result = true;
	if (document.menuForm.menuName.value == '') {
		result = false;
		alert("Name is mandatory field!");
		document.menuForm.menuName.focus();
	} else if (document.newsForm.menuText.value == '') {
		result = false;
		alert("Text is mandatory field!");
		document.menuForm.menuText.focus();
	} else if (document.menuForm.menuOrder.value     == '') {
		result = false;
		alert("Menu order is mandatory field!");
		document.menuForm.menuOrder.focus();
	}
	return result;
}

/////////////////////////////////////////////////////////
// menu tree functions
/////////////////////////////////////////////////////////

/**
* Needed to initialize the tree.
* And to call showTree(imagePath); to actually show the tree.
* Alternatively this can be done in a script block at the bottom of the page.
* Though this method is somewhat cleaner.
*/
function initTree(path) {
	container = document.getElementById('examplediv');
	showTree(path);
}

/**
* Called when a user clicks on a node.
* @param treeNode the TreeNode object which have been clicked.
*/
function standardClick(treeNode) {
	var mytext = document.getElementById('mytext');
	var param = treeNode.getParam();
	mytext.innerHTML = (param == '') ? treeNode.getName() : param;
	//alert (treeNode.getID());
	document.menuSelectionForm.id.value=treeNode.getID();
//	document.menuSelectionForm.submit();
}













  function returnDate() {
    var this_month = new Array(12);
    this_month[0]  = "Gennaio";
    this_month[1]  = "Febbraio";
    this_month[2]  = "Marzo";
    this_month[3]  = "Aprile";
    this_month[4]  = "Maggio";
    this_month[5]  = "Giugno";
    this_month[6]  = "Luglio";
    this_month[7]  = "Agosto";
    this_month[8]  = "Settembre";
    this_month[9]  = "Ottobre";
    this_month[10] = "Novembre";
    this_month[11] = "Dicembre";

    var this_day = new Array(7);
    this_day[0]  = "Domenica";
    this_day[1]  = "Lunedi'";
    this_day[2]  = "Martedi'";
    this_day[3]  = "Mercoledi'";
    this_day[4]  = "Giovedi'";
    this_day[5]  = "Venerdi'";
    this_day[6]  = "Sabato";

    var today = new Date();
    var day   = today.getDate();
    var month = today.getMonth();
    var year  = today.getYear();
    var weekday = today.getDay();
    if (year< 1900) 
      year += 1900;
    SrcStr = (this_day[weekday]+", "+day+" "+this_month[month]+", "+year);
    document.write(SrcStr);	  
  }





  function returnDate_IT() {
    var this_month = new Array(12);
    this_month[0]  = "Gennaio";
    this_month[1]  = "Febbraio";
    this_month[2]  = "Marzo";
    this_month[3]  = "Aprile";
    this_month[4]  = "Maggio";
    this_month[5]  = "Giugno";
    this_month[6]  = "Luglio";
    this_month[7]  = "Agosto";
    this_month[8]  = "Settembre";
    this_month[9]  = "Ottobre";
    this_month[10] = "Novembre";
    this_month[11] = "Dicembre";

    var this_day = new Array(7);
    this_day[0]  = "Domenica";
    this_day[1]  = "Lunedi'";
    this_day[2]  = "Martedi'";
    this_day[3]  = "Mercoledi'";
    this_day[4]  = "Giovedi'";
    this_day[5]  = "Venerdi'";
    this_day[6]  = "Sabato";

    var today = new Date();
    var day   = today.getDate();
    var month = today.getMonth();
    var year  = today.getYear();
    var weekday = today.getDay();
    if (year< 1900) 
      year += 1900;
    SrcStr = (this_day[weekday]+", "+day+" "+this_month[month]+", "+year);
    document.write(SrcStr);	  
  }




  function returnDate_EN() {
    var this_month = new Array(12);
    this_month[0]  = "January";
    this_month[1]  = "February";
    this_month[2]  = "March";
    this_month[3]  = "April";
    this_month[4]  = "May";
    this_month[5]  = "June";
    this_month[6]  = "July";
    this_month[7]  = "August";
    this_month[8]  = "September";
    this_month[9]  = "October";
    this_month[10] = "November";
    this_month[11] = "December";

    var this_day = new Array(7);
    this_day[0]  = "Sunday";
    this_day[1]  = "Monday";
    this_day[2]  = "Tuesday";
    this_day[3]  = "Wednesday";
    this_day[4]  = "Thursday";
    this_day[5]  = "Friday";
    this_day[6]  = "Saturday";

    var today = new Date();
    var day   = today.getDate();
    var month = today.getMonth();
    var year  = today.getYear();
    var weekday = today.getDay();
    if (year< 1900) 
      year += 1900;
    SrcStr = (this_day[weekday]+", "+day+" "+this_month[month]+", "+year);
    document.write(SrcStr);	  
  }






function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function checkForm() {
	return true;
}

function checkFormEN() {
	return true;
}

function doCheck( frmName, required, names, messageString) {
    var result = -1;
	for (i =0; i < required.length; i++) {
	    var elType = eval("document." + frmName +"." + required[i] + ".type");
		if (elType == "text") {
			val = eval("document." + frmName +"." + required[i] + ".value");
			if (val == '') {
				result = i;
			    	break;
			}
		} else if (elType == "textarea") {
			val = eval("document." + frmName +"." + required[i] + ".value");
			if (val == '') {
				result = i;
			    	break;
			}
		} else if (elType == "password") {
			val = eval("document." + frmName +"." + required[i] + ".value");
			if (val == '') {
				result = i;
			    break;
			}
		} else if (elType == "select-one") {
		    val = eval("document." + frmName +"." + required[i] + ".options[" + "document." + frmName +"." + required[i] + ".selectedIndex].value");
		    if (val == '0') {
		    	result = i;
		    	break;
		    }
		} else if (elType == "radio") {
			var radioLen = eval("document." + frmName +"." + required[i] + ".length");
			var val  = '';
			for (var i = 0; i < radioLen; i++) {
				singleVal = eval("document." + frmName +"." + required[i] + "[" + i +"].value");
				val += singleVal;
				alert('i=' + singleVal+ "/"+ val);
			}
		    if (val == '') {
		    	result = i;
		    	break;
		    }
		}
	}
	if (result != -1) {
		eval("document." + frmName +"." + required[result] + ".focus()");
		msg = messageString.replace(/\{fld\}/g, names[result])
		//alert("Field \'" + names[result] + "\' is mandatory!");
		alert(msg);
		return false;
	}
	return true;
}

function checkForm() {
	var required = new Array();
	required[0] = 'fname';
	required[1] = 'lname';
	required[2] = 'firm';
	required[3] = 'adress';
	required[4] = 'cap';
	required[5] = 'city';
	required[6] = 'phone';
	required[7] = 'fax';
	required[8] = 'comment';

	var names = new Array();
	names[0] = 'Nome';
	names[1] = 'Cognome';
	names[2] = 'Nome azienda';
	names[3] = 'Indirizzo';
	names[4] = 'CAP';
	names[5] = 'Citta\'';
	names[6] = 'Telefono';
	names[7] = 'Fax';
	names[8] = 'Informazioni richieste';

	return doCheck('mailer', required, names, "Campo '{fld}' e' obbligatorio!");
}

function checkFormEN() {
	var required = new Array();
	required[0] = 'fname';
	required[1] = 'lname';
	required[2] = 'firm';
	required[3] = 'adress';
	required[4] = 'cap';
	required[5] = 'city';
	required[6] = 'country';
	required[7] = 'phone';
	required[8] = 'fax';
	required[9] = 'comment';

	var names = new Array();
	names[0] = 'First Name';
	names[1] = 'Surname';
	names[2] = 'Company Name';
	names[3] = 'Address';
	names[4] = 'Post Code';
	names[5] = 'City';
	names[6] = 'State';
	names[7] = 'Tel';
	names[8] = 'Fax';
	names[9] = 'Information required';

	return doCheck('mailer', required, names, "Field '{fld}' is mandatory !");
	
}

