function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

/* Removed trailing spaces */
function trim(strTextToTrim) {
    strTextToTrim = strTextToTrim.replace(/^ +/, '');
    strTextToTrim = strTextToTrim.replace(/ +$/, '');
    return strTextToTrim;
}

function toProperCase(strText) {
    return strText.charAt(0).toUpperCase() + strText.substring(1, strText.length).toLowerCase();
}

function wait() {
}


function fixIE6flicker(fix) {
    try {
        document.execCommand("BackgroundImageCache", false, fix);
    } catch(err) {
    }
}

function replaceString(regexpression, replaceString) {
    var str1 = document.replaceForm.source.value
    var str2 = ""
    while (str1 != str2) {
        str2 = str1
        str1 = str1.replace(regexpression, replaceString)
    }
    document.replaceForm.source.value = str1
}

function jsAllowOnlyNumeric(myfield, e, dec)
{
    var key;
    var keychar;

    if (window.event) key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);
    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
        (key == 9) || (key == 13) || (key == 27))
        return true;
        // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;
        // decimal point jump
    else if (dec && (keychar == "."))
    {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}


function jsCheckDecimalNumberOnly(myfield, e, dec)
{
    var key;
    var keychar;

    if (window.event) key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);
    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
        (key == 9) || (key == 13) || (key == 27))
        return true;
        // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;
        // decimal point jump
    else if (keychar == ".")
    {
        var ctr=-1;
        for (var i=0;i<myfield.value.length;i++) {
             if (myfield.value.charAt(i) == ".") {
                 ctr++;
             }
        }

        if (ctr == 0) return false;
        //myfield.form.elements[dec].focus();
        return true;
    }
    else
        return false;
}


// non-empty textbox
function isEmpty(str) {

    if (str.length == 0) {
        return true;
    }
    return false;
}

function jsCheckAlphaNumOnly(oObject) {
    var strText = null;
    if ((window.event != null) && (window.event.type != null) && (window.event.type == "keypress")) {
        strText = String.fromCharCode(window.event.keyCode);
    } else if ((window.event != null) && (window.event.type != null) && (window.event.type == "paste")) {
        strText = window.clipboardData.getData("Text");
    } else {
        if (typeof oObject == 'object') {
            if ((oObject != null) && (oObject.value != null)) {
                strText = oObject.value;
            }
        } else if (typeof oObject == 'string') {
            strText = oObject;
        }
    }
    if (typeof strText == 'string') {
        if (strText != null) {
            var numChars = "0123456789";
            var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (index = 0; index < strText.length; index++) {
                if (numChars.indexOf(strText.charAt(index)) == -1) {
                    if (alphaChars.indexOf(strText.charAt(index)) == -1) {
                        return false;
                    }
                }
            }
            return true;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid email addressD")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Invalid email address")
		    return false
		 }

 		 return true
	}



function openWin(pageURL, title, w, h) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    //var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width='+w+', height='+h+', top='+top+', left='+left);
    window.open(pageURL, title, 'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width='+w+', height='+h+', top='+top+', left='+left);
}


function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}


function activateMenu(obj) {

    $("#nav_home").attr('class','');
    $("#nav_special").attr('class','');
    $("#nav_order").attr('class','');
    $("#nav_contact").attr('class','');
    $("#nav_cart").attr('class','');

    obj.className = "navigation_hover";
}

function openWindow3(url, title, w, h) {

  var win = new Ext.Window({
      autoLoad: url,
      id:'win',
      width: w,
      height: h,
      title: title,
      autoScroll:true,
      modal:true,
      bodyStyle: 'background-color: #ffffff;padding:10px;',
      listeners:{show:function() {
      this.loadMask = new Ext.LoadMask(this.body, {
            msg:'Loading. Please wait...'
            });
      }},
      buttons: [
         {
             text:'Close',
             handler: function(){ win.close(); }
         }
     ]
  });
  win.show();
}

function openWindow2(url, title, w, h) {

  var win2 = new Ext.Window({
      autoLoad: url,
      id:'win2',
      width: w,
      height: h,
      title: title,
      autoScroll:true,
      modal:true,
      bodyStyle: 'background-color: #ffffff;padding:10px;',
      listeners:{show:function() {
      this.loadMask = new Ext.LoadMask(this.body, {
            msg:'Loading. Please wait...'
            });
      }},
      buttons: [
         {
             text:'Close',
             handler: function(){ win2.close(); }
         }
     ]
  });
  win2.show();
}

function openPayrollDate(url, title, w, h) {

  var win = new Ext.Window({
      autoLoad: url,
      width: w,
      height: h,
      title: title,
      autoScroll:true,
      modal:true,
      bodyStyle: 'background-color: #ffffff;padding:10px;',
      buttons: [
          {
             text:'Cancel',
             handler: function(){ win.close(); }
         },
         {   text:'Update',
             handler: function() {
                var dateFrom = $("#dateFrom").val();
                var dateTo = $("#dateTo").val();

                $("#paydate").text(dateFrom +" - "+ dateTo);


                Ajax.payrollDate(dateFrom, dateTo);
                win.close();
                //setTimeout("location.reload(true);", 300);
             }
         }
     ]
  });
  win.show();
}


function openDTR(url, title, w, h) {
    $("#dialog").load(url, function() {
            $("#dialog").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Update': function() {
                        var id = $("#id").val();
                        var timeIn = $("#timeIn").val();
                        var timeOut = $("#timeOut").val();
                        var otIn = $("#otIn").val();
                        var otOut = $("#otOut").val();
                        var timeInEdit = 0;
                        var timeOutEdit = 0;
                        var otInEdit = 0;
                        var otOutEdit = 0;

                        if (timeIn != "${DTR.timeIn}") timeInEdit = 1;
                        if (timeOut != "${DTR.timeOut}") timeOutEdit = 1;
                        if (otIn != "${DTR.otIn}") otInEdit = 1;
                        if (otOut != "${DTR.otOut}") otOutEdit = 1;

                        Ajax.updateDTR(id, timeIn, timeOut, timeInEdit, timeOutEdit, otIn, otOut, otInEdit, otOutEdit);
                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });  
     });
}


