nn4=(document.layers)?true:false;
ie=(document.all)?true:false;

function PageInit(aid, gid) {
  FillLocationControls(document.forms[0].s_LocationID.value, false, aid, gid);
}

// refresh - powoduje odświeżenie list lokalizacji bez optymalizacji
function FillLocationControls(loccode, refresh, aid, gid) {
  window.open('LocDynUpdate.asp?bigt=0&code=' + loccode +
              '&ptid=' + document.forms[0].s_PropertyTypeID.value +
              '&s_TypeOfListing=' + document.forms[0].s_TypeOfListing.value +
              '&s_NewPreowned=' + document.forms[0].s_NewPreowned.value +
              '&refresh=' + (refresh ? 1:0) + '&aid=' + aid + '&gid=' + gid,
              'workarea');
}

function FillForm(selcode, refresh, aid, gid) {
  document.forms[0].s_LocationID.value = selcode;
  FillLocationControls(selcode, refresh, aid, gid);
}

function ResetLocation() {
 document.forms[0].SelLoc_2.selectedIndex = 0;
 document.forms[0].SelLoc_3.length = 0;
 document.forms[0].SelLoc_3.disabled = true;
 document.forms[0].SelLoc_4.length = 0;
 document.forms[0].SelLoc_4.disabled = true;
 document.forms[0].SelLoc_5.length = 0;
 document.forms[0].SelLoc_5.disabled = true;
 document.forms[0].SelLoc_6.length = 0;
 document.forms[0].SelLoc_6.disabled = true;
 document.forms[0].SelLoc_7.length = 0;
 document.forms[0].SelLoc_7.disabled = true;
 document.forms[0].s_LocationID.value = 'PL-';
}

// Ustawia atrybut 'selected' w kontrolkach wyboru lokalizacji
// zgodnie z zadanym kodem lokalizacji.
function SetSelectedLocation(loccode)
{
  var form = document.forms[0];
  for(var locLevel = 2; locLevel <= 7; locLevel++){
    var cntrl = form['SelLoc_' + locLevel];
    for(var i = 1; i < cntrl.length; i++){
      cntrl.options[i].selected = (cntrl.options[i].value == loccode.slice(0, locLevel*3));
    }
  }
}

function Sort(sortord) {
 if(document.forms[0].s.value == sortord) {
  if(document.forms[0].ord.value == "ASC") document.forms[0].ord.value = "DESC"
  else document.forms[0].ord.value = "ASC";
 }
 else document.forms[0].s.value = sortord;
 document.forms[0].pn.value = '1';
 document.forms[0].step.value = '5';
 document.forms[0].submit();
}

function Show(listingid, listingDisplayID) {
 document.forms[0].listingid.value = listingid;
 document.forms[0].ListingDisplayID.value = listingDisplayID;
 document.forms[0].step.value = '6';
 document.forms[0].submit();
}

function Back() {
 document.forms[0].step.value = document.forms[0].step.value - 1;
 document.forms[0].submit();
}

function GoToPage(pageNo) {    
 document.forms[0].pn.value = pageNo
 document.forms[0].step.value = '5';
 document.forms[0].submit()
}

function AddRemoveID(checkElem) {
 ifAdding = checkElem.checked
 lstList = checkElem.form.AllLstList.value
 if(ifAdding) {
  if(lstList.length > 3) lstList = lstList + ", "
  lstList = lstList + checkElem.value
 }
 else {
  if(lstList.search(checkElem.value + ", ") != -1) lstList = lstList.replace(checkElem.value + ", ", "")
  else
   if(lstList.search(", " + checkElem.value) != -1) lstList = lstList.replace(", " + checkElem.value, "")
   else lstList = lstList.replace(checkElem.value, "")
 }
 checkElem.form.AllLstList.value = lstList
}

function Email(forfriend) {
 lstList = document.forms[0].AllLstList.value;
 if(lstList.length==0) alert('Proszę zaznaczyć przynajmniej jedną ofertę');
 else {
    var w = window.open('Email.asp?aid='+document.forms[0].aid.value+
     '&alllstlist='+document.forms[0].AllLstList.value+'&forfriend='+forfriend,
     'Email', 'scrollbars=no,status=no,width=600,height=240,left=100,top=200');
    w.focus();
 }
}

function EmailNoListing(criteria) {
    var w = window.open('Email.asp?aid='+document.forms[0].aid.value+'&EmailNoListing=1&criteria=' +
                        escape(criteria),
     'Email', 'scrollbars=no,status=no,width=600,height=440,left=100,top=200');
    w.focus();
}

