// ---------------------------------------------------
// Global JavaScript
// ---------------------------------------------------

function changePage(myUrl) {
  document.location.href = myUrl;
}


// ---------------------------------------------------
// Pop Ups
// ---------------------------------------------------

function popUpWin(myUrl,myWidth,myHeight,myTop,myLeft) {
  var newWindow = window.open(myUrl,"Backpage","scrollbars=1,status=1,resizable=1,menubar=1,width=" + myWidth + ",height=" + myHeight + ",top=" + myTop + ",left=" + myLeft);
  newWindow.focus();
}

function popUpMap(myUrl) {
  var mapWindow = window.open(myUrl,"BackpageMap","scrollbars=1,status=1,resizable=1,menubar=1,location=1,toolbar=1,height=570,width=770,top=10,left=10");
  mapWindow.focus();
}


// ---------------------------------------------------
// Cookies
// ---------------------------------------------------

function getCookie(name) {
  var start = document.cookie.indexOf(name + "=");
  var len = start + name.length + 1;
  if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
  if (start == -1) return null;
  var end = document.cookie.indexOf( ";", len );
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len, end));
}  

function setCookie(name, value, expires, domain) {
  // set expires
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) expires = expires * 1000 * 60 * 60 * 24;
  var expires_date = new Date( today.getTime() + (expires) );

  // set cookie
  document.cookie = name + "=" + escape( value ) + ";path=/" +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( domain  ) ? ";domain=" + domain : "" );
}


function deleteCookie (name) {
    setCookie(name, "", -1);
}

// ---------------------------------------------------
// HTML Helper Toolbar
// -- adapted from http://www.massless.org/mozedit/
// ---------------------------------------------------

function mozWrap(input, lft, rgt) {
  var txtarea = document.f[input];
  var selLength = txtarea.textLength;
  var selStart = txtarea.selectionStart;
  var selEnd = txtarea.selectionEnd;
  if (selEnd==1 || selEnd==2) selEnd=selLength;
  var s1 = (txtarea.value).substring(0, selStart);
  var s2 = (txtarea.value).substring(selStart, selEnd)
  var s3 = (txtarea.value).substring(selEnd, selLength);
  txtarea.value = s1 + lft + s2 + rgt + s3;
}

function IEWrap(lft, rgt) {
  var strSelection = document.selection.createRange().text;
  if (strSelection != "") {
    document.selection.createRange().text = lft + strSelection + rgt;
  }
}

function wrapSelection(input, lft, rgt) {
  if (document.all) { IEWrap(lft, rgt); }
  else if (document.getElementById) { mozWrap(input, lft, rgt); }
}
  

// ---------------------------------------------------
// Calendar
// ---------------------------------------------------

function popUpCalendar(inputName) {
  var calMonth = "";
  var calDay = "";
  var calYear = "";
  var sepStyle = "-";

  var dateValue = document.f[inputName].value;

  if (dateValue) {
    if (dateValue.indexOf("/") != -1) {
      d = dateValue.split("/");
      sepStyle = "/";
    } else {
      d = dateValue.split("-");
      sepStyle = "-";
    }

    var calMonth = d[0]-1;
    var calDay = d[1];
    var calYear = d[2];
  }
  else {
    var dateObj = new Date();
    calMonth = dateObj.getMonth();
    calDay = dateObj.getDate();
    calYear = dateObj.getFullYear();
  }

  var popUpUrl = "/gyrobase/tools/PopUpCalendar.html?";
  popUpUrl +=  "dateField=" + escape(inputName);
  popUpUrl += "&calMonth="  + escape(calMonth);
  popUpUrl += "&calDay="    + escape(calDay);
  popUpUrl += "&calYear="   + escape(calYear);
  popUpUrl += "&sepStyle="  + escape(sepStyle);
  popUpUrl += "&initMonth=" + escape(calMonth);
  popUpUrl += "&initDay="   + escape(calDay);
  popUpUrl += "&initYear="  + escape(calYear);

  if (window.left) var leftX = 350 + window.left;
  else if (window.screenX) var leftX = 350 + window.screenX;
  if (window.top) var topY = 200 + window.top;
  else if (window.screenY) var topY = 200 + window.screenY;
  window.open(popUpUrl,'','width=340,height=265,left='+leftX+',screenx='+leftX+',top='+topY+',screeny='+topY+',scrollbars=no,toolbar=no,location=no,status=yes,directories=no,personalbar=no');
}
  

// ---------------------------------------------------
// Extra Print Fields
// ---------------------------------------------------

function showName(){
  var showIt = false;
  if (document.f.printAd && document.f.printAd.checked) showIt = true;
  else if (document.f.promotion && document.f.promotion.checked) showIt = true;
  
  if (!showIt) {
    if (document.f.printAd){
      for (var i=0; i < document.f.printAd.length; i++){
        if (document.f.printAd[i].checked){
          showIt = true;
        }
      }
    }      
    if (document.f.promotion){
      for (var i=0; i < document.f.promotion.length; i++){
        if (document.f.promotion[i].checked){
          showIt = true;
        }
      }
    }
  }
  if (showIt) document.getElementById("namePhoneTable").style.display = 'block';
  else document.getElementById("namePhoneTable").style.display = 'none';                  
}
  

// ---------------------------------------------------
// Limit Text
// ---------------------------------------------------

function limitText(limitField, limitNum) {
  if (limitField.value.length > limitNum) limitField.value = limitField.value.substring(0, limitNum);
  document.getElementById("dynOfferWordCount").innerHTML = document.f.offer.value.length;
  document.getElementById("dynDescriptionWordCount").innerHTML = document.f.description.value.length;
}


// ---------------------------------------------------------------------
// jQuery dependent extensions.
// ---------------------------------------------------------------------

(function ($, undefined) {
    if ($ === undefined) return;
    
    $.fn.loadOtherAdsByThisUser = function () {
        var url =   "/gyrobase/classifieds/OtherAdsByThisUser"
                  + "?lang=" + encodeURIComponent($("html").attr("lang"));
        this.each(function() {
            $(this).load(url + "&oid=" + $(this).data("oid"));
        });
    };
    
    $.fn.disableOnSubmit = function () {
        var submitButtons = $("input[type=submit]", this);
        
        submitButtons.click(function (e) {
            // Duplicate the value from the button clicked into the form.
            // Use data-value from the button if it exists.
            // We'd use .clone() here if IE allowed us to change the type.
            var name = $(this).attr("name");
            var val = $(this).data("value") || $(this).val();
            var hidden = $("<input type='hidden'>");
            hidden.attr("name", name).attr("value", val).insertBefore(this);
            
            // Disable clicked submit button and replace with "Processing…".
            this.disabled = true;
            $(this).data("origValue", $(this).val()).val("Processing…");
            $(this).data("submitted", true).parents("form").submit();
        });
    };
    
})(window.jQuery);

// ---------------------------------------------------------------------

