check_cookies();

var  INDEX = '/';                       //  var  INDEX = '/index.php/';
var  SERVER_NAME = document.location.protocol + '//' + document.location.host + '/';

setInterval( keep_alive, 60000 );       //  session expires at 1 min = 60 sec X 1000


function hint_on_mouse_over( pField ) {
     var  myHint = get_id( pField.id );
     myHint.style.display = 'none';
}

function hint_on_blur( pField ) {
     if(  pField.value == '' ) {
          var  myHint = get_id( pField.name + '_hint' );
          myHint.style.display = 'block';
     }
}

function hint_on_focus( pField ) {
     if(  pField.value != '' ) {
          var  myHint = get_id( pField.name + '_hint' );
          myHint.style.display = 'none';
     }
}

function hint_on_key_up( pField ) {
     if(  pField.value != '' ) {
          var  myHint = get_id( pField.name + '_hint' );
          myHint.style.display = 'none';
     }
}

function beep() {
     var  beep = get_id( 'beep' );
     var  html = '<embed src="' + SERVER_NAME + 'beep-8.wav" hidden=true autostart=true loop=false>';
//     beep.innerHTML = html;                //   this cause more problems than help
}

//   Place your application-specific JavaScript functions and classes here
//   This file is automatically included by javascript_include_tag :defaults

function my_request() {
     var  request;
          if(  window.XMLHttpRequest )  { request = new XMLHttpRequest(); }                     // IE7, Mozilla,...
     else if(  window.ActiveXObject  )  { request = new ActiveXObject( "Microsoft.XMLHTTP" ); } // IE6, IE6
     else                               { request = null; }
     if(  request == null || request == undefined )
          return null;
     else return request;
}

function keep_alive() {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/setlanguage?language=', false );
//   request.open( 'GET', SERVER_NAME + 'ajax/keepalive', false );
     request.send( null );
};

function save_textarea( pField ) {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/savetextarea?textarea=' + pField.value, false );
     request.send( null );
}

function save_reply( pField, pType ) {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/savereply?type=' + pType + '&textarea=' + pField.value, false );
     request.send( null );
}

/*
var XPostForm = {
     toggle: function(){
          var container = $( 'form_container' );
          var form = $$( '#form_container form' ).first();

          if(  container.hasClassName( 'active' ))
               form.visualEffect( 'blind_up'  , { duration: 0.25, afterFinish: function() { container.removeClassName( 'active' ); }});
          else form.visualEffect( 'blind_down', { duration: 0.50, beforeStart: function() { container.addClassName   ( 'active' ); }});
     }
}

function Xtoggle( div ) {
alert( div );
     var  container  = $( div );
     var  form = $$( '#' + div + ' form' ).first();

     if(  container.hasClassName( 'active' )
          form.visualEffect( 'blind_up'  , { duration: 0.25, afterFinish: function() { container.removeClassName( 'active' ); }});
     else form.visualEffect( 'blind_down', { duration: 0.50, beforeStart: function() { container.addClassName   ( 'active' ); }});
}
*/

//   ajax for Company Name -----------------------------------------------------

function search_company_name( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc == 9 || kc == 13 ) {
          pEvent.returnValue = false;
          pEvent.cancelBuble = true;
//          set_value( 'company_name', pField.value );
          pEvent = false;
          return false;
     } else {
     if(  kc >= 32 )
          ajax( SERVER_NAME + 'ajax/companies?key=' + pField.value, process_company_name, pField );
     }
}

function process_company_name( pField, pResponse ) {
     var  rows = eval( '(' + pResponse + ')' );
     var  elem = get_id( 'company_names' );
     elem.style.visibility = 'visible';
     elem.innerHTML = '';

     if(  rows.length == 1 )
          set_company_name( rows[ 0 ].id, rows[ 0 ].name );
     if(  rows.length <= 1 )
          return;

     var  elem_ul = document.createElement( 'ul' );
     elem_ul.className = 'search_names';
     
     for( var i=0; i< rows.length; i++ ) {
          var  text_name = rows[ i ].name;
          var  text_node = document.createTextNode( text_name );
          var  anchor    = document.createElement( 'a' );
          anchor.href    = 'javascript:set_company_name( ' + rows[ i ].id + ', "' + rows[ i ].name + '" );';
          anchor.appendChild( text_node );

          var  elem_li   = document.createElement( 'li' );
          elem_li.appendChild( anchor );
          elem_li.style.listStyleType = 'none';
          elem_ul.appendChild( elem_li );
     }
     elem.appendChild( elem_ul );
}

//   ajax for User Name -----------------------------------------------------

function search_user_name( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc == 9 || kc == 13 ) {
          pEvent.returnValue = false;
          pEvent.cancelBuble = true;
//          set_value( 'user_name', pField.value );
          pEvent = false;
          return false;
     } else {
     if(  kc >= 32 )
          ajax( SERVER_NAME + 'ajax/usersname?company_id=' + JKY_company_id + '&key=' + pField.value, process_user_name, pField );
     }
}

function put_name( pName ) {
     return pName.replace( '|', ' ' ).replace( '|', ' ' );
}

function process_user_name( pField, pResponse ) {
     var  rows = eval( '(' + pResponse + ')' );
     var  elem = get_id( 'user_names' );
     elem.style.visibility = 'visible';
     elem.innerHTML = '';

     if(  rows.length == 1 )
          set_user_name( rows[ 0 ].id, rows[ 0 ].name );
     if(  rows.length <= 1 )
          return;

     var  elem_ul = document.createElement( 'ul' );
     elem_ul.className = 'search_names';
     
     for( var i=0; i< rows.length; i++ ) {
          var  text_name = put_name( rows[ i ].name );
          var  text_node = document.createTextNode( text_name );
          var  anchor    = document.createElement( 'a' );
          anchor.href    = 'javascript:set_user_name( ' + rows[ i ].id + ', "' + text_name + '" );';
          anchor.appendChild( text_node );

          var  elem_li   = document.createElement( 'li' );
          elem_li.appendChild( anchor );
          elem_li.style.listStyleType = 'none';
          elem_ul.appendChild( elem_li );
     }
     elem.appendChild( elem_ul );
}

//   ajax for User Email -------------------------------------------------------
function search_user_email( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc == 9 || kc == 13 ) {
          pEvent.returnValue = false;
          pEvent.cancelBuble = true;
          pEvent = false;
          return false;
     } else {
     if(  kc >= 32 )
//        ajax( SERVER_NAME + 'ajax/usersemail?company_id=' + JKY_company_id + '&key=' + pField.value, process_user_email, pField );
          ajax( SERVER_NAME + 'ajax/usersemail?key=' + pField.value, process_user_email, pField );
     }
}

