function date ( format, timestamp ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +      parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: MeEtc (http://yass.meetcweb.com)
    // +   improved by: Brad Touesnard
    // +   improved by: Tim Wiel
    // +   improved by: Bryan Elliott
    // +   improved by: Brett Zamir (http://brettz9.blogspot.com)
    // +   improved by: David Randall
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brettz9.blogspot.com)
    // %        note 1: Uses global: php_js to store the default timezone
    // *     example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
    // *     returns 1: '09:09:40 m is month'
    // *     example 2: date('F j, Y, g:i a', 1062462400);
    // *     returns 2: 'September 2, 2003, 2:26 am'
    // *     example 3: date('Y W o', 1062462400);
    // *     returns 3: '2003 36 2003'
    // *     example 4: x = date('Y m d', (new Date()).getTime()/1000); // 2009 01 09
    // *     example 4: (x+'').length == 10
    // *     returns 4: true
 
    var a, tal=[], jsdate=(
        (typeof(timestamp) == 'undefined') ? new Date() : // Not provided
        (typeof(timestamp) == 'number') ? new Date(timestamp*1000) : // UNIX timestamp
        new Date(timestamp) // Javascript Date()
    );
    var pad = function(n, c){
        if( (n = n + "").length < c ) {
            return new Array(++c - n.length).join("0") + n;
        } else {
            return n;
        }
    };
    var ret = '';
    var txt_weekdays = ["Sunday","Monday","Tuesday","Wednesday",
        "Thursday","Friday","Saturday"];
    var txt_ordin = {1:"st",2:"nd",3:"rd",21:"st",22:"nd",23:"rd",31:"st"};
    var txt_months =  ["", "January", "February", "March", "April",
        "May", "June", "July", "August", "September", "October", "November",
        "December"];
 
    var f = {
        // Day
            d: function(){
                return pad(f.j(), 2);
            },
            D: function(){
                var t = f.l();
                return t.substr(0,3);
            },
            j: function(){
                return jsdate.getDate();
            },
            l: function(){
                return txt_weekdays[f.w()];
            },
            N: function(){
                return f.w() + 1;
            },
            S: function(){
                return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th';
            },
            w: function(){
                return jsdate.getDay();
            },
            z: function(){
                return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0;
            },
 
        // Week
            W: function(){
                var a = f.z(), b = 364 + f.L() - a;
                var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;
 
                if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){
                    return 1;
                } else{
 
                    if(a <= 2 && nd >= 4 && a >= (6 - nd)){
                        nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");
                        return date("W", Math.round(nd2.getTime()/1000));
                    } else{
                        return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
                    }
                }
            },
 
        // Month
            F: function(){
                return txt_months[f.n()];
            },
            m: function(){
                return pad(f.n(), 2);
            },
            M: function(){
                var t;
                t = f.F(); return t.substr(0,3);
            },
            n: function(){
                return jsdate.getMonth() + 1;
            },
            t: function(){
                var n;
                if( (n = jsdate.getMonth() + 1) == 2 ){
                    return 28 + f.L();
                } else{
                    if( n & 1 && n < 8 || !(n & 1) && n > 7 ){
                        return 31;
                    } else{
                        return 30;
                    }
                }
            },
 
        // Year
            L: function(){
                var y = f.Y();
                return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0;
            },
            o: function(){
                if (f.n() === 12 && f.W() === 1) {
                    return jsdate.getFullYear()+1;
                }
                if (f.n() === 1 && f.W() >= 52) {
                    return jsdate.getFullYear()-1;
                }
                return jsdate.getFullYear();
            },
            Y: function(){
                return jsdate.getFullYear();
            },
            y: function(){
                return (jsdate.getFullYear() + "").slice(2);
            },
 
        // Time
            a: function(){
                return jsdate.getHours() > 11 ? "pm" : "am";
            },
            A: function(){
                return f.a().toUpperCase();
            },
            B: function(){
                // peter paul koch:
                var off = (jsdate.getTimezoneOffset() + 60)*60;
                var theSeconds = (jsdate.getHours() * 3600) +
                                 (jsdate.getMinutes() * 60) +
                                  jsdate.getSeconds() + off;
                var beat = Math.floor(theSeconds/86.4);
                if (beat > 1000) beat -= 1000;
                if (beat < 0) beat += 1000;
                if ((String(beat)).length == 1) beat = "00"+beat;
                if ((String(beat)).length == 2) beat = "0"+beat;
                return beat;
            },
            g: function(){
                return jsdate.getHours() % 12 || 12;
            },
            G: function(){
                return jsdate.getHours();
            },
            h: function(){
                return pad(f.g(), 2);
            },
            H: function(){
                return pad(jsdate.getHours(), 2);
            },
            i: function(){
                return pad(jsdate.getMinutes(), 2);
            },
            s: function(){
                return pad(jsdate.getSeconds(), 2);
            },
            u: function(){
                return pad(jsdate.getMilliseconds()*1000, 6);
            },
 
        // Timezone
            e: function () {
//                var abbr='', i=0;
//                if (this.php_js && this.php_js.default_timezone) {
//                    return this.php_js.default_timezone;
//                }
//                if (!tal.length) {
//                    tal = timezone_abbreviations_list();
//                }
//                for (abbr in tal) {
//                    for (i=0; i < tal[abbr].length; i++) {
//                        if (tal[abbr][i].offset === -jsdate.getTimezoneOffset()*60) {
//                            return tal[abbr][i].timezone_id;
//                        }
//                    }
//                }
                return 'UTC';
            },
            I: function(){
                var DST = (new Date(jsdate.getFullYear(),6,1,0,0,0));
                DST = DST.getHours()-DST.getUTCHours();
                var ref = jsdate.getHours()-jsdate.getUTCHours();
                return ref != DST ? 1 : 0;
            },
            O: function(){
               var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4);
               if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;
               return t;
            },
            P: function(){
                var O = f.O();
                return (O.substr(0, 3) + ":" + O.substr(3, 2));
            },
            T: function () {
//                var abbr='', i=0;
//                if (!tal.length) {
//                    tal = timezone_abbreviations_list();
//                }
//                if (this.php_js && this.php_js.default_timezone) {
//                    for (abbr in tal) {
//                        for (i=0; i < tal[abbr].length; i++) {
//                            if (tal[abbr][i].timezone_id === this.php_js.default_timezone) {
//                                return abbr.toUpperCase();
//                            }
//                        }
//                    }
//                }
//                for (abbr in tal) {
//                    for (i=0; i < tal[abbr].length; i++) {
//                        if (tal[abbr][i].offset === -jsdate.getTimezoneOffset()*60) {
//                            return abbr.toUpperCase();
//                        }
//                    }
//                }
                return 'UTC';
            },
            Z: function(){
               var t = -jsdate.getTimezoneOffset()*60;
               return t;
            },
 
        // Full Date/Time
            c: function(){
                return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P();
            },
            r: function(){
                return f.D()+', '+f.d()+' '+f.M()+' '+f.Y()+' '+f.H()+':'+f.i()+':'+f.s()+' '+f.O();
            },
            U: function(){
                return Math.round(jsdate.getTime()/1000);
            }
    };
 
    return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){
        if( t!=s ){
            // escaped
            ret = s;
        } else if( f[s] ){
            // a date function exists
            ret = f[s]();
        } else{
            // nothing special
            ret = s;
        }
 
        return ret;
    });
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function addRow(tableId, item){
    if (item1) {
        // assign long reference to shorter var name
        var theTable = document.all.tableId
        // append new row to the end of the table
        var newRow = theTable.insertRow(theTable.rows.length)
        // give the row its own ID
        newRow.id = newRow.uniqueID
        
        // declare cell variable
        var newCell
        
        // an inserted row has no cells, so insert the cells
        newCell = newRow.insertCell(0)
        // give this cell its own id
        newCell.id = newCell.uniqueID
        // display the row's id as the cell text
        newCell.innerText = newRow.id
        newCell.bgColor = "yellow"
        // reuse cell var for second cell insertion
        newCell = newRow.insertCell(1)
        newCell.id = newCell.uniqueID
        newCell.innerText = item1
    }
}
function gotoUrl(url) {
  if (url == "")
    return;
  location.href = url;
}

