var thetokenlengthexist=0;
var ajaxcall;//var runajaxfunction;
// ==== event keypress to Tab
$('input, select, textarea').live('keydown',function(e){
    if(e.keyCode==13&&$(this).val()!='') 
    {
        var focusable = $('input,a,select,button,textarea').filter(':visible');
        focusable.eq(focusable.index(this)+1).focus();
        return false;
    }
});

function nextfocus(objsrc,objtarget)
{
    //alert(objsrc+' '+objtarget);
    $("#"+objsrc).keypress(function(e){
        var key=(e.keyCode ? e.keyCode : e.which);
        //alert(key);
        if(key==13&&$("#"+objsrc).val()!='')
        {
            //alert(key+' '+objtarget);
            if(objtarget)
            {
                //$("#"+objtarget).attr('disabled',false);
                $("#"+objtarget).focus();
            }
            else
            {
                e.keyCode=9;
            }
        }//$("#"+objtarget).focus();}
    });
}

function nextfocusnew(objsrc,objtarget)
{
    //alert(objsrc);
    $(objsrc).keypress(function(e){
        var key=(e.keyCode ? e.keyCode : e.which);
        if(key==13&&$(objsrc).val()!=''){e.keyCode=9;}//$("#"+objtarget).focus();}
    });
}

// ===== Number Validation 
function numberonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   // alert(unicode);
    //if(unicode!=8&&unicode!=9&&unicode!=46&&unicode!=37&&unicode!=39)
    //if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=39) ==> Modified 25-01-2014 09:21 Am ( Unicode==39 )Free keycode for single Quote
    //alert(unicode);
    if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=39&&unicode!=46&&unicode!=13)
    {
        if(unicode>=48&&unicode<=57){return true;}
        else if((unicode>=96&&unicode<=105)){return true;}
        else {return false;}
    }
}

function ipkonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   
    //alert(unicode);
    if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=39&&unicode!=190&&unicode!=46&&unicode!=110)
    {
        if(unicode>=48&&unicode<=57){return true;}
        else if((unicode>=96&&unicode<=105)){return true;}
        else {return false;}
        
    }
}

// ===== Char Validation 
function charonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   //alert(unicode);

    //if(withuppercase)
    //{
        unicode=String.fromCharCode(unicode).toUpperCase().charCodeAt(0);  
        //alert(unicode);
    //}
    
    if(unicode!=8&&unicode!=9&&unicode!=32&&unicode!=37&&unicode!=39)
    {
        //alert(unicode);
        if((unicode<65||unicode>90)){return false;}
    }
}

// ===== Char And Symbol Validation 
function charsimbolonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   //alert(unicode);

    //if(withuppercase)
    //{
        unicode=String.fromCharCode(unicode).toUpperCase().charCodeAt(0);  
        //alert(unicode);
    //}
    
    if(unicode!=8&&unicode!=9&&unicode!=32&&unicode!=37&&unicode!=39&&unicode!=43&&unicode!=45)
    {
        //alert(unicode);
        if((unicode<65||unicode>90)){return false;}
    }
}

// ===== Address Validation 
function addressonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   //alert(unicode);
    //var mychar=String.fromCharCode(unicode);
    //if(unicode!=8&&unicode!=9&&unicode!=46&&unicode!=37&&unicode!=39)
    //if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=39) ==> Modified 25-01-2014 09:21 Am ( Unicode==39 )Free keycode for single Quote
    //if(unicode!=8&&unicode!=9&&unicode!=32&&unicode!=188&&unicode!=190&&unicode!=191&&unicode!=110&&unicode!=109&&unicode!=111)
    
    //alert(unicode);
    if(unicode!=8&&unicode!=9&&unicode!=32&&unicode!=188&&unicode!=190&&unicode!=191&&unicode!=110&&unicode!=109&&unicode!=111&&unicode!=173&&unicode!=37&&unicode!=46&&unicode!=39&&unicode!=36&&unicode!=35)
    {                         
        if(unicode>=48&&unicode<=57){return true;}
        else if(unicode>=65&&unicode<=90){return true;}
        else if(unicode>=96&&unicode<=105){return true;}
        else {return false;}
        
        //if(unicode<48||unicode>57){return false;}
        //if((unicode<65||unicode>90)){return false;}
    }
}

function skonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;   //alert(unicode);
    
    unicode=String.fromCharCode(unicode).toUpperCase().charCodeAt(0); 
    if(unicode!=8&&unicode!=9&&unicode!=47&&unicode!=45)
    {                         
        if(unicode>=48&&unicode<=57){return true;}
        else if(unicode>=65&&unicode<=90){return true;}
        //else if(unicode>=96&&unicode<=105){return true;}
        else {return false;}
        
        //if(unicode<48||unicode>57){return false;}
        //if((unicode<65||unicode>90)){return false;}
    }
}

// ===== Number and characterValidation 
function numbercharonly(e,withuppercase)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;
    if(withuppercase)
    {
        unicode=String.fromCharCode(unicode).toUpperCase().charCodeAt(0);  
    }
    
    //if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=39) ==> Modified 25-01-2014 09:19 Am ( Unicode==39 )Free keycode for single Quote
    //alert(unicode);
    if(unicode!=8&&unicode!=9&&unicode!=37&&unicode!=45)
    {
        //if(unicode<48||unicode>57){return false;}
        //alert(unicode);
        if((unicode>=48&&unicode<=57)||(unicode>=65&&unicode<=90)){}
        else{return false;}
    }
}

function teleponcharonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode ;//alert(unicode);
    if(unicode!=8&&unicode!=9&&unicode!=45&&unicode!=95)
    {
        if(unicode>=48&&unicode<=57){return true;}
        else{return false;}
    }
}

function ajaxuri(obj,task,divtarget,withmsginfo,runfunction)
{
    //alert(task);
    if(withmsginfo){$('#'+divtarget).html('wait <img src="themes/images/loader.gif" />');}    
    var x=0;
    ajaxcall=$.ajax({
                type:'POST',
                url:'index.php',
                cache:false,
                data:'ajax=1&sec=1&option='+obj+'&'+task,
                beforeSend:function(data){
                    //$('#'+divtarget).html('t');
                    //$('#'+divtarget).html('wait <img src="themes/images/loader.gif" />'+x);
                },
                success:function(result){
                    var sp=result.split('|');
                    var valresult=result;
                    if(sp.length>1)
                    {
                        category=sp[0];
                        if(category=='msg'){msgtext=sp[1];valresult=sp[2];bootbox.alert(msgtext);}
                    }
                    $('#'+divtarget).show();
                    //bootbox.alert('test');  
                    $('#'+divtarget).html(valresult);
                    if(runfunction){runfunction();}
                }
            });
}

function ajaxuridhtmlx(obj,task)
{
    //alert(obj+task);
    //if(withmsginfo){$('#'+divtarget).html('wait <img src="themes/images/loader.gif" />');}    
    var ajaxcall=$.ajax({
                type:'POST',
                url:'index.php',
                data:'ajax=1&sec=1&option='+obj+'&'+task,
                dataType: 'html',
                context: document.body,
                global: false,
                async:false,
                beforeSend:function(data){
                    //$('#'+divtarget).html('t');
                    //$('#'+divtarget).html('wait <img src="themes/images/loader.gif" />'+x);
                },
                success:function(data){
                    //$('#divtest').html(data);
                }
            }).responseText;
    return ajaxcall;
}

function fn_dhtmlxsetfocus(form,objform)
{
    form.setItemFocus(objform);
}

function fn_dhtmlxloadobject(obj,task)
{
    //alert("?ajax=1&sec=1&option="+obj+"&"+task);
    return "?ajax=1&sec=1&option="+obj+"&"+task;
}

function fn_dhtmlxsenddata(obj,task)
{
    //alert("?ajax=1&sec=1&option="+obj+"&"+task);
    return "?ajax=1&sec=1&option="+obj+"&"+task;
}

function fn_dhtmlxmessage(dtype,msg)
{
    return dhtmlx.message({type:dtype,text:msg});
}

function fn_dhtmlxconfirm(mytitle,mytext)
{
      dhtmlx.confirm({
            title: mytitle,
            type:'confirm',
            ok:"Yes",cancel:"No",
            text: mytext,
            callback: function(result){return result;}
        });
}

