
var valid_color="green";
var notValid_color="red";
function onlyNumbers(e)
{
    var ret=true
    var charCode = e.which || e.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        ret = false;
    return ret;  
}

function getLimitStart(index,resultsPerPage){
        var ls=(((index) * resultsPerPage) - resultsPerPage);
        return ls;
    }

function maxNumberAllowed(field,value){
    if(field.value>value)
        field.value=field.value.substr(0,(field.value.length-1));
}


/**
 * Megalh prosoxh ston orismo twn VL sunarthsewn.Prepei na akolou8oun akrivws thn domh opws parakatw.
 */
 // to extra cond to xreiazomaste gia tous validationListeners, prokeimenou na sunduasoume sunarthseis
 function VL_password_confirm(elem){
     var id=elem.id;
     var cond=false;
     if(id.search(/confirm/)==-1)
        cond=(elem.value==document.getElementById(elem.id+"_confirm").value) && elem.value.length>0;
     else
        cond=elem.value==document.getElementById(elem.id.replace(/_confirm/g,'')).value && elem.value.length>0;

    return cond;
 }
 
 function password_confirm(elem,label_id,toolTipOnValidation){
    var id=elem.id;
    var cond=VL_password_confirm(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,cond);
    //to parakatw if kanei sthn ousia to bind meta3u pass-confirm pass gia na kokkinizoun-prasinizoun ta labels,
    //ka8ws k na emfanizontai h oxi ta cooltips analoga me to an einai valid
    if(id.search(/confirm/)==-1){
        var o_elem_id=document.getElementById(elem.id+"_confirm").id;
        var o_label_id=document.getElementById(label_id+"_confirm").id;
        toolTipValidator(cond,o_elem_id,toolTipOnValidation);
        labelValidator(o_label_id,cond);
    }
    else{
        var o_elem_id=document.getElementById(elem.id.replace(/_confirm/g,'')).id;
        var o_label_id=document.getElementById(label_id.replace(/_confirm/g,'')).id;
        toolTipValidator(cond,o_elem_id,toolTipOnValidation);
        labelValidator(o_label_id,cond);
    }
    return cond;
 }
 
function VL_isNotEmpty(elem){
    var str=elem.value;
    return str.length>0;
}

function isNotEmpty(elem,label_id,toolTipOnValidation){
    var cond=VL_isNotEmpty(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,cond);
    return cond;
}

function isMail(str){ 
    return str.match(/^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/);
}

function VL_checkMail(elem){ 
    var str=elem.value;
    return str.match(/^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/);
}

function checkMail(elem,label_id,toolTipOnValidation)
{
    var cond=VL_checkMail(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,isMail(elem.value));
    return cond;
}

function VL_isDDLoptionSelected(elem){ 
    var str=elem.value;
    return str!="-1" && str!="" && str!="null";
}

function isDDLoptionSelected(elem,label_id,toolTipOnValidation)
{
    var cond=VL_isDDLoptionSelected(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,cond);
    return cond;
}

function VL_isGVrowSelected(elem){ 
    var str=elem.value;
    return str.length>0;
}

function isGVrowSelected(elem,label_id,toolTipOnValidation)
{
    var cond=VL_isDDLoptionSelected(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,cond);
    return cond;
}


function VL_lengthRequirement(elem,mn,mx){
    var length=elem.value.length;
    var isValid=false;
    //OTAN EXOUME mn=-1 & mx=-1 8ELOUME NA MHN LAMBANETAI UPOPSHN O PERIORISMOS
    if(mn!=-1 && mx!=-1)
        if((length>=mn && length<=mx) || (length>=mx && length<=mn))
            isValid=true;
    
    if(mn!=-1 && mx==-1)
        if( length>=mn )
            isValid=true;
    
    if(mn==-1 && mx!=-1)
        if( length<=mx )
            isValid=true;

    return isValid;
}
function lengthRequirement(elem,mn,mx,label_id, toolTipOnValidation){
    var label=document.getElementById(label_id);
    var isValid= VL_lengthRequirement(elem,mn,mx);
    
    toolTipValidator(isValid,elem.id,toolTipOnValidation);
    labelValidator(label_id,isValid);
    return isValid;
}
function toolTipValidator(isValid,id,toolTipOnValidation){
    var toolTipVar="coolTip_"+id;
    if(!isValid && toolTipOnValidation)
        eval(toolTipVar+".options.delay=250");
    if(isValid && toolTipOnValidation)
        eval(toolTipVar+".options.delay=99999999999999999");
}
function isDate(elem){

     var str=elem.value;
     //Regular expressions for Date Validation
     /*var regexp1=/^((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8]) (JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC) ((1[6-9]|[2-9]\d)\d{2})$/;
     var regexp2=/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
     var regexp3=/^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$/;
     var regexp4=/^((((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9]))))[\-\/\s]?\d{2}(([02468][048])|([13579][26])))|(((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))[\-\/\s]?\d{2}(([02468][1235679])|([13579][01345789]))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$/;
     var regexp5=/^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2}))/;
     var ret=str.match(regexp1) || str.match(regexp2)|| str.match(regexp3)|| str.match(regexp4)|| str.match(regexp5);*/
     ret=Date.parse(str);
    return ret;
}



