﻿/*!
 * salesprocess.js
 * common JavaScript for salesprocess all pages.
 *
 * @application Salesprocess
 * @version 8.2
 * @author mathieu.bonin, fernando.mendez, emmanuel.sammut
 * @copyright (c) Travelsoft 2010
 */


/**
 * Protected jQuery alias $.
 */
(function($) {

  // Create travesoft Object
  var travelsoft = {
    salesprocess: {
      getYear: function(){
        var date = new Date();
        return date.getFullYear();
      }
    }
  };

  // Generated copyright years date
  $("#yearCopyright").text(travelsoft.salesprocess.getYear());


  /**
   * !
   */
  $("#sbt_savePage").live("click", function(evt){
    // set variable
    var link = $(this).parent().attr("action");
    // don't follow
    evt.preventDefault();

    $.ajax({
      type: "POST",
      url: link,
      data: $(this).parent().serialize(),
      success: function(data){
        alert(data);
      }
    });
  });

  /**
   * Generic open popup.
   *
   * @param {class} popup,
   * @param {class} w, [optional] width popup number
   * @param {class} h, [optional] height popup number
   * @param {class} sb, [optional] scrollbars number 1(yes) or 0(no)
   * @use <a href="http:www.mylink.com" class="popup w750 h480 sb1">my link</a>
   */
  $("a.popup").live("click", function(){
    var classAttr = $(this).attr("class"),
        URL = $(this).attr("href"),
        options = "fullscreen=no",
        reg,
        popup;
    // set width parameter
    reg = /w([\d]+)/.exec(classAttr);
    if (reg && reg.length > 1) {
      options += ", width=" + reg[1];
    } else {
      options += ", width=800";
    }
    // set height parameter
    reg = /h([\d]+)/.exec(classAttr);
    if (reg && reg.length > 1) {
      options += ", height=" + reg[1];
    } else {
      options += ", height=600";
    }
    // set scrollbars parameter
    reg = /sb([\d]+)/.exec(classAttr);
    if (reg && reg.length > 1) {
      options += ", scrollbars=" + reg[1];
    } else {
      options += ", scrollbars=yes";
    }
    // call popup
    if (URL) {
      popup = window.open(URL, "popup", options);
      popup.window.focus();
    }
    return false;
  });

  // Open links to backstep and home page
  $("#backstep_1, #backstep_2, #home").click(function() {
    var homeUrl = $(this).find("a").attr("href");
    window.location = homeUrl;
  });

  // Open link to product page
  $("#product").click(function(){
    var productUrl = $(this).attr("value");
    window.location = productUrl;
  });

  // Initializes trip details
  $("#moreTripDetails").hide();
  $("#lessTripDetails").show();

    // Trip details
    var detail = messagesMap['reservation.header.detail'];

    $("#linkMoreTripDetails").append(" <span class=\"link\">+ " + detail + "</span>");
    $("#linkMoreTripDetails span").click(function(){
      if($("#lessTripDetails").is(":hidden")){
        $("#moreTripDetails").slideUp("normal", function(){
          $("#lessTripDetails").show();
          $("#linkMoreTripDetails span").html("+ " + detail);
        });
      } else {
        $("#linkMoreTripDetails span").html("- " + detail);
        $("#lessTripDetails").hide();
        $("#moreTripDetails").slideDown("normal", function(){
          $.scrollTo("#moreTripDetailsAnchor", 800);
        });
      }
    });

    /**
     * DEBUG TOOLS
     */
    if ($("#debugTools").length) {
      var style = $("<link />", { type: "text/css", rel: "stylesheet", href: document.createStyleSheet ? document.createStyleSheet(salesprocess.cons.debugCSS) : salesprocess.cons.debugCSS });
      $("head").append(style);
      $.getScript(salesprocess.cons.jsUtils);
      $.getScript(salesprocess.cons.jsDegub);
    }

})(jQuery);

function getURLParameter(myParameterName) {
  var sURL = document.URL.toString();
  if (sURL.indexOf("?") > 0) {
    var arrParams = sURL.split("?");
    var arrURLParams = arrParams[1].split("&");
    var arrParamNames = new Array(arrURLParams.length);
    var arrParamValues = new Array(arrURLParams.length);
    var i = 0;

    for (i = 0; i < arrURLParams.length; i++) {
      var sParam = arrURLParams[i].split("=");
      arrParamNames[i] = sParam[0];
      if (sParam[1] != "")
        arrParamValues[i] = unescape(sParam[1]);
      else
        arrParamValues[i] = "";
    }

    for (i = 0; i < arrURLParams.length; i++) {
      if (arrParamNames[i] == myParameterName) {
        return arrParamValues[i]
      }
    }
  } else {
    return "";
  }
}