function fn_dhtmlxtabbarisexist(tabbId)
{
    var isTabCreated=false;
    myTabbar.forEachCell(function(cell){
        var id=cell.getId();
        //alert(id+' '+tabbId);
        if(id==tabbId){isTabCreated=true;}
    });
    return isTabCreated;
}
//$.when(ajaxcall).then(function(){alert('test');})
function TokenInput(obj,jurl,objnextfocus)
{ 
    var map={};
    var result=[];
    //var locateurl=jurl;
    //alert(obj);
    $("#"+obj+"1").val('');
    $("#"+obj).val('');
    $('#'+obj).typeahead({
        source:function(query,process){
            //var locateurl=jurl;
            $.ajax({
                url:'index.php',
                type:'POST',
                data:jurl+query,
                datatype:'JSON',
                async:true,
                success:function(data){
                    label=[];
                    result=JSON.parse(data);  
                    $.each(result, function (i, item) 
                    {   
                        //if(item=='Record Not Found'){map[item.label]='';label.push('Record Not Found');break;}
                        map[item.label] = name.value;
                        label.push(item.label);
                        
                    });
                 
                    process(label);
                    //process(JSON.parse(data));
                }
            });
          },
        updater: function(item) {
            for(var i=0;i<result.length;i++)
            {
                if(result[i].label==item)
                {
                    $("#"+obj+"1").val(result[i].id);
                    thetokenlengthexit=item.length;
                    $("#"+objnextfocus).focus();
                    break;
                }
            }
            return item;
            //alert(map[item].id);
            //var item=JSON.parse(item);
            //$('#t1').val(item.label);return item;
            //alert("selec"+map[item].id);//return item;
        },
        highlighter:function(item){
            var bld="<i>"+item+"</i>";
            return bld;                            
        }
    });
} 

               
//(function ($, window, delay) {
//  var theTimer = 0;
//  var theElement = null;
//    var theLastPosition = {x:0,y:0};
//    $('[data-toggle]')
        //.closest('li')
//        .on('mouseenter', function (inEvent) {
//        if (theElement) theElement.removeClass('open');
//        window.clearTimeout(theTimer);
//        theElement = $(this);

//        theTimer = window.setTimeout(function () {
//        theElement.addClass('open');
//        }, delay);
//    })
//    .on('mousemove', function (inEvent) {
//            if(Math.abs(theLastPosition.x - inEvent.ScreenX) > 4 || 
//               Math.abs(theLastPosition.y - inEvent.ScreenY) > 4)
//            {
//                theLastPosition.x = inEvent.ScreenX;
//                theLastPosition.y = inEvent.ScreenY;
//                return;
//            }
//            
//        if (theElement.hasClass('open')) return;
//        window.clearTimeout(theTimer);
//        theTimer = window.setTimeout(function () {
//          theElement.addClass('open');
//        }, delay);
//    })
//    .on('mouseleave', function (inEvent) {$(this).closest('li');
//    window.clearTimeout(theTimer);
//    theElement = $(this);
//    theTimer = window.setTimeout(function () {
//      theElement.removeClass('open');
//    }, delay);
//  });
//})(jQuery, window, 200); // 200 is the delay in milliseconds
//$(document).ready(function(){
//$('ul.dropdown-menu').mouseout(function(){ 
//    $('[data-toggle="dropdown"]').parent().removeClass('open');
//});})

// ==== MOdified 19 September 2013 11:24 Am
$(document).ready(function () {

    
    $('ul.nav').each(function(){
        $(this).on("mouseleave",function(){
            $('li.dropdown').each(function () {
                $(this).removeClass('open');
            });
        });
    });
    
    if(typeof String.prototype.trim !== 'function') { 
      String.prototype.trim = function() { 
        return this.replace(/^\s+|\s+$/g, ''); 
      } 
    }
});

// ==== 6 February 2014 14:54 pm
$(function(){
    $('.input').live("keypress", function(e){    
    //$('.nextfocus').keypress(function(e){
            var key=(e.keyCode ? e.keyCode : e.which);
            if(key==13&&$this.val()!=''){e.keyCode=9;}//$("#"+objtarget).focus();}
        });
    });
    
$(function(){
    $('.input').live("keyup", function(e){    
    //$('.nextfocus').keypress(function(e){
            this.value=this.value.toUpperCase();
        });
    });


//  ==== Public function
function showmaskingrupiah(valuess)
{
    var mydivrupiah=document.getElementById('div_rupiahs');
    var lblrupiah=document.getElementById('lblrupiahs');
        lblrupiah.value='0.00';
    if(valuess)
    {                        
        mydivrupiah.style.visibility='visible';
        lblrupiah.innerHTML=numberformat(valuess);
    }
    else
    {
        mydivrupiah.style.visibility='hidden';
    }
}

function hidemaskingrupiah()
{
    var mydivrupiah=document.getElementById('div_rupiahs');
    var lblrupiah=document.getElementById('lblrupiahs');
        lblrupiah.value='0.00';
    mydivrupiah.style.visibility='hidden';
}

