// JavaScript Document
function isValidEmail(strEmail){
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

    // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) {
        return false;
    }
    return true;
}
function showregmessage(html,  left, top,width, height) {
    alert(html);
}
//"4px solid #000"
function creatediv(id, html,  left, top,width, height,color,borderstyle) {

    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', id);

    if (width) {
        newdiv.style.width = width;
    }

    if (height) {
        newdiv.style.height = height;
    }

    if ((left || top) || (left && top)) {
        newdiv.style.position = "absolute";

        if (left) {
            newdiv.style.left = left;
        }

        if (top) {
            newdiv.style.top = top;
        }
    }

    newdiv.style.background = color;
    newdiv.style.border = borderstyle;

    if (html) {
        newdiv.innerHTML = html;
    } else {
        newdiv.innerHTML = "nothing";
    }

    document.body.appendChild(newdiv);

}
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);
}
// EXTJS GUI functions
function vclPanelToExtPanel(vclPanel,panelTitle,layoutName,autoCollapsible) {
    if (layoutName==null){layoutName='fit';}
    if (autoCollapsible==null){autoCollapsible=true;}
    var xx=new Ext.Panel({
            //applyTo:'Panel1_outer',
            contentEl:vclPanel+'_table',
            //region: 'center',
            title:panelTitle,
            collapsible:autoCollapsible,
            scripts:true,
            split: true,
            layout:layoutName
            //width:800,
            //height:320
            //renderTo:Ext.getBody()
            //closeAction:'close',
            //plain: true

        });
    xx.applyToMarkup(vclPanel+'_outer');
}
/* panels functions */
function vp_shd(vclPanel,title) {
 var html='<div class="sidebox">'+
          '<div class="boxhead"><h2>'+title+'</h2></div>'+
          '<div id="'+vclPanel+'_panel" class="boxbody">'+
          '</div>';
 var addTo=$('#'+vclPanel+'_table')[0];
 if (addTo!=null)
 {
  var ohtml=$(html);
  ohtml.insertBefore(addTo);
  $('#'+vclPanel+'_table').appendTo('#'+vclPanel+'_panel');
 }

}
function nf(vclPanel,title,color) {
 if (color==undefined)
 {var color='#C9F76F';}
 var html='<div id="'+vclPanel+'_nifty" style="background-color:'+color+';">'+
          '</div>';
 var vclTable=$('#'+vclPanel+'_table')[0];
 if (vclTable!=null)
 {
  var ohtml=$(html);
  ohtml.insertBefore(vclTable);
  $('#'+vclPanel+'_table').appendTo('#'+vclPanel+'_nifty');
  Nifty('div#'+vclPanel+'_nifty','big');
 }
}
function nfdiv(divId,title,color) {
 if (color==undefined)
 {var color='#C9F76F';}
 var html='<div id="'+divId+'_nifty" style="background-color:'+color+';">'+
          '</div>';
 var div=$('#'+divId)[0];
 if (div!=null)
 {
  var ohtml=$(html);
  ohtml.insertBefore(div);
  $('#'+divId).appendTo('#'+divId+'_nifty');
  Nifty('div#'+divId+'_nifty','big');
 }
}
/* before jquery panel update
function vp(vclPanel,title,color)
{
 if (color==undefined)
 {var color='#C9F76F';}

 var html='<div id="'+vclPanel+'_head" style="padding:5px;font-size:14px;height:20px;">'
          +'<span>'+title+'</span>'+
          '</div>';
 var addTo=$('#'+vclPanel+'_table')[0];
 if (addTo!=null)
 {
  var ohtml=$(html);
  ohtml.insertBefore(addTo);
  nfdiv(vclPanel+'_head','normal',color);
 }
}
*/
function vp(vclPanel,title,collapse)
{
 if (collapse==undefined)
 {var collapse=false;}

 var html='<div id="'+vclPanel+'_head" class="collapsibleContainer" title="'+title+'"></div>';
 var addTo=$('#'+vclPanel+'_table')[0];
 if (addTo!=null)
 {
  var ohtml=$(html);
  ohtml.insertBefore(addTo);
  $('#'+vclPanel+'_table').appendTo('#'+vclPanel+'_head');
  $("#"+vclPanel+'_head').collapsiblePanel(collapse);
 }
}

