// JavaScript Document


function ShowHideSearchGeneral(divSearch, divResult, divResultCaption) {
  
  div = document.getElementById(divSearch);
  if ((div.style.display == 'none') || (div.style.display == '')) {
     div.style.display = "block";
  } else {
     div.style.display = "none";
  }
  
  if (document.getElementById(divResult).innerHTML == '') {
    document.getElementById(divResultCaption).style.display = 'none';
  }

}

function ShowHideSearch() {
  ShowHideSearchGeneral('divLocalitySearch', 'divResult', 'divResultsCaption'); 
}


function ShowHideProductSearch() {
  ShowHideSearchGeneral('divProductSearch', 'divProductResult', 'divProductResultsCaption') ;
}


function ShowHideTrademarkSearch() {
  ShowHideSearchGeneral('divTrademarkSearch', 'divTrademarkResult', 'divTrademarkResultsCaption') ;
}

function HideSearch() {
  document.getElementById('divLocalitySearch').style.display = "none";
} 


function GoDeleteOffer(offer_id, authCode) {
  if (AreYouSure((window.AP_SIF_SURE_DEL_OFFER || 'Сигурни ли сте, че искате да изтриете офертата?'))) {
    location.href='offer_save.asp?offer_id=' + offer_id + '&delete=1' + (authCode != '' ? '&code='+authCode : '') ;
  }
}


function fhPrice(o) {
   alertAjaxFailure(o,(window.AP_SIF_ERRIR_SAVE_PRICE || 'Грешка при записа на цената!'))   
}

function alertAjaxFailure(o, problem) {
   var reason = ''
   if (o.status == 0) {
      reason = (window.AP_SIF_ERRIR_COMMUNICATION || 'Проблем в комуникацията.');
   } else if(o.status == -1 || o.status == 408) {
      reason = (window.AP_SIF_ERROR_TIMEOUT || 'Операцията е прекратена поради изтичане на допустимото време за изпълнение.');
   } else {
      reason = o.statusText
   }
   
   var FullMessage = problem + (reason != ''?'\n\n':'') + reason + '\n' + (window.AP_SIF_TRY_AGAIN || 'Опитайте отново.');
   alert(FullMessage);
}

function trim(sString) 
{
  while (sString.substring(0,1) == ' ')
  {
      sString = sString.substring(1, sString.length);
  }
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
    sString = sString.substring(0,sString.length-1);
  }

  return sString;
}

function shPrice(o) {
  if (trim(o.responseText) == '') {
  //OK
  } else {
  //върнат месидж
    alert(o.responseText);
  }
}

var oCallback = { success: shPrice, failure: fhPrice, timeout:20000 }

function SavePrice(offer_id, new_total_price) {
  //alert("http://"+host+"/sif/data/offerPriceSave.asp?id=" + offer_id + '&price=' + new_total_price);
  YAHOO.util.Connect.asyncRequest('GET', "http://"+host+"/sif/data/offerPriceSave.asp?id=" + offer_id + '&price=' + new_total_price, oCallback); 
}



function doGetCaretPosition (oField) {
  var iCaretPos = 0;
  
  //IE
  if (document.selection) {
    oField.focus ();
    var oSel = document.selection.createRange ();
    oSel.moveStart ('character', -oField.value.length);
    iCaretPos = oSel.text.length;
  }
  //FF
  else if (oField.selectionStart || oField.selectionStart == '0')
  iCaretPos = oField.selectionStart;

  
  return (iCaretPos);
}


function doSetCaretPosition (oField, iCaretPos) {
  // IE 
  if (document.selection) {
    oField.focus ();
    var oSel = document.selection.createRange ();
    oSel.moveStart ('character', -oField.value.length);
    oSel.moveStart ('character', iCaretPos);
    oSel.moveEnd ('character', 0);
    oSel.select ();
  }

  // Firefox 
  else if (oField.selectionStart || oField.selectionStart == '0') {
    oField.selectionStart = iCaretPos;
    oField.selectionEnd = iCaretPos;
    oField.focus ();
  }
}