function roundednumber(valuess,decimalplaces) // ==== created 25 Oktober 2012 7:15 AM
{
    var sp=valuess.split('.');
    var rtn='0.00';                        
    if(sp.length>1)
    {
        var dec=sp[1].substring(0,decimalplaces);
        if(parseInt(sp[1].substring(decimalplaces,decimalplaces+1))>5&&parseInt(sp[1].substring(decimalplaces,decimalplaces+1))<9)
        {
            dec=sp[1].substring(decimalplaces,decimalplaces+1);
            dec=parseInt(sp[1].substring(0,decimalplaces))+1;
        }
        if(parseInt(dec)>=90)
        {
            sp[0]=parseInt(sp[0])+1;dec='00';
        }
        rtn=sp[0]+'.'+dec;                            
    }
    else
    {
        rtn=valuess;
    }
    
    return rtn;
}

function jqajax(params,div_result,withloadergif)
{
    if(withloadergif)
    {
        //$("#"+div_result).html('<img src=/images/loader.gif/>');
    }
    $.get(params,function(data,status){
        var result=data;
        if(("#"+div_result).length>0)
        {
            $("#"+div_result).html(result);
            return true;
        }
    }); 
}
                    
function plocateurl(com_name,task,criteria,div_target,msg,noprogress)                    
{
    params='ajax=1&option='+com_name+'&task='+task+'&'+criteria;
    if(!msg){msg='wait';}
    loadObject(params,div_target,msg,noprogress);
}

function ajaxurinewtab(obj_folder,obj_name,task,params)
{
    url='?ajax=1&sec=1&dec=1&option='+obj_folder+'&obj='+obj_name+'&task='+task+'&'+params;
    //alert(url);
    $('#fmpreview').prop('action',url);
    $('#fmpreview').submit();

    //url='ajax=1&sec=1&option='+obj_folder+'&obj='+obj_name+'&task='+task+'&'+params;
    //$('#uri').val(url);
    //$('#fmpreview').submit();
}

function loadNewWindow(url,pagewidth,pageheight,scrollbar)
{
    document.body.style.cursor='wait';
    if(!pagewidth){pagewidth=450;}
    if(!pageheight){pageheight=550;}
    if(!scrollbar&&scrollbar!=0){scrollbar=1;}
    //scrollbar=1;
    parentwin=window.open(url,"_blank","top=0,left=0,width="+pagewidth+",height="+pageheight+",toolbar=0,menubar=0,statusbar=0,location=0,scrollbars="+scrollbar);
    if(pagewidth=='100%'){parentwin.resizeTo(screen.width-40,pageheight);}
    document.body.style.cursor='default';
}

function showdate(objTextBox,dateformat)
{
    if(!dateformat){dateformat='dd/mm/yy';}
    $('#'+objTextBox).datepicker({dateFormat: dateformat });
}

function closeobj(obj,objvalue)
{
    var myobj=document.getElementById(obj);
    if(objvalue)
    {
        var myobjvalue=document.getElementById(objvalue);
            myobjvalue.value='';
    
    }
    myobj.style.visibility='hidden';
}

function setpos_Popup(objpopup,objtarget)
{
    //alert(objpopup);
    var x=fn_get_objtext_posX(objtarget);
    var y=fn_get_objtext_posY(objtarget);
    var objdisplay=document.getElementById(objpopup);
        objdisplay.style.visibility='visible';                            
        objdisplay.style.marginTop=parseInt(document.getElementById(objtarget).offsetHeight)+parseInt(y);
        objdisplay.style.marginLeft=parseInt(x);
}

function fn_get_objtext_posX(objtext)
{
    var pos_x= 0;
    var obj=document.getElementById(objtext);
    if(obj.offsetParent) 
    {
        while(1) 
        {
            pos_x+= obj.offsetLeft;
            if(!obj.offsetParent)break;
            obj=obj.offsetParent;
        }
    } 
    else if(obj.x) 
    {pos_x+=obj.x;}
    return pos_x;
}

function fn_get_objtext_posY(objtext)
{
    var pos_y= 0;
    var obj=document.getElementById(objtext);
    if(obj.offsetParent) 
    {
        while(1) 
        {
            pos_y+=obj.offsetTop;
            if(!obj.offsetParent)break;
            obj=obj.offsetParent;
        }
    } 
    else if(obj.y) 
    {pos_y+=obj.y;}
    return pos_y;
}

function fn_numberformat(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)){x1 = x1.replace(rgx, '$1' + '.' + '$2');}
    return x1 + x2;
}

function fn_clsnumberformat(values){return values.replace(/,/gi,'');}

function trim(textstring){return textstring.replace(/^\s+|\s+$/g,'');}