function VL_isValidInputDate(elem){
    var str=elem.value;
    ret=Date.parse(str);
    return ret;
}
function isValidInputDate(elem,label_id,toolTipOnValidation){
    var cond=VL_isValidInputDate(elem);
    toolTipValidator(cond,elem.id,toolTipOnValidation);
    labelValidator(label_id,cond);
    return cond;
}




function VL_isValidDateRange(dateStart, dateEnd, elem){
    var dS=Date.parse(dateStart);
    var dE=Date.parse(dateEnd);
    var d=Date.parse(elem.value);
    var cond=(d>=dS && d<=dE);
    return cond;
}
function isValidDateRange(dateStart, dateEnd, elem, label_id,toolTipOnValidation){
    if(isValidInputDate(elem,label_id,toolTipOnValidation)){
        var cond=VL_isValidDateRange(dateStart, dateEnd, elem);
        toolTipValidator(cond,elem.id,toolTipOnValidation);
        labelValidator(label_id,cond);

        return cond;
    }
    else return false;
}



function VL_isValidDateToStartFrom(dateStart, elem){
    var dS=Date.parse(dateStart);
    var d=Date.parse(elem.value);
    var cond=(d>=dS);
    return cond;
}
function isValidDateToStartFrom(dateStart, elem, label_id,toolTipOnValidation){
    if(isValidInputDate(elem,label_id,toolTipOnValidation)){
        var cond=VL_isValidDateToStartFrom(dateStart, elem);
        toolTipValidator(cond,elem.id,toolTipOnValidation);
        labelValidator(label_id,cond);

        return cond;
    }
    else 
        return false;
 
}




function VL_isValidDateToEndAt(dateEnd, elem){
    var dE=Date.parse(dateEnd);
    var d=Date.parse(elem.value);
    var cond=(d<=dE && d);
    return cond;
}
function isValidDateToEndAt(dateEnd, elem, label_id, toolTipOnValidation){
    if(isValidInputDate(elem,label_id,toolTipOnValidation)){
        var cond=VL_isValidDateToEndAt(dateEnd, elem);
        toolTipValidator(cond,elem.id,toolTipOnValidation);
        labelValidator(label_id,cond);

        return cond;
    }
    else
        return false;
}

function labelValidator(label_id,valid){
    var label=document.getElementById(label_id);
    if(valid)
        label.style.color=valid_color;
    else
        label.style.color=notValid_color;
        
}
function dynamicSubmitValidation(jsString){
    eval(jsString);
}
function showHintBox(yuiTooltip,contextId, show){
    yuiTooltip.contextMouseOverEvent.subscribe(
        function(type, args) {
            var context = args[0];
            if (context && context.id == contextId) 
                return show;
            else 
                return !show;
        }
    );
}
// Set the text for the tooltip just before we display it.
function setHintBoxText(yuiTooltip, text){
    yuiTooltip.contextTriggerEvent.subscribe(
        function(type, args) {
            var context = args[0];
            this.cfg.setProperty("text", text);
        }
    );
}

function ajaxValidateSubmit(gform){
    ajaxPostForm("./actions.jsp","ajaxValidationDiv",gform);
}
// *******************************
// ** GENERAL PURPOSE FUNCTIONS **
// *******************************
function calAnchor(elem,divID){
    autoC=document.getElementById(divID);
    hideMultiCalAnchor(divID);
    
    if(autoC.style.display=="none"||bufferName!=elem.name){
        unhideMultiCalAnchor(divID)
        anchorDiv2(divID,elem,"650px");
    }

    bufferName=elem.name;
}

function anchorToElem(elem,divID){
    var div=document.getElementById(divID);
    //div=document.getElementById("debugger");
    div.style.display="none";
    if(div.style.display=="none"||bufferName!=elem.name){
        div.style.left=Left(elem)+"px";
        div.style.top=Top(elem)+elem.offsetHeight+"px";
        div.style.display="";
    }
    bufferName=elem.name;

    document.getElementById("debugger").innerHTML="left= "+Left(elem)+", Top= "+Top(elem)+" + "+elem.offsetHeight;
}

function Left(obj){
    var curleft = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
        
function Top(obj){
   var curtop = 0;
   if (obj.offsetParent){
       while (obj.offsetParent){
           curtop += obj.offsetTop
           obj = obj.offsetParent;
       }
   }
   else if (obj.y)
      curtop += obj.y;
   return curtop;
}

function parentExists(obj){
    if(obj.parentNode)
        return true;
    else
        return false;
}
function findCoordOfParent(obj){
    //if(parentExists(obj)){
        elem=obj.parentNode.parentNode.parentNode.parentNode;
        alert(Left(elem)+","+Top(elem)+",id="+elem.id);
    //}
    
}

function tester(elem){
    elem.style.left="250px";
    elem.style.top="300px";
}
                
