var _lovIdFieldID;
var _lovDnFieldID;
var _lovFormName;
var _lovCompanyPopupVersion = 1;

function openLOVCompanyPopup( formname, idfield, dnfield )
{
    _lovCompanyPopupVersion = 1;
    _lovFormName  = formname;
    _lovIdFieldID = idfield;
    _lovDnFieldID = dnfield;
    var popupURL = "ozSlsLOVCompany.jsp?lovsrc=GEN_CF&searchParam="+document.forms[formname].elements[dnfield].value;
    var lovWin = window.open(popupURL, 'LOVCompany', 'left=100,top=60,width=700,height=540,scrollbars=yes');
    lovWin.opener = self;
    lovWin.focus();
}
function acceptLOVCompanyPopup( id, dn )
{
  if ( _lovCompanyPopupVersion == 2 )
  {
    $( _lovIdFieldID ).value = id;
    $( _lovDnFieldID ).value = URLDecode(dn);
  }
  else
  {
    document.forms[_lovFormName].elements[_lovIdFieldID].value = id;
    document.forms[_lovFormName].elements[_lovDnFieldID].value = URLDecode(dn);
  }
}
function openLOVCompanyPopupV2( idIdfield, idDnfield )
{
  _lovCompanyPopupVersion = 2;
  _lovIdFieldID = idIdfield;
  _lovDnFieldID = idDnfield;
  var popupURL = "ozSlsLOVCompany.jsp?lovsrc=GEN_CF&searchParam="+$(idDnfield).value;
  var lovWin = window.open(popupURL, 'LOVCompany', 'left=100,top=60,width=700,height=540,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}



function openLOVPersonPopup( formname, idfield, dnfield )
{
    _lovCompanyPopupVersion = 1;
    _lovFormName  = formname;
    _lovIdFieldID = idfield;
    _lovDnFieldID = dnfield
    var popupURL = "ozSlsLOVContact.jsp?lovsrc=GEN_CF&searchParam="+document.forms[formname].elements[dnfield].value;
    var lovWin = window.open(popupURL, 'LOVContact', 'left=100,top=60,width=700,height=540,scrollbars=yes');
    lovWin.opener = self;
    lovWin.focus();
}
function acceptLOVPersonPopup( id, dn, fn, ln, email, phone )
{
  if ( _lovCompanyPopupVersion == 2 )
  {
    $( _lovIdFieldID ).value = id;
    $( _lovDnFieldID ).value = URLDecode(dn);
  }
  else
  {
    document.forms[_lovFormName].elements[_lovIdFieldID].value = id;
    document.forms[_lovFormName].elements[_lovDnFieldID].value = URLDecode(dn);
  }
}
function openLOVContactPopupV2( idIdfield, idDnfield )
{
  _lovCompanyPopupVersion = 2;
  _lovIdFieldID = idIdfield;
  _lovDnFieldID = idDnfield;
  var popupURL = "ozSlsLOVContact.jsp?lovsrc=GEN_CF&searchParam="+$(idDnfield).value;
  var lovWin = window.open(popupURL, 'LOVContact', 'left=100,top=60,width=700,height=540,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}


var _lovIdPrdcatId;
var _lovIdPrdId;
var _lovidDisp;
var _lovidPrice;
var _lovidQty;
var _lovidAmt;
function openLOVProductPopupV2( idPrdcatId, idPrdId, idDisp, idPrice, idQty, idAmt )
{
  _lovIdPrdcatId = idPrdcatId;
  _lovIdPrdId    = idPrdId;
  _lovidDisp     = idDisp;
  _lovidPrice    = idPrice;
  _lovidQty      = idQty;
  _lovidAmt      = idAmt;
  var lovURL = 'ozLOVProductV2.jsp?id='+$(idPrdcatId).value;
  var lovWin = window.open( lovURL, 'LOVProduct', 'left=100,top=60,width=700,height=500,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}

function acceptLOVProductV2( cid, pid, dn, itemName, itemDesc, taxable, price, cost )
{
  $( _lovIdPrdcatId ).value = cid;
  $( _lovIdPrdId ).value = pid;
  $( _lovidDisp ).value = URLDecode(dn);

  if ( _lovidPrice != '' ) $( _lovidPrice ).value = price;
  if ( _lovidQty != '' )
  {
    var qty = 1.0 * $(_lovidQty).value;
    if ( qty == 1.0 || qty == 0.0 )
    {
      $(_lovidQty).value = '1';
      $(_lovidAmt).value = price;
    }
    else
    {
      $(_lovidAmt).value = qty * price;;
    }
  }
}

function calculateAmountV2( idQty, idAmt, idPrice )
{
    var qty = 1.0 * $(idQty).value;
    $(idAmt).value = limit2dec ( qty * $(idPrice).value );
}



function openLOVUserPopup( formname, idfield, dnfield )
{
  _lovFormName  = formname;
  _lovIdFieldID = idfield;
  _lovDnFieldID = dnfield
  var popupURL = "ozLOVUser.jsp?lovsrc=GEN_CF";
  var lovWin = window.open(popupURL, 'LOVUser', 'left=100,top=60,width=700,height=540,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}
function acceptLOVUserPopup( id, dn, fn, ln, email, phone )
{
  document.forms[_lovFormName].elements[_lovIdFieldID].value = id;
  document.forms[_lovFormName].elements[_lovDnFieldID].value = URLDecode(dn);
}


function openLOVCasePopup( formname, idfield, dnfield )
{
  _lovFormName  = formname;
  _lovIdFieldID = idfield;
  _lovDnFieldID = dnfield
  var popupURL = "ozSptLOVCase.jsp?lovsrc=GEN_CF";
  var lovWin = window.open(popupURL, 'LOVCase', 'left=100,top=60,width=700,height=540,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}
function acceptLOVCasePopup( id, dn )
{
  document.forms[_lovFormName].elements[_lovIdFieldID].value = id;
  document.forms[_lovFormName].elements[_lovDnFieldID].value = URLDecode(dn);
}



// the following function is for the Search & View definition, Date field
function openLOVDatePopup2( formname, qualfield, valfield )
{
  _lovFormName  = formname;
  _lovIdFieldID = valfield;

  // qualfield is a select
  var qualValue = document.forms[formname].elements[qualfield].options[ document.forms[formname].elements[qualfield].selectedIndex ].value;
  var popupURL = "ozLOVDateRange.jsp?lovsrc=GEN_CF&mode=";
  if ( qualValue == 'BETWEEN' ) popupURL = popupURL + '2';
  else                          popupURL = popupURL + '1';
  popupURL = popupURL + '&val=' + document.forms[formname].elements[valfield].value;
  var lovWin = window.open(popupURL, 'LOVDateRange', 'left=100,top=60,width=800,height=560,scrollbars=yes');
  lovWin.opener = self;
  lovWin.focus();
}
function acceptLOVDatePopup2( val1, val2 )
{
  if ( val2 == '' )
    document.forms[_lovFormName].elements[_lovIdFieldID].value = val1;
  else
    document.forms[_lovFormName].elements[_lovIdFieldID].value = val1 + ',' + val2;
}


var __popupContent = '';
function blockAllAndPopup( popupContent )
{
  if ( popupContent == 'OPP_NEW' )
  {
    blockAllAndPopupOppNew( '', -1, -1, -1 );
    return;
  }
  else if ( popupContent == 'PSN_NEW' )
  {
    blockAllAndPopupPsnNew( '', -1 );
    return;
  }
  else if ( popupContent == 'ORG_NEW' )
  {
    blockAllAndPopupOrgNew( '', -1 );
    return;
  }
  else if ( popupContent == 'MLD_NEW' )
  {
    blockAllAndPopupMldNew( '' );
    return;
  }
}
function blockAllAndPopupOrgNew( popupContent, custid )
{
  __popupContent = 'ORG_NEW';
  thickboxOpenUrl( 'Quick Create Organization', 'xhpTBOrgNew.jsp'+genappend_submiturl+'&width=980&height=480&custid='+custid );
  setTimeout( 'blockAllAndPopupAutoComplete()', 700 );
}
function blockAllAndPopupPsnNew( popupContent, custid )
{
  __popupContent = 'PSN_NEW';
  thickboxOpenUrl( 'Quick Create Contact',      'xhpTBPsnNew.jsp'+genappend_submiturl+'&width=980&height=480&custid='+custid );
  setTimeout( 'blockAllAndPopupAutoComplete()', 700 );
}
function blockAllAndPopupOppNew( popupContent, custid, cnctid, productId )
{
  __popupContent = 'OPP_NEW';
  thickboxOpenUrl( 'Quick Create Opportunity',  'xhpTBOppNew.jsp'+genappend_submiturl+'&width=980&height=480&custid='+custid+'&cnctid='+cnctid+'&productId='+productId );
  setTimeout( 'blockAllAndPopupAutoComplete()', 700 );
}
function blockAllAndPopupMldNew( popupContent )
{
  __popupContent = 'MLD_NEW';
  thickboxOpenUrl( 'Quick Create Lead',  'xhpTBMldNew.jsp'+genappend_submiturl+'&width=980&height=480' );
  setTimeout( 'blockAllAndPopupAutoComplete()', 700 );
}


function blockAllAndPopupAutoComplete()
{
  if ( __popupContent == 'ORG_NEW' || __popupContent == 'PSN_NEW' )
  {
    try {
      autocomplete('inlinePopupCreateForm_zip', 'zip-autocomplete-popup', 'xhpZipCode.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateForm' );
    } catch ( Exception ) {}
  }
  if ( __popupContent == 'OPP_NEW' )
  {
    try {
      autocomplete('ajaxnewopp_prodName_0', '_product-autocomplete-popup_0', 'xhpAutoCompleteProduct.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateFormOpp' );
      autocomplete('ajaxnewopp_prodName_1', '_product-autocomplete-popup_1', 'xhpAutoCompleteProduct.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateFormOpp' );
      autocomplete('ajaxnewopp_prodName_2', '_product-autocomplete-popup_2', 'xhpAutoCompleteProduct.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateFormOpp' );
      autocomplete('ajaxnewopp_prodName_3', '_product-autocomplete-popup_3', 'xhpAutoCompleteProduct.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateFormOpp' );
      autocomplete('ajaxnewopp_prodName_4', '_product-autocomplete-popup_4', 'xhpAutoCompleteProduct.jsp'+genappend_submiturl, 'strInput', null, 'inlinePopupCreateFormOpp' );
    } catch ( Exception ) {}
  }
}



function thickboxOpenUrl( tbcaption, tburl )
{
  tb_show( tbcaption, tburl);
//  tb_remove();
}

function ajax_gen_ui_helper_hidediv()
{
  var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
  gen_ui_helper_div.style.visibility = 'hidden';
  gen_ui_helper_div.style.display = "none";
  adjustiFrame( gen_ui_helper_div );
}





function ajaxneworg_proceed_save()
{
  currMouseX = window.event.clientX+document.body.scrollLeft;
  currMouseY = window.event.clientY+document.body.scrollTop;

  var ajax_neworg_submiturl = 'xhpCustomerNew.jsp'+genappend_submiturl + '&custName=' + escape( $('ajaxneworg_custName').value );
  postXmlHttp( ajax_neworg_submiturl, 'ajaxneworg_ajax_neworg_followup()' );
}
function ajaxneworg_ajax_neworg_followup()
{
  var retStr = LTrim( _xmlHttpRequestObj.responseText ); 
    
  if ( retStr.substring(0,7) == 'SUCCESS' )
  {
    Ext.Ajax.request({
      url:  'xhpCustomerNew.jsp'+genappend_submiturl+'&inlinePopupCreate=Y',
      form: 'ajaxneworg',
      success: function(response, opts) {
        var rt = jQuery.trim( response.responseText );
        if ( rt.indexOf("SUCCESS") >= 0 )
        {
          alert( rt );
          tb_remove();
          baseScreenReload();
        }
        else
        {
          ajaxnew_followup_displaymsg( rt );
        }
      },
      failure: function(response, opts) {
          alert( 'sendmail failure with status code ' + response.status );
      }
    });
  }
  else
  {
          ajaxnew_followup_displaymsg( retStr );
  }
}




function ajaxnewpsn_proceed_save()
{
  currMouseX = window.event.clientX+document.body.scrollLeft;
  currMouseY = window.event.clientY+document.body.scrollTop;

  var ajax_newpsn_submiturl = 'xhpContactNew.jsp'+genappend_submiturl + '&contactFN=' + escape( $('ajaxnewpsn_firstName').value )
              + '&contactLN=' + escape( $('ajaxnewpsn_lastName').value ) + '&ajaxform=Y';
  postXmlHttp( ajax_newpsn_submiturl, 'ajaxnewpsn_ajax_newpsn_followup()' );
}
function ajaxnewpsn_ajax_newpsn_followup()
{
  var retStr = LTrim( _xmlHttpRequestObj.responseText ); 
    
  if ( retStr.substring(0,7) == 'SUCCESS' )
  {
    ajaxnewpsn_proceed_save_confirm();
  }
  else
  {
    ajaxnew_followup_displaymsg( retStr );
  }
}
function ajaxnewpsn_proceed_save_confirm()
{
    Ext.Ajax.request({
      url:  'xhpContactNew.jsp'+genappend_submiturl+'&inlinePopupCreate=Y',
      form: 'ajaxnewpsn',
      success: function(response, opts) {
        var rt = jQuery.trim( response.responseText );
        if ( rt.indexOf("SUCCESS") >= 0 )
        {
          alert( rt );
          tb_remove();
          baseScreenReload();
        }
        else
        {
          ajaxnew_followup_displaymsg( rt );
        }
      },
      failure: function(response, opts) {
          alert( 'sendmail failure with status code ' + response.status );
      }
    });
}





function ajaxnewopp_proceed_save()
{
  currMouseX = window.event.clientX+document.body.scrollLeft;
  currMouseY = window.event.clientY+document.body.scrollTop;

  var ajax_newopp_submiturl = 'xhpOppNew.jsp'+genappend_submiturl + '&oppName=' + escape( $('ajaxnewopp_oppName').value )
            + '&customerId=' + $('ajaxnewopp_customerId').value  + '&contactId=' + $('ajaxnewopp_contactId').value;
  postXmlHttp( ajax_newopp_submiturl, 'ajaxnewopp_ajax_newopp_followup()' );
}
function ajaxnewopp_ajax_newopp_followup()
{
  var retStr = LTrim( _xmlHttpRequestObj.responseText ); 

  if ( retStr.substring(0,7) == 'SUCCESS' )
  {
    Ext.Ajax.request({
      url:  'xhpOppNew.jsp'+genappend_submiturl+'&inlinePopupCreate=Y',
      form: 'ajaxnewopp',
      success: function(response, opts) {
        var rt = jQuery.trim( response.responseText );
        if ( rt.indexOf("SUCCESS") >= 0 )
        {
          alert( rt );
          tb_remove();
          baseScreenReload();
        }
        else
        {
          ajaxnew_followup_displaymsg( rt );
        }
      },
      failure: function(response, opts) {
          alert( 'sendmail failure with status code ' + response.status );
      }
    });
  }
  else
  {
          ajaxnew_followup_displaymsg( retStr );
  }
}



function ajaxnewmld_proceed_save()
{
  if( $( 'tbnewmld_firstName' ).value == '' )
  {
      alert('First Name is a required field. ');
      $('tbnewmld_firstName').focus(); return 0; 
  }
  if( $( 'tbnewmld_lastName' ).value == '' )
  {
      alert('Last Name is a required field. ');
      $('tbnewmld_lastName').focus(); return 0; 
  }

  currMouseX = window.event.clientX+document.body.scrollLeft;
  currMouseY = window.event.clientY+document.body.scrollTop;

    Ext.Ajax.request({
      url:  'xhpMldNew.jsp'+genappend_submiturl+'&inlinePopupCreate=Y',
      form: 'ajaxnewmld',
      success: function(response, opts) {
        var rt = jQuery.trim( response.responseText );
        if ( rt.indexOf("SUCCESS") >= 0 )
        {
          alert( rt );
          tb_remove();
          baseScreenReload();
        }
        else
        {
          ajaxnew_followup_displaymsg( rt );
        }
      },
      failure: function(response, opts) {
          alert( 'create failure with status code ' + response.status );
      }
    });
}


function ajaxnew_followup_displaymsg( msg )
{
  var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
  if ( gen_ui_helper_div != null )
  {
    gen_ui_helper_div.innerHTML = msg; 
    gen_ui_helper_div.style.visibility = 'visible';
    gen_ui_helper_div.style.display = "block";
    gen_ui_helper_div.style.top   = (currMouseY - 100) + "px";
    gen_ui_helper_div.style.left  = (currMouseX - 20) + "px";
    gen_ui_helper_div.style.width = "330px";
    adjustiFrame( gen_ui_helper_div );
  }
}



function ajaxnewopp_change_stage()
{
  var ajax_newopp_submiturl = 'xhpOppNew.jsp'+genappend_submiturl + '&act=CHANGE_STAGE&stage=' + escape( $('ajaxnewopp_oppStage').value );
  postXmlHttp( ajax_newopp_submiturl, 'ajaxnewopp_ajax_change_stage_followup()' );
}
function ajaxnewopp_ajax_change_stage_followup()
{
  var retStr = LTrim( _xmlHttpRequestObj.responseText ); 
  eval( retStr );
}

  function tb_new_mld_nameTyping()
  {
      $( 'tbnewmld_leadDesc' ).value = $( 'tbnewmld_firstName' ).value + ' ' + $( 'tbnewmld_lastName' ).value;
      if ( $('tbnewmld_companyName').value != '' )
        $( 'tbnewmld_leadDesc' ).value += ' @ '+$('tbnewmld_companyName').value;
  }
