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);
}


// 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 )
{
  blockAllAndPopupOnResize();
  window.onresize = blockAllAndPopupOnResize;

  $('ajax_inline_popup_div').innerHTML = $('ajax_inline_popup_div_backup').value + ' <BR> &nbsp; <img id="waitgif" src="graphics/wait2.gif" align=absmiddle width=20 height=20>';

  __popupContent = popupContent;
  postXmlHttp( 'xhpLOVDispatcher.jsp'+genappend_submiturl+'&pc='+popupContent, 'blockAllAndPopupRefresh()' );
}
function blockAllAndPopupPsnNew( popupContent, custid )
{
  blockAllAndPopupOnResize();
  window.onresize = blockAllAndPopupOnResize;

  $('ajax_inline_popup_div').innerHTML = $('ajax_inline_popup_div_backup').value + ' <BR> &nbsp; <img id="waitgif" src="graphics/wait2.gif" align=absmiddle width=20 height=20>';

  __popupContent = popupContent;  // 'PSN_NEW'
  postXmlHttp( 'xhpLOVDispatcher.jsp'+genappend_submiturl+'&pc='+popupContent+'&custid='+custid, 'blockAllAndPopupRefresh()' );
}
function blockAllAndPopupOppNew( popupContent, custid, cnctid, productId )
{
  blockAllAndPopupOnResize();
  window.onresize = blockAllAndPopupOnResize;

  $('ajax_inline_popup_div').innerHTML = $('ajax_inline_popup_div_backup').value + ' <BR> &nbsp; <img id="waitgif" src="graphics/wait2.gif" align=absmiddle width=20 height=20>';

  __popupContent = popupContent;  // 'OPP_NEW'
  postXmlHttp( 'xhpLOVDispatcher.jsp'+genappend_submiturl+'&pc='+popupContent+'&custid='+custid+'&cnctid='+cnctid+'&productId='+productId, 'blockAllAndPopupRefresh()' );
}
function blockAllAndPopupOppNew2( custid, cnctid, productId )
{
  blockAllAndPopupOnResize();
  window.onresize = blockAllAndPopupOnResize;

  $('ajax_inline_popup_div').innerHTML = $('ajax_inline_popup_div_backup').value + ' <BR> &nbsp; <img id="waitgif" src="graphics/wait2.gif" align=absmiddle width=20 height=20>';

  __popupContent = 'OPP_NEW';
  postXmlHttp( 'xhpLOVDispatcher.jsp'+genappend_submiturl+'&pc=OPP_NEW&custid='+custid+'&cnctid='+cnctid+'&productId='+productId, 'blockAllAndPopupRefresh()' );
}
function blockAllAndPopupRefresh()
{
  $('ajax_inline_popup_div').innerHTML = _xmlHttpRequestObj.responseText; 
  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 blockAllAndPopupOnResize()
{
  var blockAll = $( 'ajax_blockall_div' );

  blockAll.style.top   = "0px";
  blockAll.style.left  = "0px";
  blockAll.style.width  = getPageSizeWithScroll()[0] + 100;
  blockAll.style.height = getPageSizeWithScroll()[1] + 100;
//  blockAll.style.zIndex = 1000000;
  blockAll.position = "relative";
  blockAll.style.visibility = 'visible';
  blockAll.style.display = "block";
  adjustiFrame( blockAll );

  var popup = $( 'ajax_inline_popup_div' );
  popup.style.top   = 60; // "60px";
  popup.style.left  = 30;  // "30px";
  popup.style.width  = 700;
  popup.style.height = 500;
  popup.style.background = "FFFFFF";
  popup.style.backgroundColor = "FFFFFF";
//  popup.style.zIndex = 1000001;
  popup.position = "relative";
  popup.style.visibility = 'visible';
  popup.style.display = "block";
  adjustiFrame( popup );
}

function blockAllAndPopupCancel()
{
  var blockAll = $( 'ajax_blockall_div' );

  blockAll.style.top   = "0px";
  blockAll.style.left  = "0px";
  blockAll.style.width  = 1;
  blockAll.style.height = 1;
  blockAll.style.zIndex = 1;
  blockAll.style.visibility = 'hidden';
  blockAll.style.display = "none";
  adjustiFrame( blockAll );

  var popup = $( 'ajax_inline_popup_div' );
  popup.style.top   = "0px";
  popup.style.left  = "0px";
  popup.style.width  = 1;
  popup.style.height = 1;
  popup.style.zIndex = 1;
  popup.style.visibility = 'hidden';
  popup.style.display = "none";
  adjustiFrame( popup );
}

function getPageSizeWithScroll()
{
  if (window.innerHeight && window.scrollMaxY) {// Firefox

		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight+ document.body.offsetTop;
		xWithScroll = document.body.offsetWidth+ document.body.offsetLeft;
  }
  	
  arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
  return arrayPageSizeWithScroll;
}


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' )
  {
    new iiframe(document.ajaxneworg);
  }
  else
  {
    var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
    if ( gen_ui_helper_div != null )
    {
      gen_ui_helper_div.innerHTML = retStr; 
      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 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' )
  {
    new iiframe(document.ajaxnewpsn);
  }
  else
  {
    var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
    if ( gen_ui_helper_div != null )
    {
      gen_ui_helper_div.innerHTML = retStr; 
      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 ajaxnewpsn_proceed_save_confirm()
{
    new iiframe(document.ajaxnewpsn);
}

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' )
  {
    new iiframe(document.ajaxnewopp);
  }
  else
  {
    var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
    if ( gen_ui_helper_div != null )
    {
      gen_ui_helper_div.innerHTML = retStr; 
      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 blockAllAndPopupOrgNewHandler( xmlHttpRequestObj )
{
//  if( xmlHttpRequestObj.readyState == 'complete' )
  {
    var retStr = LTrim( xmlHttpRequestObj.responseText ); 
    if ( retStr.substring(0,7) == 'SUCCESS' )
    {
      if ( ajax_eb_appcode == 'PPOL' || ajax_eb_appcode == 'PPOLB' )
      {
      }
      else
      {
        alert( retStr );
      }
      blockAllAndPopupCancel();
      baseScreenReload();
    }
    else
    {
      var gen_ui_helper_div = document.getElementById('gen_ui_helper_div');
      if ( gen_ui_helper_div != null )
      {
        gen_ui_helper_div.innerHTML = retStr; 
        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 );
      }
    }
  }
}