function addPayrollSched(url, title, w, h) {
    $("#dialog").load(url, function() {
            $("#dialog").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Save': function() {
                        var dateFrom = $("#dateFrom").val();
                        var dateTo = $("#dateTo").val();

                        Ajax.addPayrollSched(dateFrom, dateTo);
                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });
     });
}

function editPayrollSched(url, title, w, h) {
    $("#dialog").load(url, function() {
            $("#dialog").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Save': function() {
                        var id = $("#id").val();
                        var dateFrom = $("#dateFrom").val();
                        var dateTo = $("#dateTo").val();
                        var active = 0;

                        if ($("#active:checked")) active = 1;

                        Ajax.updatePayrollSched(id, dateFrom, dateTo, active);
                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });
     });
}

function delPayrollSched(id, title, w, h) {
    $("#delDialog").dialog('open');
    $("#delDialog").dialog({
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                title: title,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Delete': function() {
                        Ajax.delPayrollSched(id);
                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);
                    },
                    'Cancel': function() {
                        $(this).dialog('close');
                    }
                }
            });

}

function openFile(url, title, w, h) {
    $("#dialog").load(url, function() {
            $("#dialog").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Attach': function() {

                        Ajax.upload($("#filename").val(), {
                               callback:function(data) {
                                  if (data) {
                                      var num = new NumberFormat();
                                      num.setNumber(data);
                                      num.setPlaces('0', false);
                                      num.setSeparators(true, ',', ',');

                                     $("#msg").html("<font color='black'><b> Upload Complete! </b><br>"+ num.toFormatted() +" records uploaded.</font>");
                                     $("#filename").val("");
                                  }
                               },
                               errorHandler:function(message) {
                                   alert("Oops: " + message);
                               }
                        });

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });
     });
}


function openEmployeeDetails(url, title, w, h) {
    $("#dialogDetails").load(url, function() {
            $("#dialogDetails").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Save': function() {
                        var empID = $("#empID").val();
                        var lastname =  $("#lastname").val();
                        var firstname = $("#firstname").val();
                        var middlename = $("#middlename").val();
                        var sss = $("#sss").val();
                        var tin = $("#tin").val();
                        var basicSalary = $("#basicSalary").val();
                        var dailyRate = $("#dailyRate").val();
                        var hourlyRate = $("#hourlyRate").val();
                        var allowance = $("#allowance").val();
                        var sssPremium = $("#sssPremium").val();
                        var sssLoan = $("#sssLoan").val();
                        var pagibigPremium = $("#pagibigPremium").val();
                        var pagibigLoan = $("#pagibigLoan").val();
                        var philhealth = $("#philhealth").val();

                        Ajax.updateEmployee(empID, lastname, firstname, middlename,
                               sss, tin, basicSalary, dailyRate, hourlyRate, allowance,
                               sssPremium, sssLoan, pagibigPremium, pagibigLoan,
                               philhealth);

                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });
     });
}

function openAddEmployee(url, title, w, h) {
    $("#dialogDetails").load(url, function() {
            $("#dialogDetails").dialog({
                title: title,
                bgiframe: true,
                resizable: false,
                closeOnEscape: true,
                width: w,
                height:h,
                modal: true,
                buttons: {
                    'Save': function() {
                        var empID = $("#empID").val();
                        var lastname =  $("#lastname").val();
                        var firstname = $("#firstname").val();
                        var middlename = $("#middlename").val();
                        var sss = $("#sss").val();
                        var tin = $("#tin").val();
                        var basicSalary = $("#basicSalary").val();
                        var dailyRate = $("#dailyRate").val();
                        var hourlyRate = $("#hourlyRate").val();
                        var allowance = $("#allowance").val();
                        var sssPremium = $("#sssPremium").val();
                        var sssLoan = $("#sssLoan").val();
                        var pagibigPremium = $("#pagibigPremium").val();
                        var pagibigLoan = $("#pagibigLoan").val();
                        var philhealth = $("#philhealth").val();

                        Ajax.addEmployee(empID, lastname, firstname, middlename,
                               sss, tin, basicSalary, dailyRate, hourlyRate, allowance,
                               sssPremium, sssLoan, pagibigPremium, pagibigLoan,
                               philhealth);

                        $(this).dialog('close');
                        setTimeout("window.location.reload();", 300);

                    },
                    'Cancel': function() {
                        $(this).dialog('close');

                    }
                }
           });
     });
}



function openFile2(url, title, w, h) {

  var win = new Ext.Window({
      autoLoad: url,
      width: w,
      height: h,
      title: title,
      autoScroll:false,
      modal:true,
      buttons: [
         { text:'Attach',
           handler: function() {
               Ajax.upload($("#filename").val(), {
                       callback:function(data) {
                          if (data) {
                              var num = new NumberFormat();
                              num.setNumber(data);
                              num.setPlaces('0', false);
                              num.setSeparators(true, ',', ',');

                             $("#msg").html("<font color='black'><b> Upload Complete! </b><br>"+ num.toFormatted() +" records uploaded.</font>");
                             $("#filename").val(""); 
                          }
                       },
                       errorHandler:function(message) {
                           alert("Oops: " + message);
                       }
                });
           }
         },
         { text:'Cancel',
           handler: function(){
               win.close();
           }
         }
     ]
  });
  win.show();
}