function loadautocomplete(objtextbox,url){setAutoComplete(objtextbox, 'results', url);}

function initial()
{
    var div_body=document.getElementById('div_body');
    var height_tb_header=document.getElementById('tb_header').height;
    var height_tb_menu=document.getElementById('tb_menu').height;
    var height_tb_title=document.getElementById('tb_title').height;
    var height_div_body=document.documentElement.clientHeight-(parseInt(height_tb_header)+parseInt(height_tb_menu)+parseInt(height_tb_title));
    div_body.style.height=height_div_body+'px';
}

function validationobject(spobj)
{
    var spobj=spobj.split(';');
    //alert('test');
    for(i=0;i<spobj.length;i++)
    {
        //alert(spobj[i]);
        spobjval=spobj[i].split(':');
        //alert(spobjval[0].value);
        if(document.getElementById(spobjval[0]).value=='')
        {
            //alert('test');return false;
            //document.getElementById(spobjval[0]).focus();
            bootbox.alert(spobjval[1],function(){if(spobjval[2]=='isfocus'){$('#'+spobjval[0]).focus();}});
            //alert(spobjval[1]);if(spobjval[2]=='isfocus'){$('#'+spobjval[0]).focus();}
            return false;
        }
    }
    return true;

//    var obj=spobj.split(';');
//    for(i=0;i<obj.length;i++)
//    {
//        alert(obj[4]);
//        sp=obj[i].split(':');
//        objname=sp[0];msg=sp[1];isfocus=sp[2];                            
//        if(myobj.value=='')
//        {
//            bootbox.alert(msg,function(result){if(isfocus){$("#"+objname).focus();}});return false;
//        }
//    }
//    return true;
}

function validationobjectwithmsg(spobj)
{
    var spobj=spobj.split(';');
    //alert('test');
    for(i=0;i<spobj.length;i++)
    {
        //alert(spobj[i]);
        spobjval=spobj[i].split(':');
        //alert(spobjval[0].value);
        if(document.getElementById(spobjval[0]).value=='')
        {
            alert(spobjval[1]);
            if(spobjval[2]=='isfocus'){$('#'+spobjval[0]).focus();}
            //alert('test');return false;
            //document.getElementById(spobjval[0]).focus();
            //bootbox.alert(spobjval[1],function(){if(spobjval[2]=='isfocus'){$('#'+spobjval[0]).focus();}});
            return false;
        }
    }
    return true;

//    var obj=spobj.split(';');
//    for(i=0;i<obj.length;i++)
//    {
//        alert(obj[4]);
//        sp=obj[i].split(':');
//        objname=sp[0];msg=sp[1];isfocus=sp[2];                            
//        if(myobj.value=='')
//        {
//            bootbox.alert(msg,function(result){if(isfocus){$("#"+objname).focus();}});return false;
//        }
//    }
//    return true;
}

function findselectedvalue(objcombo,val)
{
    jQuery("#"+objcombo).find("option:contains('"+val+"')").each(function()
    {
     if( jQuery(this).text() == val )
     {
      jQuery(this).attr("selected","selected");
      }
    });
}

function elementtouppercase(obj)
{
    obj.value=obj.value.toUpperCase();
}
function clearobject(spobject,objfocus)
{
    var obj=spobject.split(';');
    for(i=0;i<obj.length;i++)
    {
        var myobj=document.getElementById(obj[i]);
        myobj.value='';
    }
    if(objfocus){$('#'+objfocus).focus();}
}

// === GLobal Event For Element TextBlink css class 26-01-2014 10:89 AM
$('.textblink').each(function(){
    var elem = $(this);
    setInterval(function(){
        if (elem.css('visibility') == 'hidden') 
        {elem.css('visibility', 'visible');} 
        else 
        {elem.css('visibility', 'hidden');}    
    }, 550);
});

// === GLobal Event For Element UpperCase Element css class 29-01-2014 11:35 AM
//$('.uppercase').live('keyup',function(){alert('t');$(this).val().toUpperCase();});

// === Disabled Backward Page
$(document).keydown(function(e) {
    var doPrevent;
    if (e.keyCode == 8){
        var d = e.srcElement || e.target;
        if (d.tagName.toUpperCase() == 'INPUT' || d.tagName.toUpperCase() == 'TEXTAREA') 
        {
            doPrevent = d.readOnly || d.disabled;
        }
        else
            doPrevent = true;
    }
    else
        doPrevent = false;

    if (doPrevent)
        e.preventDefault();
});




//$('input[type="text"]').change(function() {alert('t');
//    $(this).css({'background-color':'white'});
//});