function comment_checkInput(){
    /*
	if(!document.comment.author.value) {
	 alert("Please enter name.");
	 document.comment.author.focus();
	 return false;
	}
	
	if(isEmpty(document.comment.author)) {
	 alert("Please enter name.");
	 document.comment.author.focus();
	 return false;
	}
	
	if(!document.comment.email.value) {
	 alert("Please enter email address.");
	 document.comment.email.focus();
	 return false;
	}
	
	if (document.comment.email.value.indexOf("@") < 0){
	alert('Email format is not valid. Please re-enter.');
	document.comment.email.focus();
	return false;
	}
	
	if (document.comment.email.value.indexOf("/") >= 0){
	 alert('Email format is not valid. Please re-enter.');
	 document.comment.email.focus();
	return false;
	
	}
	*/
	if (isEmpty(document.comment.comment)){
	 alert('Please enter comment.');
	 document.comment.comment.focus();
	return false;
	
	}	
	/*
	if(!document.comment.comment.value) {
	 alert("Please enter name.");
	 document.comment.comment.focus();
	 return false;
	}	
    */
	document.comment.submit();
}


function isEmpty(thefield) {
    var re = /^\s{1,}$/g; //match white space i.e. space, tab, form-feed, etc.
    if ((thefield.value.length==0) || (thefield.value==null)
            || ((thefield.value.search(re)) > -1)) {
        return true;
    }
    return false;
}