function process_user_email( pField, pResponse ) {
     var  rows = eval( '(' + pResponse + ')' );
     var  elem = get_id( 'user_emails' );
     elem.style.visibility = 'visible';
     elem.innerHTML = '';

     if(  rows.length == 1 )
          set_user_email( rows[ 0 ].id, rows[ 0 ].email, rows[ 0 ].company_id, rows[ 0 ].company );
     if(  rows.length <= 1 )
          return;

     var  elem_ul = document.createElement( 'ul' );
     elem_ul.className = 'search_names';
     
     for( var i=0; i< rows.length; i++ ) {
          var  email = rows[ i ].email;
          if(  rows[ i ].company == null )        rows[ i ].company = '';
          var  text_node = document.createTextNode( email + ' [ ' + rows[ i ].company.substr( 0, 40 ) + ' ] ' );
          var  anchor    = document.createElement( 'a' );
          anchor.href    = 'javascript:set_user_email( ' + rows[ i ].id + ', "' + rows[ i ].email + '", ' + rows[ i ].company_id + ', "' + rows[ i ].company + '" );';
          anchor.appendChild( text_node );

          var  elem_li   = document.createElement( 'li' );
          elem_li.appendChild( anchor );
          elem_li.style.listStyleType = 'none';
          elem_ul.appendChild( elem_li );
     }
     elem.appendChild( elem_ul );
}

//   ajax for Client Name ------------------------------------------------------

function search_client_name( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc == 9 || kc == 13 ) {
          pEvent.returnValue = false;
          pEvent.cancelBuble = true;
//          set_value( 'client_name', pField.value );
          pEvent = false;
          return false;
     } else {
     if(  kc >= 32 ) {
          var   first_name = get_value(  'first_name' );
          var  middle_name = get_value( 'middle_name' );
          var    last_name = get_value(   'last_name' );
          var  key = first_name + '%|%' + middle_name + '%|%' + last_name;
          ajax( SERVER_NAME + 'ajax/users?key=' + key, process_client_name, pField );
     }}
}

function process_client_name( pField, pResponse ) {
     var  rows = eval( '(' + pResponse + ')' );
     var  elem = get_id( 'client_names' );
     elem.style.visibility = 'visible';
     elem.innerHTML = '';

     if(  rows.length == 1 )
          set_client_name( rows[ 0 ].id, rows[ 0 ].name, rows[ 0 ].email, rows[ 0 ].phone, rows[ 0 ].mobile, rows[ 0 ].company );
     if(  rows.length <= 1 )
          return;

     var  elem_ul = document.createElement( 'ul' );
     elem_ul.className = 'search_names';
     
     for( var i=0; i< rows.length; i++ ) {
          var  name      = put_name( rows[ i ].name );
          if(  rows[ i ].company == null )        rows[ i ].company = '';
//        var  text_name = rows[ i ].name;
          var  text_node = document.createTextNode( name + ' [ ' + rows[ i ].company.substr( 0, 40 ) + ' ]' );
          var  anchor    = document.createElement( 'a' );
          anchor.href    = 'javascript:set_client_name( ' + rows[ i ].id + ', "' + rows[ i ].name + '", "' + rows[ i ].email + '", "' + rows[ i ].phone + '", "' + rows[ i ].mobile + '", "' + rows[ i ].company + '" );';
          anchor.appendChild( text_node );

          var  elem_li   = document.createElement( 'li' );
          elem_li.appendChild( anchor );
          elem_li.style.listStyleType = 'none';
          elem_ul.appendChild( elem_li );
     }
     elem.appendChild( elem_ul );
}

function set_client_name( pId, pName, pEmail, pPhone, pMobile, pCompany ) { 
     beep();
     search_hide( 'client_names' );
     ajax( SERVER_NAME + 'ajax/setclientid?id=' + pId );

     var  names = pName.split( '|' );    
     set_hint(    'first_name', names[ 0 ]   );
     set_hint(   'middle_name', names[ 1 ]   );
     set_hint(     'last_name', names[ 2 ]   );
     set_hint(  'client_email', pEmail       );
     set_hint(  'client_phone', pPhone       );
     set_hint( 'client_mobile', pMobile      );
     set_hint(  'company_name', pCompany     );
//   get_id( 'subject' ).focus();
     set_focus( 'company_name' );
}

//   ajax for Client Email -----------------------------------------------------

function search_client_email( pField, pEvent ) {
}

function set_focus( pName ) {
     var  elem = get_id( pName );
     if(  elem ) {
          elem.focus ();
          elem.select();
     }
}

function set_hint( name, value ) {
     var  elem = get_id( name );
     if(  elem ) {
          elem.value = value;
          if(  value != '' ) {
               var  hint = get_id( name + '_hint' );
               if(  hint )
                    hint.style.display = 'none';
          }
     }
}

function set_product_id( pId ) {
     ajax( SERVER_NAME + 'ajax/setproductid?id=' + pId );
     
     setTimeout( function() {
          if(  opener != null )    opener.location.reload();
          window.close();
     }, 250 );
}

//   ajax for Product Type -----------------------------------------------------

function search_product_type( pField, pEvent ) {
     var  kc = pEvent.keyCode;

     if(  kc >= 32 )
          ajax( SERVER_NAME + 'ajax/producttypes?key=' + pField.value, process_product_type, pField );
     else if(  kc == 13 ) {
               search_hide ( 'product_types' );
               search_focus( 'product_model' );          
          }
}

function process_product_type( pField, pResponse ) {
     var  id   = 'product_type';
     var  rows = eval( '(' + pResponse + ')' );

     if(  rows.length == 0 ) {
          search_hide( id + 's' );
     } else
     if(  rows.length == 1 ) {
          set_value( id, rows[ 0 ].name + ' ' + rows[ 0 ].value );
     } else {
          var  elem = get_id( id + 's' );
          elem.style.visibility = 'visible';
          elem.innerHTML = '';

          var  elem_ul = document.createElement( 'ul' );
          elem_ul.className = 'search_models';
     
          for( var i=0; i< rows.length; i++ ) {
               var  text_name = rows[ i ].name + ' ' + rows[ i ].value;
               var  text_node = document.createTextNode( text_name );
               var  anchor    = document.createElement( 'a' );
               anchor.href    = 'javascript: set_value( "' + id + '", "' + text_name + '" );';
               anchor.appendChild( text_node );

               var  elem_li   = document.createElement( 'li' );
               elem_li.appendChild( anchor );
               elem_li.style.listStyleType = 'none';
               elem_ul.appendChild( elem_li );
          }
          elem.appendChild( elem_ul );
     }
}

//   ajax for Product Model ----------------------------------------------------

function search_product_model( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc >= 32 )
          ajax( SERVER_NAME + 'ajax/productmodels?key=' + pField.value, process_product_model, pField );
     else if(  kc == 13 ) {
               search_hide ( 'product_models' );
               search_focus( 'model_code'     );          
          }
}

function process_product_model( pField, pResponse ) {
     var  id   = 'product_model';
     var  rows = eval( '(' + pResponse + ')' );

     if(  rows.length == 0 ) {
          search_hide( id + 's' );
     } else
     if(  rows.length == 1 ) {
          set_value( id, rows[ 0 ].name );
     } else {
          var  elem = get_id( id + 's' );
          elem.style.visibility = 'visible';
          elem.innerHTML = '';

          var  elem_ul = document.createElement( 'ul' );
          elem_ul.className = 'search_models';
     
          for( var i=0; i< rows.length; i++ ) {
               var  text_name = rows[ i ].name;
               var  text_node = document.createTextNode( text_name );
               var  anchor    = document.createElement( 'a' );
               anchor.href    = 'javascript: set_value( "' + id + '", "' + text_name + '" );';
               anchor.appendChild( text_node );

               var  elem_li   = document.createElement( 'li' );
               elem_li.appendChild( anchor );
               elem_li.style.listStyleType = 'none';
               elem_ul.appendChild( elem_li );
          }
          elem.appendChild( elem_ul );
     }
}

