﻿// Common.js

function openwin(url, h, w){
    XPos = screen.availWidth/2 - w/2; 
    YPos = (screen.availHeight/2 - h/2) - 40; 
    var ow = window.open(url, "newwindow", "toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=top,status=no,width="+w+",height="+h+",ScreenX=0,ScreenY=0,Top="+YPos+",Left="+XPos); 
    ow.focus();
}

function countTxt(frm, limitNum, contCtl, asc){
    if (frm.value.length > limitNum) {
        frm.value = frm.value.substring(0, limitNum);
    }
    if (contCtl!=""){
        var txtCount=document.getElementById(contCtl);
        if (asc)
            txtCount.innerHTML=frm.value.length;
        else
            txtCount.innerHTML=limitNum - frm.value.length;
    }
}

function SetInputFocusHS(ctl){
    if(!$) 
        return false;
    var top = $(document).scrollTop(); 
    var left = $(document).scrollLeft(); 
    $('form').prepend("<input id='tmphs' type='text'/>");
    $('#tmphs').focus().remove(); 
    $(document).scrollTop(top); 
    $(document).scrollLeft(left); 
    if(ctl) 
        $(ctl).focus();
}
        
function fnPrintPage(id){
    document.getElementById(id).style.display = "none";
    setTimeout("fnShowElement('" + id + "')", 3000);
    window.print();
}

function fnShowElement(id){
    document.getElementById(id).style.display = "";
}

function MultiFileRequiredValidator(source, args){
    args.IsValid = $("." + $(source).attr('fuToValidate') + ":file").length > 1;
}

function CheckboxRequiredValidator(source, args){
    var hasChecked = false;
    $(source).parent().find(":checkbox").each(function(){if(this.checked){hasChecked=true}});
    args.IsValid = hasChecked;
}

function FormCheckDateRequiredValidator(source, args){
    args.IsValid = false;    
    var ctl = document.getElementById(args.Value);
    var parentctl = ctl.parentNode;
    var ctls = parentctl.childNodes;
    for (i = 0; i < ctls.length; i++) {
        if(ctls[i].type=='select-one' && ctls[i].id.lastIndexOf('ddMonth')>-1  && ctls[i].value.trim()=='')
            return false
        else if(ctls[i].type=='select-one' && ctls[i].id.lastIndexOf('ddDay')>-1  && ctls[i].value.trim()=='')
            return false
        else if(ctls[i].type=='text' && ctls[i].id.lastIndexOf('tbYear')>-1  && ctls[i].value.trim()=='')
            return false
    }
    args.IsValid = true;
}

function FormCheckDateIsValidValidator(source, args){
    args.IsValid = false;    
    var ctl = document.getElementById(args.Value);
    var parentctl = ctl.parentNode;
    var ctls = parentctl.childNodes;
    var monthfield = ''
    var dayfield = ''
    var yearfield = '';
    for (i = 0; i < ctls.length; i++) {
        if(ctls[i].type=='select-one' && ctls[i].id.lastIndexOf('ddMonth')>-1  && ctls[i].value.trim()!='')
            monthfield = ctls[i].value;
        else if(ctls[i].type=='select-one' && ctls[i].id.lastIndexOf('ddDay')>-1  && ctls[i].value.trim()!='')
            dayfield = ctls[i].value;
        else if(ctls[i].type=='text' && ctls[i].id.lastIndexOf('tbYear')>-1  && ctls[i].value.trim()!='')
            yearfield = ctls[i].value;
    }
    if(monthfield=='' && dayfield=='' && yearfield==''){
        args.IsValid = true;
        return true
    }
        
    var dayobj = new Date(yearfield, monthfield-1, dayfield);
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
        return false;
    args.IsValid = true;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

var fontsize_min=8;
var fontsize_max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=fontsize_max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=fontsize_min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}
