//**************************************
// Global

////////////////////////////////////////
// Funzioni Generiche
function popit(cosa){
	newwindow=window.open(cosa,"News","width=550,height=350,scrollbars=yes");
	newwindow.creator=self;
	newwindow.focus();
}
function popit_Aziende(cosa){
	newwindow=window.open(cosa,"Aziende","width=500,height=500,scrollbars=yes");
	newwindow.creator=self;
	newwindow.focus();
}
function popitProdStruct(cosa){
	newwindow=window.open(cosa,"Product","width=550,height=550,scrollbars=yes");
	newwindow.creator=self;
	newwindow.focus();
}
function changeImgSrc(idImg) {   
	var img = $(idImg);
	var src = img.src; 
	var imgOn = "_on.gif", imgOff = "_off.gif"; 
	var index = src.indexOf("_on.gif"); 
	if (index >= 0) {
		src = src.substr(0, index) + imgOff;
	}
	else {
		index = src.indexOf("_off.gif");
		src = src.substr(0, index) + imgOn;
	}
	//alert(src);
	img.src = src;
}
////////////////////////////////////////
// Funzioni Apparizione/Scomparsa di DIV
function toggleDiv(ID, ID2, rel){
	if (document.getElementById(ID).style.display=='none'){
		document.getElementById(ID).style.display='block'
	}
	else{
		document.getElementById(ID).style.display='none'
	}

	if (document.getElementById(ID2).src==rel+'images/categorie/minus.gif'){
		document.getElementById(ID2).src=rel+'images/categorie/plus.gif'
	}
	else{
		document.getElementById(ID2).src=rel+'images/categorie/minus.gif'
	}
	return false;
}
//**************************************
// Report

////////////////////////////////////////
// Funzioni Area Report
function inviaForm(op){
	document.operazioni.operazione.value=op;
	document.operazioni.submit();
}

//**************************************
// Filebase

////////////////////////////////////////
// CheckAll per checkboxes (Filebase)
var checkflag = "false";
function check(field) {
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++) {
		field[i].checked = true;}
		checkflag = "true";
		return "Uncheck all";
		}
	else {
		for (i = 0; i < field.length; i++) {
		field[i].checked = false; }
		checkflag = "false";
		return "Check all";
		}
}

//**************************************
// Products

////////////////////////////////////////
// Compare functions
function checkMaxCompare(){
	var contatore=0;
	for (i = 0; i < document.prodotti.controllo.length; i++){
		if (document.prodotti.controllo[i].checked==true){
			contatore+=1;
			if (contatore>4){
				alert("Max 4 prod.");
				//document.getElementById("prodCompCount").innerHTML = contatore;
				return false;
			}
		}
	}
	document.getElementById("prodCompCount").innerHTML = contatore;
	return true;
}

function checkMinCompare(){
	var contatore=0;
	for (i = 0; i < document.prodotti.controllo.length; i++){
		if (document.prodotti.controllo[i].checked==true) contatore+=1;
	}
	if (contatore>1){
		return true;
	}	else {
		return false;
	}
}

//**************************************
// Cart

////////////////////////////////////////
// setOptAll imposta allo stesso valore tutti i campi dell'opzione
function setOptAll(formToUse, fieldNPrefix, valueToSet)
{
if(valueToSet != '') {
	var selecter
	var lenFieldNPrefix = fieldNPrefix.length;
	var lenFieldN;
	// get the form elements
	var el = formToUse.elements;
	// loop through the elements...
	for(i=0;i<el.length;i++) {
		lenFieldN = el[i].name.length;
		if(Left(el[i].name, lenFieldNPrefix) == fieldNPrefix) {
			// Qui sotto vado a costruire il nome del checkbox sulla colonna di sinistra che si riferisce alla texbox in esame
			selecter = document.getElementById('selected'+(Right(el[i].name, (lenFieldN-lenFieldNPrefix))));
			if(selecter.checked == true) {
				el[i].value = valueToSet;
				el[i].style.backgroundColor = '#FF9900';
				}    
			}
		}
	}
}

////////////////////////////////////////
// CheckAll per checkboxes con nomi misti (Cart)
function checkAll(form1, exby)
{
var frm = form1;
var el = frm.elements;
for(i=0;i<el.length;i++) {
  if(el[i].type == "checkbox" ) {
    el[i].checked = exby.checked? true:false;
    }
  }
}
////////////////////////////////////////
// ValidateNumber in cart
function validateNumber(field){
  var val = field.value;
	//if(!/^\d*$/.test(val)){
	//if(!/^((\d+(\,\d*)?)|((\d*\,)?\d+))$/.test(val)){
	if(!/^(((\+|\-)?\d+(\,\d*)?)|(((\+|\-)?\d*\,)?\d+))$/.test(val)){
		alert("Valore non valido");
		field.focus();
		field.select();
		}
}
function validateNumberAndSubmit(field, form){
  var val = field.value;
	//if(!/^\d*$/.test(val)){
	//if(!/^((\d+(\,\d*)?)|((\d*\,)?\d+))$/.test(val)){
	if(!/^(((\+|\-)?\d+(\,\d*)?)|(((\+|\-)?\d*\,)?\d+))$/.test(val)){
		alert("Valore numerico non valido");
		field.focus();
		field.select();
		return false;
	}
	else{
		form.submit();
		return true;
	}
}

function validateAndDiscount(field, form){
  var val = field.value;
  if(!/^\d*$/.test(val)){
      alert("Valore numerico non valido");
      field.focus();
      field.select();
			return false;
	}
	else{
		form.selAll.checked = true
		checkAll(form, form.selAll);
		setOptAll(form, 'discount', val);
		form.submit();
		return true;
	}
}
////////////////////////////////////////
// Used on onBlur to submit
function takeAndSubmit(form){
	var frm = form
	frm.submit();
	return true;
}
///////////////////////////////////////
//  check for valid numeric strings
function IsNumeric(strString)
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
		{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
			 {
			 blnResult = false;
			 }
		}
	return blnResult;
}

////////////////////////////////////////
// Used by setOptAll function
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//**************************************
// Gneric AJAX

////////////////////////////////////////
// Generic Ajax function (mootools 0.83 compatibile anche con 1.00)

function callAjaxPage(pageToCall, divToUpdate, parToPost){	
	showLoading();
	var myAjax = new Ajax(pageToCall, { postBody: parToPost, 
																			onComplete: function() {
																			 													//the function below is defined in Main-Body.asp
																																taskContainerEffect.show();
																																showLoading();
																			 													},
																			update: $(divToUpdate), 
																			evalScripts: true	} );
	myAjax.request();
}

function showLoading() {
    var l = $('loading');
    if(l.style.display == 'none') l.style.display = 'block';
    else l.style.display = 'none';
}

//**************************************
// FCKeditor API

////////////////////////////////////////
// The serialize functions doesn't work with FCKeditor
function exportEditorInnerHTML(fromEditor, toField){
	oEditor = FCKeditorAPI.GetInstance(fromEditor);
	$(toField).value = unescape(oEditor.GetXHTML(oEditor.FormatOutput));
}