//   ajax for Product Name -----------------------------------------------------

function search_product_name( pField, pEvent ) {
     var  kc = pEvent.keyCode;
     if(  kc == 9 || kc == 13 ) {
          pEvent.returnValue = false;
          pEvent.cancelBuble = true;
//          set_value( 'product_name', pField.value );
          pEvent = false;
          return false;
     } else {
     if(  kc >= 32 )
          ajax( SERVER_NAME + 'ajax/products?key=' + pField.value, process_product_name, pField );
     }
}

function process_product_name( pField, pResponse ) {
     var  rows = eval( '(' + pResponse + ')' );
     var  elem = get_id( 'product_names' );
     elem.style.visibility = 'visible';
     elem.innerHTML = '';

     if(  rows.length == 1 )
          set_product_name( rows[ 0 ].id, rows[ 0 ].name, rows[ 0 ].NCM );
     if(  rows.length <= 1 )
          return;

     var  elem_ul = document.createElement( 'ul' );
     elem_ul.className = 'search_names';
     
     for( var i=0; i< rows.length; i++ ) {
          var  text_name = rows[ i ].id + ' : ' + rows[ i ].name + ' : ' + rows[ i ].NCM;
          var  text_node = document.createTextNode( text_name );
          var  anchor    = document.createElement( 'a' );
          anchor.href    = 'javascript:set_product_name( ' + rows[ i ].id + ', "' + rows[ i ].name + '", "' + rows[ i ].NCM + '" );';
          anchor.appendChild( text_node );

          var  elem_li   = document.createElement( 'li' );
          elem_li.appendChild( anchor );
          elem_li.style.listStyleType = 'none';
          elem_ul.appendChild( elem_li );
     }
     elem.appendChild( elem_ul );
}

//   to process [Selects] functions --------------------------------------------

function change_selected( pField, pId ) {
     pField.value = ( pField.value == 'N' ? 'Y' : 'N' );
     var  myValue = pField.value;

     ajax( SERVER_NAME + 'ajax/changeselected?id=' + pId + '&value=' + myValue );
}

function update_description( event, pField, pId ) {
     if(  event.keyCode != 9 && event.keyCode != 13 )
          return true;

     var  myValue = pField.value;
     var  myId    = pField.id   ;
     var  myCheck = get_id( 'CK-' + myId );
     myCheck.checked = ( myValue == '' ? false : true ); 

     if(  event.keyCode == 9 )
          return true;

     var  texts = document.getElementsByName( 'VN-text' );
     for( var i=0; i<texts.length; i++ ) {
          var  vn_text = texts.item( i );
          if(  vn_text.id == myId ) {
               i++;
               if(  i == texts.length )
                    i = 0;
               texts[ i ].focus();
               texts[ i ].select();
               return true;
          }
     }
}

function change_description( event, pField, pId ) {
     var  myValue = pField.value;
     var  myId    = pField.id   ;
     var  myCheck = get_id( 'CK-' + myId );
     myCheck.checked = ( myValue == '' ? false : true ); 
     
     ajax( SERVER_NAME + 'ajax/changedescription?id=' + pId + '&text=' + myValue );
     return;     
}

//   to process [Texts] functions ----------------------------------------------

function update_text( event, pField, pId ) {
     if(  event.keyCode != 13 )
          return true;

     var  myValue = pField.value;
     var  myId    = pField.id   ;
     
     var  texts = document.getElementsByName( 'VN-text' );
     for( var i=0; i<texts.length; i++ ) {
          var  vn_text = texts.item( i );
          if(  vn_text.id == myId ) {
               i++;
               if(  i == texts.length )
                    i = 0;
               texts[ i ].focus();
               texts[ i ].select();
               return true;
          }
     }
}

function change_text( event, pField, pId ) {
     var  myValue = pField.value;
     ajax( SERVER_NAME + 'ajax/changetext?id=' + pId + '&text=' + myValue );
     return;
}

function addnew_text( event, pField, pParentName, pParentId, pPeriod, pGroupSet ) {
     if(  event.keyCode != 9 && event.keyCode != 13 )
          return true;

     var  myValue = pField.value;
     if(  myValue != '' ) {
          ajax( SERVER_NAME + 'ajax/addnewtext?parent_name=' + pParentName + '&parent_id=' + pParentId + '&period=' + pPeriod + '&group_set=' + pGroupSet + '&text=' + myValue, display_text, pField );
          pField.value = '';
     }
     return event.keyCode;
}

function delete_text( pField, pId ) {
     var  text_div = get_id( 'TEXT' + pId );
     if(  text_div )
          text_div.parentNode.removeChild( text_div );
     ajax( SERVER_NAME + 'ajax/deletetext?id=' + pId );
}

function display_text( pField, pResponse ) {
     var  html = '';
     var  rows = eval( '(' + pResponse + ')' );
     for( var i=0; i< rows.length; i++ ) {
          var  id   = rows[ i ].id;
          var  del  = '<img id="' + id + '" name="' + id + '" src="' + SERVER_NAME + 'del.png"'
                    + ' onClick="delete_text( this, ' + id + ' )"'
                    + ' />';
          var  text = '<input type="text" id="' + id + '" name="VN-text" value="' + rows[ i ].description + '" size="108"'
                    + ' onBlur   ="return change_text( event, this, ' + id + ' )"'
                    + ' onKeyDown="return update_text( event, this, ' + id + ' )"'
                    + ' />';
          html +='<div class=texts id=TEXT' + id + '>'
               + '<div class=button >' + del   + '</div>'
               + '<div class=text   >' + text  + '</div>'
               + '</div>'
               ;
     }
     var  group_div = get_id( 'TEXT' + pField.name );
     group_div.innerHTML = html;
}


var  my_timeout_id = null;

function ajax( pURL, pCallBack, pField ) {
     if(  my_timeout_id )
          clearTimeout( my_timeout_id );

     my_timeout_id = setTimeout( function() {
          var  request = my_request();
          request.onreadystatechange = function() {                   // watch for the state of the document gets updated
               if(  request.readyState == 4
               &&   request.status     == 200 ) {                     // wait until the data is fully loaded
                    if(  pCallBack )
                         pCallBack( pField, request.responseText );   // call the completion callback
                    request = null;                                   // clean up after ourselves to avoid memory leaks
               }
          };
          request.open( 'GET', pURL, true );                          // open the asynchronous POST request
          request.send( null );                                       // establish the connection to the server
     }, 250 );
}

function get_id( pElem ) {
     return document.getElementById( pElem );
}

function get_value( pElem ) {
     var  elem = get_id( pElem );
     if(  elem )
          return elem.value;
     else return '';
}

function search_focus( pName ) {
     var  elem = get_id( pName );
     elem.focus();
     elem.select();
}

function search_hide( pName ) {
     var  elem = get_id( pName );
     elem.style.visibility = 'hidden';
}

