/*
 * script para los formularios de UBU
 *
 * @author Divisa Informática y Telecomunicaciones S.A.
 * @copy  2007
 */

function setColor(elm,color) {
	elm.style.background = color;
}

function setColorAll(elmId,color) {
	elm = document.getElementsByName(elmId);
	for (var k=0; k<elm.length; k++){
		if(navigator.family=='gecko') {
			elm[k].parentNode.style.background = color;
		}
		if(navigator.family=='ie4') {
			elm[k].style.background = color;
		}
	}
}

// función general para todos los formularios
function checkFormBubu(form1,paramarray,paramarrayvalues) {
	var correct = true;
	
	switch(navigator.family) {
        case 'nn4':
            break;
        case 'ie4': 
        case 'gecko':
		default:
            // Versión de Netscape 6.
            for(var i=0; i<paramarray.length; i++) {
                var elm = form1.elements[paramarray[i]];
                if (elm.type=="text"){
                    if (elm.value.length==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                }
                if (elm.type=="textarea"){
                    if (elm.value.length==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }   
                }
                if (elm.type=="password"){
                    if (elm.value.length==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }   
                }
                if (elm.type=="checkbox"){
                    // Es un checkbox de un elemento.
                    if (!elm.checked){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                }
                if (elm.type=="hidden"){
                    if (elm.value.length==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                }
                if (elm.type=="radio"){
                    if (!elm.checked){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                }
                if (elm.type=="file"){
                    if (elm.value.length==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                }
                if (elm.type=="select-multiple"){
                    if (elm.selectedIndex==-1){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                    var count=0;
                    for(var j=0; j<elm.length; j++) {
                        if (elm.options[j].selected){
                            if (elm.options[j].value!=null && elm.options[j].value!=""){
                                count++
                            }
                        }
                    }
                    if (count==0){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }                
                }
            
                if (elm.type=="select-one"){
                    if (elm.selectedIndex==-1){
						correct = false;
						elm.style.background = "#FFFFCC";
                    }
                    for(var j=0; j<elm.length; j++) {
                        if (elm.options[j].selected){
                            if (elm.options[j].value==null || elm.options[j].value==""){
								correct = false;
								elm.style.background = "#FFFFCC";
                            }
                        }
                    }
                }
            
                // Tenemos que obtener la versión del browser utilizada.
                if(elm.length!=undefined) {
                    // Suponemos que es un array de elementos
                    var isCheckbox =0;
                    var isRadio=0;
                    var isOther=0;
                    var radioChecked=0;
                    var checkboxChecked=0;
                    var nValues=0;
                    for (var k=0; k<elm.length;k++){
                        if (elm[k].type=="text"){
                            isOther++;
                            if (elm[k].value.length>0){
                                nValues++;
                            }
                        }
                        if (elm[k].type=="textarea"){
                            isOther++;
                            if (elm[k].value.length>0){
                                nValues++;
                            }
                        }
                        if (elm[k].type=="password"){
                            isOther++;
                            if (elm[k].value.length>0){
                                nValues++;
                            }
                        }
                        if (elm[k].type=="checkbox"){
                            isCheckbox++;
                            // Es un checkbox de un elemento
                            if (elm[k].checked){
                                checkboxChecked++;
                            }
                        }
                        if (elm[k].type=="hidden"){
                            isOther++;
                            if (elm[k].value.length>0){
                                nValues++;
                            }
                        }
                        if (elm[k].type=="file"){
                            isOther++;
                            if (elm[k].value.length>0){
                                nValues++;
                            }
                        }
                        if (elm[k].type=="select-multiple"){    
                            if (elm[k].selectedIndex==-1){
								correct = false;
                            }
                            var count=0;
                            for(var j=0; j<elm[k].length; j++) {
                                if (elm[k].options[j].selected){
                                    if (elm[k].options[j].value!=null && elm[k].options[j].value!=""){
                                        count++
                                    }
                                }
                            }
                            if (count==0){
								correct = false;
                            }
                        }
                        if (elm[k].type=="select-one"){
                            if (elm[k].selectedIndex==-1){
                                output += paramarrayvalues[i] + "\n";
								correct = false;
                            }
                            for(var j=0; j<elm[k].options.length; j++) {
                                if (elm[k].options[j].selected){
                                    if (elm[k].options[j].value==null || elm[k].options[j].value==""){
										correct = false;
                                    }
                                }
                            }
                        }
                        if (elm[k].type=="radio"){
                            isRadio++;
                            // Es un checkbox de un elemento
                            if (elm[k].checked){
                                radioChecked++;
                            }
                        }
                    }
                    if (isRadio!=0 && radioChecked==0){
						correct = false;
						for(j=0; j<elm.length; j++) {
							if(navigator.family=='gecko') {
								elm.item(j).parentNode.style.background = "#FFFFCC";
							}
							if(navigator.family=='ie4') {
								elm.item(j).style.background = "#FFFFCC";
							}
						}
                    }
                    if (isCheckbox!=0 && checkboxChecked==0){
						correct = false;
						for(j=0; j<elm.length; j++) {
							if(navigator.family=='gecko') {
								elm.item(j).parentNode.style.background = "#FFFFCC";
							}
							if(navigator.family=='ie4') {
								elm.item(j).style.background = "#FFFFCC";
							}
						}
                    }
                    if (isOther!=0 && nValues==0){
						correct = false;
						for(j=0; j<elm.length; j++) {
							if(navigator.family=='gecko') {
								elm.item(j).parentNode.style.background = "#FFFFCC";
							}
							if(navigator.family=='ie4') {
								elm.item(j).style.background = "#FFFFCC";
							}
						}
                    }
                }
            }
			if(form1.name=='grupo') {
				//estamos en el formulario de compra bibliografica
				//hay que controlar que los datos de los libros a solicitar no esten vacios
				//el textarea o el input file 
				var textarea = document.getElementById('datos_libros');
				var file = document.getElementById('fichero');
				if ((textarea && textarea.value.length==0) && (file && file.value.length==0)){
					correct = false;
					textarea.style.background = "#FFFFCC";
					file.style.background = "#FFFFCC";
                } 
			}
			if(!correct) {
				alert('Rellene todos los campos marcados!');
				return false;
			}
        }
	
	waitCursor();
      
    return true;
 }