function EmailListing(forfriend, contactToID) {
  var w = window.open('Email.asp?aid='+document.forms[0].aid.value+
     '&alllstlist='+document.forms[0].listingid.value+'&forfriend='+forfriend +
     '&contactToID=' + contactToID,
     'Email', 'scrollbars=no,status=no,width=500,height=240,left=100,top=200');
  w.focus();
}

function MSSubmitForm(form, action, target)
{
  if(typeof(form) == 'string') form = document.forms[form];
  var oldAction = form.action;
  var oldTarget = form.target;
  if(action){
    form.action = action;
  }
  if(target){
    form.target = target;
  }
  form.submit();
  form.action = oldAction;
  form.target = oldTarget;
}

function MSPrintList() {
  var form = document.forms['ms_form']; 
  if(form.AllLstList.value.length==0) alert('Proszę zaznaczyć przynajmniej jedną ofertę');
  else {
    var w = window.open('', 'MSPrintList', 'width=650,status=no,dependent=yes,toolbar=no,resizable=yes,scrollbars=yes');
    MSSubmitForm(form, 'Search.asp?MSPrintList=1', 'MSPrintList');
    w.focus();
  }
}

function GoStep(stepNo) {    
 document.forms[0].step.value = stepNo
 document.forms[0].submit()
}

function LocChanged(LocCode) {    
 document.forms[0].s_LocationID.value = LocCode;
 document.forms[0].step.value = document.forms[0].step.value - 1;
 document.forms[0].submit()
}

function MSFormSubmit(target) {
  var oldDisabled = ms_form.s_PropertyTypeID.disabled;
  ms_form.s_PropertyTypeID.disabled = false;
  if(target){
    var w = window.open('', target,
       'scrollbars=yes,status=no,width=750,height=650,left=100,top=200,resizable=yes');
    ms_form.target = target;
  }
  ms_form.submit();
  if(target) w.focus();
  ms_form.s_PropertyTypeID.disabled = oldDisabled;
  ms_form.target = '';
}

function onkey(evt, vtype) {
 if(nn4) keycode = evt.which 
 else {
  keycode = window.event.keyCode;
  if(keycode==44) {
   // zamiana przecinka na kropkę :-)
   keycode = 46;
   window.event.keyCode = 46;
  }
 }
 switch(vtype) {
 case 2: // INTEGER
  return (keycode > 47 && keycode < 58);
  break;
 case 4: // FLOAT
  return ((keycode > 47 && keycode < 58) || keycode==46);
  break;
 case 8: // DATE
  return ((keycode > 47 && keycode < 58) || keycode==45);
  break;
 }
}

function PrintListing(listingID)
{
  var form = ms_form;
  var w;
  w = window.open("../listings/print_main_frame.asp?list_view_select=2" +
        "&PrintoutTarget=xxx&print_photo=1&lpr_Currency=1&lpr_ListingID=" + listingID,
        "Printing",
        'scrollbars=no,status=no,width=700,height=600,dependent=yes,toolbar=no' );
  w.focus();
}

function PrintWindow() {
  if (typeof(window.print) == 'undefined') 
  	alert('Uwaga: Aby wydrukować, proszę wybrać przycisk [Drukuj] z paska narzędzi.');  //transl 
  else {
  	window.print();				
  }
}

function ShowAutoNotificationForm()
{
  ms_form.step.value = '10';
  MSFormSubmit();
}

function MSASave(step)
{
  if(!CheckEmailField(ms_form.ma_Email, 'E-mail', true)) return;
  if(!CheckFieldNotEmpty(ms_form.ma_ProfileName, 'Nazwa profilu')) return;
  ms_form.step.value = step.toString();
  MSSubmitForm(ms_form, 'Search.asp?ma_Save=1');
}

function MSGoogleMapBOnClose()
{
  document.getElementById('VLMainTable').style.visibility = 'visible';
  var hb = document.getElementById('MSGoogleMapBHideButton');
  HideElement(hb);
}

function MSGoogleMapBOnShow()
{
  document.getElementById('VLMainTable').style.visibility = 'hidden';
  var gmElem = document.getElementById('GoogleMapBElement');
  var hb = document.getElementById('MSGoogleMapBHideButton');
  hb.style.top = (GoogleMapBObj.gmElemHeight + 5) + 'px';
  ShowElement(hb);
}