function search_show( pName ) {
     var  elem = get_id( pName );
     elem.style.visibility = 'visible';
}

// -------------------------------------
function check_cookies() {
     set_cookie( 'cookie', 'on' );
     if( !is_cookie( 'cookie' ))
          document.write
               ( '<h1>Cookies must be allowed</h1>'
               + '<p>Your browser is currently set to block cookies. Your browser must allow cookies before you can use the system.</p>'
               + '<p>Cookies are small text files stored on your computer that tell the system when you\'re signed in.</p>'
               + '<p>To learn how to allow cookies, see online help in your web browser.</p>'
               );
}

function is_cookie( pName ) {
     var cookies = document.cookie;
     var start = cookies.indexOf( pName + '=' );
     if(  start == -1 )
          return false;
     else return true ;
}

function get_cookie( pName ) {
     var cookies = document.cookie;
     var start = cookies.indexOf( pName + '=' );
     if(  start == -1 )
          return '';
     start = start + pName.length + 1;
     var end = cookie.indexOf( ';', start );
     if(  end == -1 )
          end = cookies.length;
//   return unescape( cookies.substring( start, end ));
     return unescape( cookies.substr( start, end ));
}

function set_cookie( pName, pValue ) {
     var expires = new Date();
     expires.setTime( expires.getTime() + ( 7 * 86400 ));
     document.cookie = pName + '=' + escape( pValue ) + ';expires=' + expires.toGMTString();
}

// -------------------------------------

function set_value( pName, pValue ) {
     beep();

     var  elem = get_id( pName );
     elem.value = pValue;

     search_hide( pName + 's' );
/*     
          if(  pName == 'client_name'  )     search_focus( 'client_email' );
     else if(  pName == 'client_email' )     search_focus( 'company_name' );
     else if(  pName == 'company_name' )     search_focus( 'subject'      );
     else if(  pName == 'product_type' )     search_focus( 'product_model');
     else if(  pName == 'product_model')     search_focus( 'model_code'   );
*/
}

function submit_form( pForm, pValue ) {
//     get_id( 'commit' ).value = pValue;
//     pForm.submit();
//     return true;

     var  input = document.createElement( 'input' );
          input.type  = 'hidden';
          input.name  = 'commit';
          input.value =  pValue ;

     pForm.appendChild( input );
     pForm.submit();
     return true;
}

function submit_preview( pVideoId ) {
     pHref = window.location.protocol + '//' + 'www.innovateads.com/iads/preview.php' + pVideoId;
     window.location.href = pHref;
}

function onChangeSwf( pField ) {
     get_id( 'swf_image' ).src = SERVER_NAME + 'images/swfs/' + pField.options[ pField.selectedIndex ].text + '.png';
}

function onClickGroup( pField, pId, pIndex ) {
     myHtml = pField.innerHTML;
     if(  myHtml.indexOf( 'on.png' ) > 0 )
          myFlag = 'N';
     else myFlag = 'Y';

     if(  myFlag == 'Y' )     
          myHtml = myHtml.replace( 'off.png',  'on.png' ).replace( 'off.png',  'on.png' );
     else myHtml = myHtml.replace(  'on.png', 'off.png' ).replace(  'on.png', 'off.png' );
     pField.innerHTML = myHtml;

     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/setgroup?id=' + pId + '&index=' + pIndex + '&flag=' + myFlag, true );
     request.send( null );
}

function onChangeWidth( pField, pId ) {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/setwidth?id=' + pId + '&width=' + pField.value, false );
     request.send( null );
     window.location.replace( unescape( window.location.pathname ));
}

function onChangeLanguage( pField ) {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/setlanguage?language=' + pField.value, false );
     request.send( null );
     window.location.replace( unescape( window.location.pathname ));
}

function onChangeWidth( pField, pId ) {
     var  request = my_request();
     request.open( 'GET', SERVER_NAME + 'ajax/setwidth?id=' + pId + '&width=' + pField.value, false );
     request.send( null );
     window.location.replace( unescape( window.location.pathname ));
}

function popup_confirm( pMessage, pHref ) {
     myReply = window.confirm( pMessage );
     if(  myReply )
          window.location = pHref;
}

var PostForm = {
     toggle: function( pForm ){
          if(  typeof pForm == 'undefined' ) {
               pForm = 'form_container';
          }
          var container = $( pForm );
          var form = $$( '#' + pForm + ' form' ).first();

          if(  container.hasClassName( 'active' ))
               form.visualEffect( 'blind_up'  , { duration: 0.25, afterFinish: function() { container.removeClassName( 'active' ); }});
          else form.visualEffect( 'blind_down', { duration: 0.25, beforeStart: function() { container.addClassName   ( 'active' ); }});
     }
}

var XPostForm = {
     toggle: function(){
          var container = $( 'form_container' );
          var form = $$( '#form_container form' ).first();

          if(  container.hasClassName( 'active' ))
               form.visualEffect( 'blind_up'  , { duration: 0.25, afterFinish: function() { container.removeClassName( 'active' ); }});
          else form.visualEffect( 'blind_down', { duration: 0.25, beforeStart: function() { container.addClassName   ( 'active' ); }});
     }
}

var  EmailForm = {
     toggle: function(){
          var container = $( 'email_container' );
          var form = $$( '#email_container form' ).first();

//        document.getElementsByName( "description" )[ 0 ].select();

          if(  container.hasClassName( 'active' ))
               form.visualEffect( 'blind_up'  , { duration: 0.25, afterFinish: function() { container.removeClassName( 'active' ); }});
          else form.visualEffect( 'blind_down', { duration: 0.50, beforeStart: function() { container.addClassName   ( 'active' ); }});
     }
}

function SetFocus( pField ) {
//   Form.elements[ pField ].focus();
     var myField = document.getElementsByName( pField );
     myField[ 0 ].focus();
}

function SetSelect( pField ) {
//   Form.elements[ pField ].focus();
//   Form.elements[ pField ].select();
     var myField = document.getElementsByName( pField );
     myField[ 0 ].focus();
     myField[ 0 ].select();
}

function popup_pdf( file_name ) {
     pdf_name = window.location.protocol + '//' + window.location.host + '/images/carona/' + file_name;
     popup = window.open( pdf_name, 'PDF', 'width=1000, height=816, left=300, top=50, resizable, scrollbars=yes' );
     return false;
}