function input_change_font_color(id) {
document.getElementById(id).style.color="#000"; 
}

function input_clearbg(id) {
document.getElementById(id).style.background="#ffffff"; 
}



 function total_cal(id){
               var preName,startTimeHourValue,finishTimeHourValue,startTimeMinValue,finishTimeMinValue,lunchTimeValue,dailyTotal,i;
               var startTimeHour='_start_hour';
               var finishTimeHour='_finish_hour';
               var startTimeMin='_start_min';
               var finishTimeMin='_finish_min';
               var lunchTime='_lunch_time';
               var objID=document.getElementById(id).id;
               var totalPreName = new Array ("mon_daily_total","tue_daily_total","wed_daily_total","thu_daily_total","fri_daily_total","sat_daily_total","sun_daily_total");
                   preName=objID.substr(0,3);
               startTimeHourValue=document.getElementById(preName+startTimeHour).value;
               finishTimeHourValue=document.getElementById(preName+finishTimeHour).value;
               startTimeMinValue=document.getElementById(preName+startTimeMin).value;
               finishTimeMinValue=document.getElementById(preName+finishTimeMin).value;  
               lunchTimeValue=document.getElementById(preName+lunchTime).value;
               
               //alert(finishTimeHourValue);
               
               finishDay=0;
               if(parseFloat(finishTimeHourValue+'.'+finishTimeMinValue) < parseFloat(startTimeHourValue+'.'+startTimeMinValue)) {
            	   finishDay=1;
               }
               
               if (startTimeHourValue!=" " && finishTimeHourValue!=" " && startTimeMinValue!=" " && finishTimeMinValue!=" " )
               {   
              // var s = (new Date(0,0,0,startTimeHourValue,startTimeMinValue,0).valueOf()-new Date(0,0,0,finishTimeHourValue,finishTimeMinValue,0).valueOf())/3600000;
                 var s = (new Date(0,0,0,startTimeHourValue,startTimeMinValue,0).valueOf()-new Date(0,0,finishDay,finishTimeHourValue,finishTimeMinValue,0).valueOf())/60000;
               //s = Math.floor(s * 10) / -10;
                 s = s/-1;
               if (lunchTimeValue){
               //s=s-(lunchTimeValue/60);
               s = s-lunchTimeValue;
               }    
               
                modmin = s % 60;
                if ( modmin <=7 )
                   s = s - modmin;
                if ( modmin >7 && modmin <=22)
                   s = s - modmin + 15;
                if ( modmin >22 && modmin <=37)
                   s = s - modmin + 30;
                if ( modmin >37 && modmin <=52)
                   s = s - modmin + 45;
                if ( modmin >52 )
                   s = s - modmin + 60;      
                
                     
               //document.getElementById(preName+'_daily_total').value=Math.round(s*100)/100;
               document.getElementById(preName+'_daily_total').value= s / 60;
              }
              //Calculate total
              dailyTotal=0;
               for (i in totalPreName){
                   if (document.getElementById(totalPreName[i]).value){
                    dailyTotal = parseFloat(document.getElementById(totalPreName[i]).value) + parseFloat(dailyTotal);
                   }
               }
               
               if(dailyTotal!=0){ 
               document.getElementById('total').value=dailyTotal;
               }
           }
           
