/* Las siguientes funciones son comunes a todos los ficheros de las estadísticas del FO */
// Esta función valida un CIF. Devuelve true si el formato es correcto. False en caso contrario.
function validaCIF(cadena) {
    var re = new RegExp("^[A-Za-z]\\d{7}[0-9A-Za-z]$");

    if(!re.test(cadena)) {
        return false;
    }

    return true;
}

function checkAll(form){form.Logins.checked=true;form.NewUsers.checked=true;form.NewReferences.checked=true;form.Exports.checked=true;form.Imports.checked=true;form.Searches.checked=true;form.Captures.checked=true;form.CFBibliography.checked=true;form.CTLibrary.checked=true;form.TotalPagesServed.checked=true;}
function checkNone(form){form.Logins.checked=false;form.NewUsers.checked=false;form.NewReferences.checked=false;form.Exports.checked=false;form.Imports.checked=false;form.Searches.checked=false;form.Captures.checked=false;form.CFBibliography.checked=false;form.CTLibrary.checked=false;form.TotalPagesServed.checked=false;}
function showChecking(){var elem=document.getElementById('checking');elem.style.display="inline";}
function hideChecking(){var elem=document.getElementById('checking');elem.style.display="none";}
function sortFuncAsc(record1, record2) {var value1 = record1.optText.toLowerCase();var value2 = record2.optText.toLowerCase();if (value1 > value2) return(1);if (value1 < value2) return(-1);return(0);}
function sortSelect(selectToSort, ascendingOrder) {
            var myOptions = [];
            for (var loop=0; loop<selectToSort.options.length; loop++) {
                myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
            }
            myOptions.sort(sortFuncAsc);
            selectToSort.options.length = 0;
            for (loop=0; loop<myOptions.length; loop++) {
                var optObj = document.createElement('option');
                optObj.text = myOptions[loop].optText;
                optObj.value = myOptions[loop].optValue;
                selectToSort.options.add(optObj);
            }
}
function removeData(theSel) {
			var selLength = theSel.length;
			for (var i=selLength-1; i>=0; i--) {
                theSel.options[i]=null;
            }
}
function removeDataSelected(theSel) {
		var selLength = theSel.length;

		for(var i = (selLength - 1); i >= 0; i--) {
			if(theSel.options[i].selected) {
				theSel.options[i] = null;
			}
		}
}
function addFirstElement(theSel) {
			var newOpt = new Option("Todas", "Todas");
			var selLength = theSel.length;
  			theSel.options[selLength]=newOpt;
			for (var i=selLength-1; i>=0; i--) {
				var tmpOpt=theSel.options[i];
   				theSel.options[i+1]=new Option(tmpOpt.text,tmpOpt.value);
            }
  			theSel.options[0]=newOpt;
  			theSel.options[0].selected=true;
}
function selPostValue(theSel,val) {
			var selLength = theSel.length;
			var m = val.split(",");
			for (var i=selLength-1; i>=0; i--) {theSel.options[i].selected=false;}
			for (var j=0; j<m.length; j++) {
					for (i=selLength-1; i>=0; i--) {
		   				if (theSel.options[i].value==m[j]) {
		   						theSel.options[i].selected=true;
		   				}
                    }
            }
}
function selPostValueEntity(theSel,val) {
			var selLength=theSel.length;
			for (var i=selLength-1; i>=0; i--) {
		   		if (theSel.options[i].value==val) {
		   				theSel.options[i].selected=true;
		   		}
            }
}
function clearValueEntity(theSel) {
			var selLength=theSel.length;
			for (var i=selLength-1; i>=0; i--) {
		   		theSel.options[i].selected=false;
            }
}
function selectOption(obj, val) {
	for(var i = 0; i < obj.options.length; i++) {
		if(obj.options[i].value == val) {
			obj.options[i].selected = true;
		}
	}
}
function selectAllOptions(obj) {
	for(var i = 0; i < obj.options.length; i++) {
		obj.options[i].selected = true;
	}

	for(i = 0; i < obj.options.length; i++) {
		obj.options[i].value = obj.options[i].text;
		obj.options[i].selected = true;
	}
}
function addElement(theSel,dato,value) {var newOpt = new Option(dato, value);var selLength = theSel.length;theSel.options[selLength] = newOpt;}
function getStringList(theSel,token) {
  			var selLength = theSel.length;
  			var data="";
  			for (var temp1=0; temp1<selLength; temp1++) {
  					if (theSel[temp1].selected==true) {
	  						if (theSel[temp1].value=="Todas")   return "Todas";
  							if (data=="")   data=data+theSel[temp1].value;
	  						else    data=data+token+theSel[temp1].value;
  					}
  			}
  			return data;
}
function fillData(from1, from2, from3, from4, to1, to2, to3, to4, theSel){
    if (from1.value==""||parseInt(from1.value)>255||
        from2.value==""||parseInt(from2.value)>255||
        from3.value==""||parseInt(from3.value)>255||
        from4.value==""||parseInt(from4.value)>255) {
        alert( "El rango de IP inicial no tiene valores correctos" );
        from1.focus();
        return false ;
    }
    if ( !( (to1.value==""&&to2.value==""&&to3.value==""&&to4.value=="") ||
        (to1.value!=""&&to2.value!=""&&to3.value!=""&&to4.value!="") ) ) {
        alert( "El rango de IP final no tiene valores correctos" );
        to1.focus();
        return false ;
    }
    if (parseInt(to1.value)>255||parseInt(to2.value)>255||parseInt(to3.value)>255||parseInt(to4.value)>255) {
        alert( "El rango de IP final no tiene valores correctos" );
        to1.focus();
        return false ;
    }
    if (parseInt(from1.value) > parseInt(to1.value)) {
        alert( "El rango de IP no tiene valores correctos" );
        from1.focus();
        return false ;
    }
    if (parseInt(from2.value) > parseInt(to2.value)) {
        alert( "El rango de IP no tiene valores correctos" );
        from2.focus();
        return false ;
    }
    if (parseInt(from3.value) > parseInt(to3.value)) {
        alert( "El rango de IP no tiene valores correctos" );
        from3.focus();
        return false ;
    }
    if (parseInt(from4.value) > parseInt(to4.value)) {
        alert( "El rango de IP no tiene valores correctos" );
        from4.focus();
        return false ;
    }
    var dato = "";
    if (from1.value==to1.value||to1.value==""){dato+=from1.value+".";}
    else{dato+=from1.value+"-"+to1.value+".";}
    if (from2.value==to2.value||to2.value==""){dato+=from2.value+".";}
    else{dato+=from2.value+"-"+to2.value+".";}
    if (from3.value==to3.value||to3.value==""){dato+=from3.value+".";}
    else{dato+=from3.value+"-"+to3.value+".";}
    if (from4.value==to4.value||to4.value==""){ dato+=from4.value;}
    else{dato+=from4.value+"-"+to4.value;}

    from1.value="";
    from2.value="";
    from3.value="";
    from4.value="";

    to1.value="";
    to2.value="";
    to3.value="";
    to4.value="";

    var newOpt = new Option(dato, dato);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;

    return true;
}
function sameYear(year){
    //window.alert("FUNCIONA!" + year);
    document.wokstats.dell.value=year;
}