function popup_error() {
     myURL = INDEX + 'popuperror/show';
     winErrorX = window.open( myURL, 'ErrorX', 'width=600, height=210, screenX=400, screenY=300, top=300, left=400, toolbar=no, location=no,      directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winErrorX.opener )
          winErrorX.opener = self;
     winErrorX.focus();
}

function Xpopup_message() {
     myURL = INDEX + 'message/show';
     winMessageX = window.open( myURL, 'MessageX', 'width=600, height=210, screenX=400, screenY=300, top=300, left=400, toolbar=no, location=no,      directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winMessageX.opener )
          winMessageX.opener = self;
     winMessageX.focus();
}

function popup_comment( pId ) {
     myURL = INDEX + 'comments/show?id=' + pId;
     winComment = window.open( myURL, 'Comment', 'width=600, height=300, screenX=400, screenY=300, top=300, left=400, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winComment.opener )
          winComment.opener = self;
     winComment.focus();
}

function popup_images( pParentName, pParentId ) {
     myURL = INDEX + 'images/index?parent_name=' + pParentName + '&parent_id=' + pParentId;
     winImage = window.open( myURL, 'image', 'width=400, height=600, screenX=680, screenY=100, top=100, left=680, toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winImage.opener )
          winImage.opener = self;
     winImage.focus();
}

function popup_thumbs() {
     myURL = INDEX + 'thumbs/index';
     winThumb = window.open( myURL, 'thumb', 'width=550, height=380, screenX=000, screenY=000, top=000, left=000, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winThumb.opener )
          winThumb.opener = self;
     winThumb.focus();
}

function popup_note( pId ) {
     myURL = INDEX + 'notes/show?id=' + pId;
     winNote = window.open( myURL, 'note', 'width=600, height=210, screenX=400, screenY=300, top=300, left=400, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winNote.opener )
          winNote.opener = self;
     winNote.focus();
}

function popup_instructions( pAction, pWidth, pHeight ) {
     if(  typeof( pWidth  ) == 'undefined' )      pWidth  = 300;
     if(  typeof( pHeight ) == 'undefined' )      pHeight = 300;
     myURL = INDEX + 'instructions/' + pAction;
     winInstruct = window.open( myURL, 'Instruct', 'width=' + pWidth + ', height=' + pHeight + ', screenX=100, screenY=100, top=100, left=100, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winInstruct.opener )
          winInstruct = self;
     winInstruct.focus();
}

function popup_new_item( pOrderId, pItemId ) {
     if(  typeof( pItemId ) == 'undefined' )
          myURL = INDEX + 'items/insert?order_id=' + pOrderId;
     else myURL = INDEX + 'items/update?order_id=' + pOrderId + '&id=' + pItemId;
     winItem = window.open( myURL, 'item', 'width=700, height=560, screenX=400, screenY=100, top=100, left=400, toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winItem.opener )
          winItem.opener = self;
     winItem.focus();
}

function popup_new_nfeitem( pNFe_id, pNFeItem_id ) {
     if(  typeof( pNFeItem_id ) == 'undefined' )
          myURL = INDEX + 'nfeitems/insert?NFe_id=' + pNFe_id;
     else myURL = INDEX + 'nfeitems/update?NFe_id=' + pNFe_id + '&id=' + pNFeItem_id;
     winItem = window.open( myURL, 'nfeitem', 'width=700, height=600, screenX=400, screenY=100, top=100, left=400, toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winItem.opener )
          winItem.opener = self;
     winItem.focus();
}

function popup_member( pId ) {
     myURL = INDEX + 'member/addnew?group_id=' + pId;
     winUser = window.open( myURL, 'User', 'width=700, height=540, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUser.opener )
          winUser = self;
     winUser.focus();
}

function check_form( pForm, pField, pValue ) {
//alert( 'check_form - value: ' + pValue );
//#     get_id( 'commit' ).value = pValue;
     pField.value = pValue;

     if(  pValue == 'LogIn' ) {
//#          myEmail    = pForm.email.value;
          myPassword = pForm.password.value;
          myUserTime = pForm.user_time.value;

//        remove user_time from DOM
          var user_time = pForm.user_time;
          var user_parent = user_time.parentNode;
          user_parent.removeChild( user_time );

          pForm.password.value = MD5( myUserTime + MD5( myPassword ));
     }
     
     var  input = document.createElement( 'input' );
          input.type  = 'hidden';
          input.name  = 'commit';
          input.value =  pValue ;

//     document.Form.appendChild( input );
//     document.Form.submit();
     pForm.appendChild( input );
     pForm.submit();
}

/**
*
*  MD5 (Message-Digest Algorithm)
*  http://www.webtoolkit.info/
*
**/

function MD5( string ) {
    function RotateLeft(lValue, iShiftBits) {
        return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
    }

    function AddUnsigned(lX,lY) {
        var lX4,lY4,lX8,lY8,lResult;
        lX8 = (lX & 0x80000000);
        lY8 = (lY & 0x80000000);
        lX4 = (lX & 0x40000000);
        lY4 = (lY & 0x40000000);
        lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
        if (lX4 & lY4) {
            return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
        }
        if (lX4 | lY4) {
            if (lResult & 0x40000000) {
                return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
            } else {
                return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
            }
        } else {
            return (lResult ^ lX8 ^ lY8);
        }
    }

    function F(x,y,z) { return (x & y) | ((~x) & z); }
    function G(x,y,z) { return (x & z) | (y & (~z)); }
    function H(x,y,z) { return (x ^ y ^ z); }
    function I(x,y,z) { return (y ^ (x | (~z))); }

    function FF(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function GG(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function HH(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function II(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function ConvertToWordArray(string) {
        var lWordCount;
        var lMessageLength = string.length;
        var lNumberOfWords_temp1=lMessageLength + 8;
        var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
        var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
        var lWordArray=Array(lNumberOfWords-1);
        var lBytePosition = 0;
        var lByteCount = 0;
        while ( lByteCount < lMessageLength ) {
            lWordCount = (lByteCount-(lByteCount % 4))/4;
            lBytePosition = (lByteCount % 4)*8;
            lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));
            lByteCount++;
        }
        lWordCount = (lByteCount-(lByteCount % 4))/4;
        lBytePosition = (lByteCount % 4)*8;
        lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
        lWordArray[lNumberOfWords-2] = lMessageLength<<3;
        lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
        return lWordArray;
    };

    function WordToHex(lValue) {
        var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
        for (lCount = 0;lCount<=3;lCount++) {
            lByte = (lValue>>>(lCount*8)) & 255;
            WordToHexValue_temp = "0" + lByte.toString(16);
            WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
        }
        return WordToHexValue;
    };

    function Utf8Encode(string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    };

    var x=Array();
    var k,AA,BB,CC,DD,a,b,c,d;
    var S11=7, S12=12, S13=17, S14=22;
    var S21=5, S22=9 , S23=14, S24=20;
    var S31=4, S32=11, S33=16, S34=23;
    var S41=6, S42=10, S43=15, S44=21;

    string = Utf8Encode(string);

    x = ConvertToWordArray(string);

    a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;

    for (k=0;k<x.length;k+=16) {
        AA=a; BB=b; CC=c; DD=d;
        a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
        d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
        c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
        b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
        a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
        d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
        c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
        b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
        a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
        d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
        c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
        b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
        a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
        d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
        c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
        b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
        a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
        d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
        c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
        b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
        a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
        d=GG(d,a,b,c,x[k+10],S22,0x2441453);
        c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
        b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
        a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
        d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
        c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
        b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
        a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
        d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
        c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
        b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
        a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
        d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
        c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
        b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
        a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
        d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
        c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
        b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
        a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
        d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
        c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
        b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
        a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
        d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
        c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
        b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
        a=II(a,b,c,d,x[k+0], S41,0xF4292244);
        d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
        c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
        b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
        a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
        d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
        c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
        b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
        a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
        d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
        c=II(c,d,a,b,x[k+6], S43,0xA3014314);
        b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
        a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
        d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
        c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
        b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
        a=AddUnsigned(a,AA);
        b=AddUnsigned(b,BB);
        c=AddUnsigned(c,CC);
        d=AddUnsigned(d,DD);
    }

    var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);

    return temp.toLowerCase();
}

function popup_adjust( pId ) {
     myURL = INDEX + 'paymentsca/adjust?group_id=' + pId;
     winAdjust = window.open( myURL, 'Adjust', 'width=730, height=400, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winAdjust.opener )
          winAdjust = self;
     winAdjust.focus();
}

// -----------------------------------------------------------------------------

function onChangeReport( pField ) {
     myDateType = get_value( 'date_type' );
     mySortBy   = get_value( 'sort_by'   );
     myVideoType= get_value( 'video_type');
     if(  myDateType == 'SR' )
          return;
//   pHref = window.location.protocol + '//' + window.location.host + '/reports?dateType=' + myDateType  + '&sortBy=' + mySortBy;
     pHref = '/reports/index?date_type=' + myDateType + '&sort_by=' + mySortBy + '&video_type=' + myVideoType;
     window.location.href = pHref;
}

function onChangeSchool( pField ) {
     var  myField = get_id( 'birth_required' );
//   myField.firstChild.nodeValue = (( pField.value == 'Adult' || pField.value == 'CA' || pField.value == 'CSP' ) ? ' ' : '(required)' );
     myField.firstChild.nodeValue = (( pField.value == 'Adult' ) ? ' ' : '(required)' );
}

function popup_window( pName, pUrl, pWidth, pHeight, pTop, pLeft, pToolBar ) {
     if(  typeof( pToolBar ) == 'undefined' )     pToolBar = 'no';
     newWindow = window.open( pUrl, pName, 'width=' + pWidth + ', height=' + pHeight + ', top=' + pTop + ', left=' + pLeft + ', screenY=' + pTop + ', screenX=' + pLeft + ', toolbar=' + pToolBar + ', location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !newWindow.opener )
          newWindow.opener = self;
     newWindow.focus();
}

function Xpopup_users_orders( pId ) {
     myURL = INDEX + 'users/orders?id=' + pId;
     winUserOrders = window.open( myURL, 'UserOrders', 'width=650, height=600, screenX=100, screenY=100, top=100, left=100, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUserOrders.opener )
          winUserOrders.opener = self;
     winUserOrders.focus();
}

//function popup_users_orders ( pId )   { popup_window( 'UsersOrders', '/orders/index?user=' + pId, 650, 600, 100, 100 ); }

function popup_user_upload    ( pId )   { popup_window( 'User', 'upload?id=' + pId, 700, 400, 100, 100 ); }

function popup_users_orders   ( pId )   { window.location = INDEX +   'orderslx/index?user_id=' + pId; }
function popup_users_payments ( pId )   { window.location = INDEX + 'paymentslx/index?user_id=' + pId; }
function popup_users_packages ( pId )   { window.location = INDEX + 'packageslx/index?user_id=' + pId; }

function popup_orders_history ( pId )   { popup_window( 'OrdersHistory' ,  'history?id=' + pId, 700, 380, 100, 100 ); }
function popup_orders_shipment( pId )   { popup_window( 'OrdersShipment', 'shipment?id=' + pId, 700, 380, 100, 100 ); }
function popup_orders_address ( pId )   { popup_window( 'OrdersAddress' ,  'address?id=' + pId, 700, 380, 100, 100 ); }

function popup_upload         ( pId )   { popup_window( 'Items', 'upload?id=' + pId, 700, 400, 100, 100 ); }

function popup_new_file       ( pId )   { popup_window( 'Files', '/itemfiles/index?id=' + pId, 700, 400, 100, 100 ); }



function popup_products_photo( pId ) {
     myURL = INDEX + 'productslx/photo?id=' + pId;
     winProdPhoto = window.open( myURL, 'ProdPhoto', 'width=650, height=600, screenX=100, screenY=100, top=100, left=100, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProdPhoto.opener )
          winProdPhoto.opener = self;
     winProdPhoto.focus();
}

function popup_products_body( pId ) {
     myURL = INDEX + 'productslx/body?id=' + pId;
     winProdBody = window.open( myURL, 'ProdBody', 'width=600, height=450, screenX=300, screenY=200, top=200, left=300, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProdBody.opener )
          winProdBody.opener = self;
     winProdBody.focus();
}

function popup_products_script( pId ) {
     myURL = INDEX + 'productslx/script?id=' + pId;
     winProdScript = window.open( myURL, 'ProdScript', 'width=600, height=450, screenX=500, screenY=300, top=300, left=500, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProdScript.opener )
          winProdScript.opener = self;
     winProdScript.focus();
}

function popup_products_upload( pId ) {
     myURL = INDEX + 'productslx/upload?id=' + pId;
     winProdUpload = window.open( myURL, 'ProdUpload', 'width=650, height=300, screenX=700, screenY=400, top=400, left=700, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProdUpload.opener )
          winProdUpload.opener = self;
     winProdUpload.focus();
}

function popup_products_search() {
     myURL = INDEX + 'productstt/search';
     winProdSearch = window.open( myURL, 'ProdSearch', 'width=800, height=500, screenX=800, screenY=500, top=100, left=100, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProdSearch.opener )
          winProdSearch.opener = self;
     winProdSearch.focus();
}

function popup_userprojs( pId, pVersion ) {
     myURL = INDEX + 'userprojs/invite';
     winUserProj = window.open( myURL, 'UserProj', 'width=600, height=400, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUserProj.opener )
          winUserProj.opener = self;
     winUserProj.focus();
}

function popup_files( pId, pVersion ) {
     if(  typeof( pId ) == 'undefined' )
          myURL = INDEX + 'files/insert';
     else myURL = INDEX + 'files/upload?id=' + pId + '&version=' + pVersion;
     winFiles = window.open( myURL, 'Files', 'width=600, height=400, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winFiles.opener )
          winFiles.opener = self;
     winFiles.focus();
}

function popup_uploads( pType ) {
     myURL = INDEX + 'uploads/index?type=' + pType;
     winUploads = window.open( myURL, 'Uploads', 'width=500, height=500, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUploads.opener )
          winUploads.opener = self;
     winUploads.focus();
}

function popup_church( pId ) {
     myURL = INDEX + 'church/show?id=' + pId;
     winChurch = window.open( myURL, 'Church', 'width=640, height=520, screenX=200, screenY=100, top=100, left=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winChurch.opener )
          winChurch.opener = self;
     winChurch.focus();
}

function popup_house( pId ) {
     myURL = INDEX + 'house/show?id=' + pId;
     winHouse = window.open( myURL, 'House', 'width=640, height=620, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winHouse.opener )
          winHouse.opener = self;
     winHouse.focus();
}

function popup_address( pName ) {
     myURL = INDEX + '' + pName + 'addr/show';
     winAddress = window.open( myURL, 'Address', 'width=700, height=470, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winAddress.opener )
          winAddress.opener = self;
     winAddress.focus();
}

function popup_revenues( pId ) {
     myURL = INDEX + 'revenues/index';
     winRevenues = window.open( myURL, 'Revenues', 'width=330, height=400, screenX=250, screenY=100, top=100, left=250, tolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winRevenues.opener )
          winRevenues.opener = self;
     winRevenues.focus();
}

function popup_company( pName, pId ) {
     myURL = INDEX + 'companies/select?name=' + pName + '&id=' + pId;
     winCompany = window.open( myURL, 'Company', 'width=640, height=490, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winCompany.opener )
          winCompany.opener = self;
     winCompany.focus();
}

function popup_tags( pName, pId ) {
     myURL = INDEX + 'tags/index?name=' + pName + '&id=' + pId;
     winTags = window.open( myURL, 'Tags', 'width=350, height=500, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winTags.opener )
          winTags.opener = self;
     winTags.focus();
}

function popup_navbar( pId ) {
     myURL = INDEX + 'navbar/upload?id=' + pId;
     winNavbar = window.open( myURL, 'Navbar', 'width=700, height=290, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winNavbar.opener )
          winNavbar.opener = self;
     winNavbar.focus();
}

function popup_swfupload( pId ) {
     myURL = INDEX + 'swfs/upload?id=' + pId;
     winUpload = window.open( myURL, 'Upload', 'width=700, height=290, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUpload.opener )
          winUpload.opener = self;
     winUpload.focus();
}

function popup_flvupload( pId ) {
     myURL = INDEX + 'flvs/upload?id=' + pId;
     winUpload = window.open( myURL, 'Upload', 'width=700, height=290, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winUpload.opener )
          winUpload.opener = self;
     winUpload.focus();
}

function popup_swf( pId ) {
     myURL = INDEX + 'swf/show?id=' + pId;
     winSwf = window.open( myURL, 'Swf', 'width=380, height=620, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winSwf.opener )
          winSwf.opener = self;
     winSwf.focus();
}

function popup_flv( pId ) {
     myURL = INDEX + 'flv/show?id=' + pId;
     winFlv = window.open( myURL, 'Flv', 'width=640, height=550, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winFlv.opener )
          winFlv.opener = self;
     winFlv.focus();
}

function popup_options( pId ) {
     myURL = INDEX + 'options/show';
     winOptions = window.open( myURL, 'Options', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winOptions.opener )
          winOptions.opener = self;
     winOptions.focus();
}

function popup_keywords( pId ) {
     myURL = INDEX + 'keywords/show';
     winKeywords = window.open( myURL, 'Keywords', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winKeywords.opener )
          winKeywords.opener = self;
     winKeywords.focus();
}

function popup_balances( pId ) {
     myURL = INDEX + 'balances/show';
     winBalances = window.open( myURL, 'Balances', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winBalances.opener )
          winBalances.opener = self;
     winBalances.focus();
}

function popup_shareholders( pId ) {
     myURL = INDEX + 'shareholders/show';
     winShareholders = window.open( myURL, 'Shareholders', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winShareholders.opener )
          winShareholders.opener = self;
     winShareholders.focus();
}

function popup_fellowships( pId ) {
     myURL = INDEX + 'fellowships/show';
     winFellowships = window.open( myURL, 'Fellowships', 'width=640, height=500, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winFellowships.opener )
          winFellowships.opener = self;
     winFellowships.focus();
}

function popup_vendors( pId ) {
     myURL = INDEX + 'vendors/show';
     winVendors = window.open( myURL, 'Vendors', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winVendors.opener )
          winVendors.opener = self;
     winVendors.focus();
}

function popup_products( pId ) {
     myURL = INDEX + 'products/show';
     winProducts = window.open( myURL, 'Products', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winProducts.opener )
          winProducts.opener = self;
     winProducts.focus();
}

function popup_fellowships( pId )
{
     myURL = INDEX + 'fellowships/show';
     winFellowships = window.open( myURL, 'Fellowships', 'width=640, height=500, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winFellowships.opener )
          winFellowships.opener = self;
     winFellowships.focus();
}

function popup_vendors( pId )
{
     myURL = INDEX + 'vendors/show';
     winVendors = window.open( myURL, 'Vendors', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winVendors.opener )
          winVendors.opener = self;
     winVendors.focus();
}

function popup_customers( pId )
{
     myURL = INDEX + 'customers/show';
     winCustomers = window.open( myURL, 'Customers', 'width=640, height=400, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winCustomers.opener )
          winCustomers.opener = self;
     winCustomers.focus();
}

function popup_emails( pId ) {
     if(  pId )
          myURL = INDEX + 'emails/index?id=' + pId;
     else myURL = INDEX + 'emails/index';
     winEmails = window.open( myURL, 'Emails', 'width=730, height=720, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winEmails.opener )
          winEmails.opener = self;
     winEmails.focus();
}

function popup_email_flv( pId ) {
     myURL = INDEX + 'emailflv/show?id=' + pId;
     winEmailFlv = window.open( myURL, 'EmailFlv', 'width=640, height=550, screenX=250, screenY=100, top=100, left=250, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winEmailFlv.opener )
          winEmailFlv.opener = self;
     winEmailFlv.focus();
}

function popup_live( pUrl ) {
//alert( 'url:' + pUrl );
     winLive = window.open( pUrl, 'Live', 'width=880, height=960, screenX=050, screenY=000, top=000, left=050, toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes' );
     if( !winLive.opener )
          winLive.opener = self;
     winLive.focus();
}

function popup_play_video( arg ) {
//alert( 'flv: ' + arg.flv + ', width: ' + arg.width + ', height: ' + arg.height + ', position: ' + arg.position );
     arg.swf    = arg.swf    || 'OverAll';
     arg.width  = arg.width  || 320;
     arg.height = arg.height || 240;

     if(  typeof( winPlayVideo ) != 'undefined' )
          winPlayVideo.close();
     winPlayVideo = window.open( '', 'PlayVideo', 'width=' + arg.width + ', height=' + arg.height + ',screenX=050, screenY=000, top=000, left=050, toolbar=yes, location=no, directories=no, status=no, menubar=yes, scrollbars=no, resizable=no, copyhistory=no' );
     if( !winPlayVideo.opener )
          winPlayVideo.opener = self;
     winPlayVideo.focus();

     winPlayVideo.document.write( "<style>body { margin:0; }</style>" );
     winPlayVideo.document.write( "<div id='iad" + arg.flv + "' style='width:1px; height:1px; z-index:1;'>" );
     winPlayVideo.document.write( "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'" );
     winPlayVideo.document.write( "        codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'" );
     winPlayVideo.document.write( "        width=" + arg.width );
     winPlayVideo.document.write( "        height=" + arg.height + ">" );
     winPlayVideo.document.write( "<param name='allowScriptAccess' value='always' />" );
     winPlayVideo.document.write( "<param name='movie' value='" + arg.website + "swf/" + arg.swf + ".swf?flv=" + arg.flv + "' />" );
     winPlayVideo.document.write( "<param name='quality' value='high' />" );
     winPlayVideo.document.write( "<param name='wmode' value='transparent' />" );
     winPlayVideo.document.write( "<param name='loop' value='false' />" );
     winPlayVideo.document.write( "<param name='bgcolor' value='#666666' />" );
     winPlayVideo.document.write( "<embed src='" + arg.website + "swf/" + arg.swf + ".swf?flv=" + arg.flv + "'" );
     winPlayVideo.document.write( "       quality='high'" );
     winPlayVideo.document.write( "       wmode='transparent'" );
     winPlayVideo.document.write( "       loop='false'" );
     winPlayVideo.document.write( "       bgcolor='#666666'" );
     winPlayVideo.document.write( "       width=" + arg.width );
     winPlayVideo.document.write( "       height=" + arg.height );
     winPlayVideo.document.write( "       align='bottom'" );
     winPlayVideo.document.write( "       allowscriptaccess='always'" );
     winPlayVideo.document.write( "       type='application/x-shockwave-flash'" );
     winPlayVideo.document.write( "       pluginspage='http://www.macromedia.com/go/getflashplayer' />" );
     winPlayVideo.document.write( "</object>" );
     winPlayVideo.document.write( "</div>" );
}

function popup_paymentlx( pAction ) {
     myURL = INDEX + 'paymentlx/' + pAction;
     winPayment = window.open( myURL, 'Payment', 'width=620, height=700, screenX=050, screenY=050, top=050, left=050, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no' );
     if( !winPayment.opener )
          winPayment = self;
     winPayment.focus();
}

function popup_message( pName, pWidth, pHeight ) {
     myURL = INDEX + 'index/message?name=' + pName;
     winMessage = window.open( myURL, 'Message', 'width=' + pWidth + ', height=' + pHeight + ', screenX=050, screenY=050, top=050, left=050, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no' );
     if( !winMessage.opener )
          winMessage = self;
     winMessage.focus();
}

//   This is needed because of bug in Firefox 3.5.6
function reset_selected_index( pName ) {
     select  = get_id( pName );
     options = select.getElementsByTagName( 'option' );
     for( var i=0; i< options.length; i++ ) {
          selected = options[ i ].getAttribute( 'selected' );
          if(  selected == 'selected' ) {
               select.selectedIndex = i;
               return;
          }
     }
     select.selectedIndex = 0;
}

function set_selected_index( pName, pValue ) {
     select  = get_id( pName );
     options = select.getElementsByTagName( 'option' );
     for( var i=0; i< options.length; i++ ) {
          if(  options[ i ].value == pValue ) {
               select.selectedIndex = i;
               return;
          }
     }
}

function select_year( pName ) {
     document.write( '<select id="' + pName + '_1" name="' + pName + '_1">' );
     document.write( '<option value="0000">0000</option>' );
     document.write( '<option value="2006">2006</option>' );
     document.write( '<option value="2007">2007</option>' );
     document.write( '<option value="2008">2008</option>' );
     document.write( '<option value="2009">2009</option>' );
     document.write( '<option value="2010">2010</option>' );
     document.write( '<option value="2011">2011</option>' );
     document.write( '<option value="2012">2012</option>' );
     document.write( '<option value="2013">2013</option>' );
     document.write( '<option value="2014">2014</option>' );
     document.write( '<option value="2015">2015</option>' );
     document.write( '</select>' );
}    
     
function select_month( pName ) {
     document.write( '<select id="' + pName + '_2" name="' + pName + '_2">' );
     document.write( '<option value="00">Undefined</option>' );
     document.write( '<option value="01">January  </option>' );
     document.write( '<option value="02">February </option>' );
     document.write( '<option value="03">March    </option>' );
     document.write( '<option value="04">April    </option>' );
     document.write( '<option value="05">May      </option>' );
     document.write( '<option value="06">June     </option>' );
     document.write( '<option value="07">July     </option>' );
     document.write( '<option value="08">August   </option>' );
     document.write( '<option value="09">September</option>' );
     document.write( '<option value="10">October  </option>' );
     document.write( '<option value="11">November </option>' );
     document.write( '<option value="12">December </option>' );
     document.write( '</select>' );
}

function select_mes( pName ) {
     document.write( '<select id="' + pName + '_2" name="' + pName + '_2">' );
     document.write( '<option value="00">Indefinido</option>' );
     document.write( '<option value="01">Janeiro  </option>' );
     document.write( '<option value="02">Fevereiro</option>' );
     document.write( '<option value="03">Marco    </option>' );
     document.write( '<option value="04">Abril    </option>' );
     document.write( '<option value="05">Maio     </option>' );
     document.write( '<option value="06">Junho    </option>' );
     document.write( '<option value="07">Julho    </option>' );
     document.write( '<option value="08">Agosto   </option>' );
     document.write( '<option value="09">Setembro </option>' );
     document.write( '<option value="10">Outubro  </option>' );
     document.write( '<option value="11">Novembro </option>' );
     document.write( '<option value="12">Dezembro </option>' );
     document.write( '</select>' );
}

function select_day( pName, pValue ) {
     document.write( '<select id="' + pName + '_3" name="' + pName + '_3">' );
     document.write( '<option value="00">00</option>' );
     document.write( '<option value="01">01</option>' );
     document.write( '<option value="02">02</option>' );
     document.write( '<option value="03">03</option>' );
     document.write( '<option value="04">04</option>' );
     document.write( '<option value="05">05</option>' );
     document.write( '<option value="06">06</option>' );
     document.write( '<option value="07">07</option>' );
     document.write( '<option value="08">08</option>' );
     document.write( '<option value="09">09</option>' );
     document.write( '<option value="10">10</option>' );
     document.write( '<option value="11">11</option>' );
     document.write( '<option value="12">12</option>' );
     document.write( '<option value="13">13</option>' );
     document.write( '<option value="14">14</option>' );
     document.write( '<option value="15">15</option>' );
     document.write( '<option value="16">16</option>' );
     document.write( '<option value="17">17</option>' );
     document.write( '<option value="18">18</option>' );
     document.write( '<option value="19">19</option>' );
     document.write( '<option value="20">20</option>' );
     document.write( '<option value="21">21</option>' );
     document.write( '<option value="22">22</option>' );
     document.write( '<option value="23">23</option>' );
     document.write( '<option value="24">24</option>' );
     document.write( '<option value="25">25</option>' );
     document.write( '<option value="26">26</option>' );
     document.write( '<option value="27">27</option>' );
     document.write( '<option value="28">28</option>' );
     document.write( '<option value="29">29</option>' );
     document.write( '<option value="30">30</option>' );
     document.write( '<option value="31">31</option>' );
     document.write( '</select>' );
}

function select_date( pName, pValue ) {
     select_month( pName );
     select_day  ( pName );
     select_year ( pName );
     if(  pValue == null )    pValue = '0000-00-00';
     var  dates  = pValue.split( '-' );
     set_selected_index( pName + '_1', dates[ 0 ]);    //   year
     set_selected_index( pName + '_2', dates[ 1 ]);    //   month
     set_selected_index( pName + '_3', dates[ 2 ]);    //   day
}

function select_dmy( pName, pValue ) {
     select_day  ( pName );
     select_mes  ( pName );
     select_year ( pName );
     if(  pValue == null )    pValue = '0000-00-00';
     var  dates  = pValue.split( '-' );
     set_selected_index( pName + '_1', dates[ 0 ]);    //   year
     set_selected_index( pName + '_2', dates[ 1 ]);    //   month
     set_selected_index( pName + '_3', dates[ 2 ]);    //   day
}