function change_date() {
 
        var week_end_date = parseInt(document.getElementById("week_end_date").value);
        var mon = week_end_date - (6*24*3600);
        var tue = week_end_date - (5*24*3600);
        var wed = week_end_date - (4*24*3600);
        var thu = week_end_date - (3*24*3600);
        var fri = week_end_date - (2*24*3600);
        var sat = week_end_date - (1*24*3600);
        var sun = week_end_date ;
        
        document.getElementById("mon_date").value = mon;
        document.getElementById("tue_date").value = tue;
        document.getElementById("wed_date").value = wed;
        document.getElementById("thu_date").value = thu;
        document.getElementById("fri_date").value = fri;
        document.getElementById("sat_date").value = sat;
        document.getElementById("sun_date").value = sun;
         

        document.getElementById("mon_date_display").value = date('d-m-Y',mon);
        document.getElementById("tue_date_display").value = date('d-m-Y',tue);
        document.getElementById("wed_date_display").value = date('d-m-Y',wed);
        document.getElementById("thu_date_display").value = date('d-m-Y',thu);
        document.getElementById("fri_date_display").value = date('d-m-Y',fri);
        document.getElementById("sat_date_display").value = date('d-m-Y',sat);
        document.getElementById("sun_date_display").value = date('d-m-Y',sun);        
}       


   
function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
     for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'chk_add_groups[all]'){
      theForm[z].checked = theElement.checked;
      }
     }
}


function change_total() {

     var mon = document.getElementById("mon_daily_total").value;
     var tue = document.getElementById("tue_daily_total").value;
     var wed = document.getElementById("wed_daily_total").value;
     var thu = document.getElementById("thu_daily_total").value;
     var fri = document.getElementById("fri_daily_total").value;
     var sat = document.getElementById("sat_daily_total").value;
     var sun = document.getElementById("sun_daily_total").value;
     var total;
     
    mon = (mon != "") ?parseFloat(mon):0;
    tue = (tue != "") ?parseFloat(tue):0; 
    wed = (wed != "") ?parseFloat(wed):0;
    thu = (thu != "") ?parseFloat(thu):0;
    fri = (fri != "") ?parseFloat(fri):0;
    sat = (sat != "") ?parseFloat(sat):0;
    sun = (sun != "") ?parseFloat(sun):0; 
    total = mon + tue + wed + thu + fri + sat + sun;
     
    document.getElementById("total").value = total;
}

function check_confirm() {
    if ( confirm('Delete selected timesheet?')){
          document.back_form.submit();
    
    } else {
          return false;
    }
}

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function check_new_candidate(){
    if  (document.getElementById('candidate_first').value =='' || document.getElementById('candidate_last').value =='' ) {
        alert('Please input both Candidate first name and last name.');
        return false;
    }else {
        return true;
    }

}

function check_new_job(){
    if (document.getElementById('job_name').value ==''){
        alert('Please input Job title.');
        return false;
    }else{
        return true;
    }
}


function clearLine(date) {
               var startTimeHour='_start_hour';
               var finishTimeHour='_finish_hour';
               var startTimeMin='_start_min';
               var finishTimeMin='_finish_min';
               var lunchTime='_lunch_time';
               var dailyTotal = '_daily_total';
               var notes = '_notes';
               var t1 = '_t1';
               var t1_5 = '_t1_5';
               var t2 = '_t2';
               var travel = '_travel';
               var meal = '_meal';
               var shift = '_shift';
               
    document.getElementById(date+startTimeHour).value=' ';
    document.getElementById(date+finishTimeHour).value=' ';
    document.getElementById(date+startTimeMin).value=' ';
    document.getElementById(date+finishTimeMin).value=' ';
    document.getElementById(date+lunchTime).value='';
    document.getElementById(date+dailyTotal).value='';
    
    if (document.getElementById(date+notes))
        document.getElementById(date+notes).value='';
    
    if (document.getElementById(date+t1))
    document.getElementById(date+t1).value='';
    if (document.getElementById(date+t1_5))
    document.getElementById(date+t1_5).value='';
    if (document.getElementById(date+t2))
    document.getElementById(date+t2).value='';
    if (document.getElementById(date+travel))
    document.getElementById(date+travel).value='';
    if (document.getElementById(date+meal))
    document.getElementById(date+meal).value='';
    if (document.getElementById(date+shift))
    document.getElementById(date+shift).value='';
    
    change_total();

}