function extPanelFromUrl(vclPanel,panelTitle,layoutName,autoCollapsible,url) {
    if (layoutName==null){layoutName='fit';}
    if (autoCollapsible==null){autoCollapsible=true;}
    var xx=new Ext.Panel({
            //applyTo:'Panel1_outer',
            contentEl:vclPanel+'_table',
            //region: 'center',
            title:panelTitle,
            collapsible:autoCollapsible,
            scripts:true,
            split: true,
            layout:layoutName,
            autoLoad:url
            //width:800,
            //height:320
            //renderTo:Ext.getBody()
            //closeAction:'close',
            //plain: true

        });
    xx.applyToMarkup(vclPanel+'_outer');
}
function loadApp(vclPanel,panelTitle,layoutName,autoCollapsible,url) {
    if (layoutName==null){layoutName='fit';}
    if (autoCollapsible==null){autoCollapsible=true;}
    var xx=new Ext.Panel({
            //applyTo:'Panel1_outer',
            applyTo:vclPanel+'_table',
            //region: 'center',
            title:panelTitle,
            collapsible:autoCollapsible,
            scripts:true,
            split: true,
            layout:layoutName,
            html:'<iframe src="'+url+'" width="600" height="600"/>'
            //width:800,
            //height:320
            //renderTo:Ext.getBody()
            //closeAction:'close',
            //plain: true

        });
    //xx.applyToMarkup(vclPanel+'_outer');
}

function getPanels(panels) {
    var xx=new Array();
    for(i=0;i<panels.length;i++) {
        xx[i]=new Ext.Panel({
            applyTo:panels[i][0]+'_outer',
            contentEl:panels[i][0]+'_table',
            region:panels[i][1],
            title:'test',
            collapsible:true,
            split: true,
            scripts:true,
            layout:'fit'
            //width:800,
            //height:320
            //renderTo:Ext.getBody()
            //closeAction:'close',
            //plain: true

        });



    }
    return xx;
}
/*function getPanels(panels)
{
    var xx=new Array();
    for(i=0;i<panels.length;i++) {
        xx[i]=new Ext.BoxComponent({
            height: 300, // give north and south regions a height
            region: panels[i][1],
            items:[
            new Ext.Panel({
                applyTo:panels[i][0]+'_outer',
                contentEl:panels[i][0]+'_table',

                title:'test',
                collapsible:true,
                split: true,
                scripts:true
                //layout:layoutName
                //width:800,
                //height:320
                //renderTo:Ext.getBody()
                //closeAction:'close',
                //plain: true

            })]});



    }
return xx;
    }*/

function testGUI(panels) {
    var viewport = new Ext.Viewport({
        //contentEl:Ext.getBody(),
        items:getPanels(panels),
        layout: 'border',
        scripts:true,
        anchorSize: {width:500, height:400},
        applyTo:'panelForm_outer'
    });
    //viewport.show();
    viewport.doLayout();
}
function showMessage(title,msg,msgType)
{
if (msgType==null)
{
 msgType='ext-mb-warning';
}
 Ext.MessageBox.show({
           title:title,
           msg:msg,
           buttons: Ext.MessageBox.OK,
           icon:msgType
       });
}
function unixTimeToTime(unixTime)
{
 var date = new Date(unixTime*1000);
 var time = date.getFullYear()+'-'+date.getMonth()+'-'+date.getDate()+
            ' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
 return time;

}
 function mysqlTimeStampToDate(timestamp) {
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1],parts[2],parts[3],parts[4],parts[5]);
  }
function decodeUrl(ss) {
  var encoded =ss;
  return decodeURIComponent(encoded.replace(/\+/g,  " "));
}
function encodeUrl(ss) {
  var encoded =ss;
  return encodeURIComponent(encoded);
}
// Img Uploader functions
function showImgUploader(id)
{
 $.openDOMWindow({
    height:550,
    width:630,
    loader:1,
    loaderImagePath:'/images/animationProcessing.gif',
    loaderHeight:16,
    loaderWidth:17,
    windowSource:'iframe',
    windowSourceURL:'/common/imguploader.php?id='+id,
    windowHTTPType:'get'
    });
}
function updateImg(id,imgUrl)
{
 $('#hf'+id)[0].value=imgUrl;
 $('#img'+id)[0].src=imgUrl;
}
// end Img Uploader functions
function closeDomWindow()
{
 $.closeDOMWindow();
}
// Jquery Panel
(function($) {
    $.fn.extend({
        collapsiblePanel: function() {
            // Call the ConfigureCollapsiblePanel function for the selected element
            return $(this).each(ConfigureCollapsiblePanel);
        }
    });

})(jQuery);

function ConfigureCollapsiblePanel(collapse) {
    if (collapse==null)
    {
     collapse=false;
    }
    $(this).addClass("ui-widget");

    // Wrap the contents of the container within a new div.
    $(this).children().wrapAll("<div class='collapsibleContainerContent ui-widget-content'></div>");

    // Create a new div as the first item within the container.  Put the title of the panel in here.
    $("<div class='collapsibleContainerTitle ui-widget-header'><div>" + $(this).attr("title") + "</div></div>").prependTo($(this));
    if (collapse==true)
    {
    // Assign a call to CollapsibleContainerTitleOnClick for the click event of the new title div.
     $(".collapsibleContainerTitle", this).click(CollapsibleContainerTitleOnClick);
    }
}

function CollapsibleContainerTitleOnClick() {
    // The item clicked is the title div... get this parent (the overall container) and toggle the content within it.
    $(".collapsibleContainerContent", $(this).parent()).slideToggle();
}

