// JScript File


function showdiv(sDiv) { 

    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById(sDiv).style.visibility = 'visible'; 
    } 
    else { 
        if (document.layers) { // Netscape 4 
            document.hideShow.visibility = 'visible'; 
        } 
        else { // IE 4 
            document.all.hideShow.style.visibility = 'visible'; 
        } 
    } 
    
} 

function hidediv(sDiv) { 
    
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById(sDiv).style.visibility = 'hidden'; 
    } 
    else { 
        if (document.layers) { // Netscape 4 
            document.hideShow.visibility = 'hidden'; 
        } 
        else { // IE 4 
            document.all.hideShow.style.visibility = 'hidden'; 
        } 
    } 

} 

function isHidden(sDiv) {
    var retval = false;

    if (document.getElementById) { // DOM3 = IE5, NS6 
        if (document.getElementById(sDiv).style.visibility == 'hidden')  
            retval = true;
    } 
    else { 
        if (document.layers) { // Netscape 4 
            if (document.hideShow.visibility == 'hidden')
                retval = true;
        } 
        else { // IE 4 
            if (document.all.hideShow.style.visibility == 'hidden')
                retval = true;
        } 
    } 

    return retval;

}







/*function ajax_GetData(root,handleResponseFunction,qry) {

    d = new Date();
    
    qry += '&unique_call_ref=' + URLEncode(d.getTime());
    
    new Ajax(root + 'AJAX/AjaxProcessor.php?' + qry, {
        method:     'get',
        //postBody: $(frm),
        onComplete: handleResponseFunction,
        evalResponse: true
    }).request();

}

function ajax_GetWebControl(root, ctl, qry, updateCtl) {
    path = root + 'AJAX/ajaxWebControlHTML.aspx?control=' + ctl + qry;
    new Ajax(path, {
        method: 'get',
        update: $(updateCtl),
        onComplete: handleWebControlResponse
    }).request();
}*/



function ajax_GetData(root, data) {

    d = new Date();
    $extend(data, {'t' : d.getTime()});
    url = root + 'AJAX/AjaxProcessor.php';
    new Request({
        url: url,
        method: 'get',
        data: data,
        onComplete: handleResponse,
        evalResponse: true
    }).send();
}

function ajax_GetWebControl(root, loadctl, outctl, data) {
    d = new Date();
    data += '&t=' + d.getTime() + '&control=' + loadctl;
    url = root + 'AJAX/ajaxWebControlHTML.php';
    new Request.HTML({
        url: url + data,
        onSuccess: function(html) {
            outctl.set('text', '');
            outctl.adopt(html);
            handleWebControlResponse();
        },
        onFailure: function() {
            outctl.set('text', 'Sorry, there was an unhandled error making this call');
        }
    }).send();
}

function showError(m) {
    alert('Sorry. There has been a problem running your command.\n\nError: ' + m);
}

function toggleMenu(menuName) {
    s = 'nav_' + menuName;
    if ($(s) != null) {
        if ($(s).style.display == 'none') {
            $(s).style.display = 'inline';
        } else {
            $(s).style.display = 'none';
        }
    }
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function showPopUp(url, width, height, scrollbars) {
    window.open(url, 'win', 'toolbar=0,location=0,width=' + width + ',height=' + height + ',scrollbars=' + scrollbars);
    return false;
}

function dateAdd(p_Interval, p_Number, p_Date){
//if(!isDate(p_Date)){return "invalid date: '" + p_Date + "'";}
//if(isNaN(p_Number)){return "invalid number: '" + p_Number + "'";} 

p_Number = new Number(p_Number);
var dt = new Date(p_Date);
switch(p_Interval.toLowerCase()){
case "yyyy": {// year
dt.setFullYear(dt.getFullYear() + p_Number);
break;
}
case "q": { // quarter
dt.setMonth(dt.getMonth() + (p_Number*3));
break;
}
case "m": { // month
dt.setMonth(dt.getMonth() + p_Number);
break;
}
case "y": // day of year
case "d": // day
case "w": { // weekday
dt.setDate(dt.getDate() + p_Number);
break;
}
case "ww": { // week of year
dt.setDate(dt.getDate() + (p_Number*7));
break;
}
case "h": { // hour
dt.setHours(dt.getHours() + p_Number);
break;
}
case "n": { // minute
dt.setMinutes(dt.getMinutes() + p_Number);
break;
}
case "s": { // second
dt.setSeconds(dt.getSeconds() + p_Number);
break;
}
case "ms": { // second
dt.setMilliseconds(dt.getMilliseconds() + p_Number);
break;
}
default: {
return "invalid interval: '" + p_Interval + "'";
}
}
return dt;
}



//===============================================================================

function isNumeric(sText,AllowMinus)
{
   var ValidChars = "-0123456789.";
   var IsNumber=true;
   var Char;

    if (AllowMinus == 0) {
        //no minus allowed
        ValidChars = "0123456789.";
        }
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }




function isDate(dateStr) {

    var matchArray;
    var datePat1 = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var datePat2 = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;

    // is the format ok?
    matchArray = dateStr.match(datePat1); 

    if (matchArray == null) {
    
        matchArray = dateStr.match(datePat2); 
    
        if (matchArray == null) {
           alert("Please enter date as dd/mm/yyyy.");
            return false;
            }    
    }
    
    month = matchArray[3]; // p@rse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
        }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
        }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn`t have 31 days!")
        return false;
        }

        if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
                alert("February " + year + " doesn`t have " + day + " days!");
                return false;
                }
            }
    
    return true; // date is valid
}


function isDateNoMessage(dateStr) {

    var matchArray;
    var datePat1 = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var datePat2 = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;

    // is the format ok?
    matchArray = dateStr.match(datePat1); 

    if (matchArray == null) {
    
        matchArray = dateStr.match(datePat2); 
    
        if (matchArray == null) {
            return false;
            }    
    }
    
    month = matchArray[3]; // p@rse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        return false;
        }

    if (day < 1 || day > 31) {
        return false;
        }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
        }

        if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
                return false;
                }
            }
    
    return true; // date is valid
}



function formatDateYYYYMMDD(sDate,sType) {
    //formats from ddd DD/MM/YYYY to YYYYYMMDD

    if (sType == 1)
        return sDate.substring(10,14) + '' + sDate.substring(7,9) + '' + sDate.substring(4,6);
    else
        return sDate.substring(6,10) + '' + sDate.substring(3,5) + '' + sDate.substring(0,2);

}




