/* V A L I D A T E    F O R M */
function validateForm(validate) {
    var curr_field, fieldName, filterList, element, errno, tvalue, len;
    var missinginfo = '';
    var matchEmail =/email/i
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    var matchField = /([a-zA-Z0-9]{1}[\w_]+[a-zA-Z0-9]{1})\[([\S]{0,})\]/i
    
    /*filters used*/
    var filter1 = /len/i //maximum length
    var filter2 = /numeric/i //numeric fields
    
    var checkList = validate.split(",");
    for( var z=0; z < checkList.length; z++ ) {
        //get the field name
        fieldName = checkList[z].replace(matchField,"$1");
        //get the field criterias
        curr_field = checkList[z].replace(matchField, "$2");
        element = document.getElementById(fieldName);
        
        //check if this field is the email field to use different filter
        if(matchEmail.test(fieldName)) {
            if( !filter.test(element.value) ) { errno = 3; } else { errno = 0; }
        } else {
            //check if field is blank
            if( element.value == '' ) {
                errno = 1;
            } else {
                filterList = curr_field.split(";");
                for( var f=0; f < filterList.length; f++ ) {
                    
                    tvalue = filterList[f].split("=");
                    if(filter1.test(filterList[f])) {//check field length
                        if(element.value.length > tvalue[1] ) { errno = 2; len=tvalue[1]; break; } else { errno = 0; }
                    } else if(filter2.test(filterList[f])){//check if field is numeric
                        if(!IsNumeric(element.value)) { errno = 4; break; } else { errno = 0; }
                    }
                }
            }
        }
        //ERRORS messages type
        if(errno==0){ //field is valid
            element.style.backgroundColor='#FFFFFF';
        }else if(errno==1) {//field is blank
            missinginfo += '\n - '+ capitalize(fieldName)+ ' es obligatorio';
            element.style.backgroundColor='#E3F0FF';
        }else if(errno==2) {//max characters permitted
            missinginfo += '\n - '+ capitalize(fieldName)+ ' Max. Characteres permitidos = '+len;
            element.style.backgroundColor='#E3F0FF';
        }else if(errno==3) {//invalid email
            missinginfo += '\n - '+ capitalize(fieldName)+ ' no es valido';
            element.style.backgroundColor='#E3F0FF';
        }else if(errno==4) {//numeric fields
            missinginfo += '\n - '+ capitalize(fieldName)+ ' es un campo numerico';
            element.style.backgroundColor='#E3F0FF';
        }
    }
    if (missinginfo != '') {
        missinginfo = '_____________________________\n' + 'Por favor rellene correctamente los campos abajo:\n' +
        missinginfo + '\n_____________________________';
        alert(missinginfo);
        return false;
    } else {
        return true;
    }
    return false;
}

/*C O N V E R T   F I R S T   L E T T E R   T O    U P P E R C A S E*/
function capitalize(str) {
  var arr=str.split("");
  arr[0]=arr[0].toUpperCase();
  var s=arr.join("");
  return s;
}

/*C H E C K   I F   A   F I E L D   I S   N U M E R I C*/
function IsNumeric(sText){
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

 
    for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
   return IsNumber;
   
}


function playMovie() {
    document.getElementById('movie').style.display='';
}

/* S T O P   M O V I E S*/
function closeMovie() {
    if(navigator.appName == "Microsoft Internet Explorer") {
        document.getElementById('pelicula').stop();
    }
    //if(navigator.appName == "Netscape")
    document.getElementById('pelicula').src='';
    document.getElementById('movie').style.display='none';
}



/*A J A X*/
function isValidFragment(fragment_url) {
    /* 
     * Maximum URL Length Is 2,083 Characters in Internet Explorer, 
     * though RFC 2616 that defines HTTP does not set limitations to URL size
     * Ref: http://support.microsoft.com/default.aspx?scid=KB;en-us;q208427
     */
    var strFullUrl = fragment_url;

    if( strFullUrl.length > 2083) {
            alert("Error: URL length " + strFullUrl.length  + " in remote query exceeds 2083 characters, which is the limit in IE");
            return false;
    } else {
            return true;
    }
}
function ajax(fragment_url, element_id, callback) {
    var element = document.getElementById(element_id);

    if(! isValidFragment(fragment_url)) return false;
    document.body.style.cursor = "progress";
    
    element.innerHTML = '<img src="/images/animated/loading_animation_liferay.gif">';
    
    if (window.XMLHttpRequest) { //for all browers but IE
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { //for IE
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    }
    
    xmlhttp.open("GET", fragment_url+ '&r='+Math.random());
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.body.style.cursor = "auto";
            element.innerHTML = (unescape(xmlhttp.responseText)).replace(/\+/gi," ");
            if(callback) callback();
        }
    }
    xmlhttp.send(null);
}

/* T E X T   C O U N T */
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    } else { 
        countfield.value = maxlimit - field.value.length;
    }
}

function addFavorites() {
    var url = "http://www.comedia-zone.com";
    var title = "Comedia-Zone.com Humor y diversión en la red";
    
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } 
    else if(document.all)// ie
        window.external.AddFavorite(url, title);
}

function setHomePage(){
    this.style.behavior='url(#default#homepage)';
    this.setHomePage('http://dynamicdrive.com');
    return false;
}

/* C O N F I R M    B E F O R E   S U B M I T */
function confirmAction(url,message) {
    if(confirm(message)) {
        window.location.href = url;
    } else {
        return false;
    }
    return false;
}

/* E X E C U T E    F U N C T I O N    O N    K E Y   P R E S S*/
function keyPress(e) {
    if(e == 13){ //if generated character code is equal to ascii 13 (if enter key)
        submitIt('index.php?action=search');
    }
    return false
}

/* G E T   U R L  */
function getURL(url) {
    window.location.href = url;
    return false
}

function orgName(value){
    if(value != 'Select Organisations')
        document.getElementById('ref').innerHTML = value;
}

function isNumeric(e) {
    var unicode=e.charCode? e.charCode : e.keyCode;
    if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
        if (unicode<48||unicode>57) //if not a number
            return false; //disable key press
    }

}

function toggleSMS(value){
    if(value==0)
        document.getElementById('sms').style.display='none';
        
    if(value==1)
        document.getElementById('sms').style.display='inline';
}

//--------------------------------------------------------
//PRINT ALL DOCUMENTS AND REMOVE SPECIFIED TABLES
//--------------------------------------------------------
var _jslib_isIE=document.all?true:false;
var _jslib_isNS=document.layers?true:false;
var _jslib_isNS6=document.getElementById&&!document.all?true:false;

function removeelements(){
    var remove_el;
    var eleLen;
    if(document.all){
        remove_el = document.all.remove;
        //if there is only one element with id=remove
        if ( remove_el!='' && remove_el.length==null ){
            remove_el.style.display='none';
        } else {
            //for each element with id=remove
            for ( i=0;i<remove_el.length;i++ ){
                remove_el[i].style.display='none';
            }
        }
    } else {
        remove_el = document.getElementById('remove');
        //remove_el = document.layers['remove'];
        remove_el.style.display='none';
    }

}

function revertback(){
setTimeout("parent.location.reload()",50)
}

function printArea(){
    document.getElementById('tblTop').style.display='none';
    document.getElementById('tblm1').style.display='none';
    document.getElementById('tblm2').style.display='none';
    document.getElementById('tblm3').style.display='none';
    document.getElementById('tblFooter').style.display='none';
    document.getElementById('tblFooter2').style.display='none';
    window.print();
    revertback();
}