/****************************************************************************************
	Description:   1st - The Exchange Prospects Site - Default Include JS 
	Created:       25/02/2009
	Author:        Mike Hearfield/Nick Elliott - Presentation Team, Cheltenham 
	Copyright:     Independent Specialist Technology (UK) Ltd. 2009, All Rights Reserved.
****************************************************************************************/


/******************** TABLE OF CONTENTS ********************/

// 1.  GLOBAL VARIABLES
// 2.  DOCUMENT READY (ON LOAD)
// 3.  DEBUG FUNCTIONALITY
// 4.  EQUAL HEIGHTS
// 5.  WAIT FOR
// 6.  JQUERY COOKIE PLUGIN

/************************************************************/


/******************** GLOBAL VARIABLES **********************/
	var strCookieContextText = "strCookieContextText";
	var strCookieContextId = "strCookieContextId";
	var pageContext = null;
/************************************************************/


/******************** DOCUMENT READY (ON LOAD) ********************/
	$(function() {
	    // check to see if there's a context in the cookie
	    if ($.cookie(strCookieContextText) != undefined) {
	        pageContext = $.cookie(strCookieContextText);
	    }
	    // Write out the copyright text
	    $('#divFooter').append('<p>&copy; Independent Specialist Technology (UK) Ltd and Exchange FS Limited, 2009 All rights reserved.</p>');

	    //Partner list page only
	    if ($("ul#partnersList").length > 0) { // make images vertically aligned within partners list page
	        $("ul#partnersList li a img").each(function() {
	            $(this).css({ marginTop: ($(this).parents("a").height() - $(this).height()) / 2 });
	        });
	    }

	    //'Request a fact sheet' or 'Request a demo' pages only
	    if ($("form.requestform").length > 0) { // validate form
	        $("form.requestform").submit(function() {
	            var isValid = true;
	            var ep = /^[^@]+@[^@ ,]{2,}\.[^@ ,]+[^.]$/i;
				if ($("#HowHear").val() == "Please Select") { isValid = invalidForm("Please select how you heard about us"); }
				else if ($("#Notes").val() == "") { 
					if($(this).attr("id") == "supportenquiry") {  
						isValid = invalidForm("Please fill in the Problem Details field");
					} else {
						isValid = invalidForm("Please provide some Comments");						
					}
				}
				else if ($("#Title").val() == "" || $("#Forenames").val() == "" || $("#Surname").val() == "") { isValid = invalidForm("Please enter your title, first name and surname."); }
	            else if ($("#ConName").val() == "") { isValid = invalidForm("Please enter your Name"); }
	            else if ($("#Company").val() == "") { isValid = invalidForm("Please enter your Company Name"); }
	            else if ($("#Telephone").val() == "") { isValid = invalidForm("Please enter your Telephone Number"); }
	            else if (!ep.test($("#EmailAddress").val())) { isValid = invalidForm("Please enter a valid email address"); }
	            else if ($("#Address1").val() == "" || $("#PostCode").val() == "") { isValid = invalidForm("Please enter an address and Post Code"); }
	            return isValid;
	        });
	    }

	    function invalidForm(message) {
	        alert(message);
	        var isValid = false;
	        return isValid;
	    }

	    //'Online Support' page only
	    if ($("select#ExpertKeys").length > 0) {
	        $("select#ExpertKeys").change(function() {
	            document.QuestionEntry.JavaScript.value = "true";
	            document.QuestionEntry.Target.value = "he/queryPost";
	            $("form#QuestionEntry").submit();
	        });
	    }

	    //'AO Offer' page only
	    if ($("a#aoOffer").length > 0) {
	        $("a#aoOffer").click(function(e) {
	            $("ol#aoOfferTandC").slideToggle("fast", function() {
	                $("a#TandCEnd").focus();
	                $("a#aoOffer").focus();
	            });
	            e.preventDefault();
	        });
	    }

	    //'provider matrix on Online Quotes' page only
	    if ($("div#div_matrixOuter").length > 0) {
	        //toggleTab($("div_matrixOuter a:first"), "div_matrixTerm", "Term assurance");
	        //toggleSubTab($("div_matrixOuter a:first"), "div_matrixTerm_assurance", "Assurance");
	        //toggleTab(firstLink("div_matrixOuter"), "div_matrixTerm", "Term assurance");
	        //toggleSubTab(firstLink("div_matrixTerm"), "div_matrixTerm_assurance", "Assurance");
	    }
	});
/************************************************************/


/******************** DEBUG FUNCTIONALITY ***************/
function log(s) {
  if (typeof console != "undefined" && typeof console.debug != "undefined") {
    console.log(s);
  } else {
    alert(s);
  }
}
/************************************************************/


/******************** EQUAL HEIGHTS ***************/

$.fn.equalHeights = function(px) {
  var currentTallest = 0;
  $(this).each(function() {
    if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
  });
  if ($.browser.msie) { $(this).css({ 'height': currentTallest }); } // for ie6, set height since min-height isn't supported
  $(this).css({ 'min-height': currentTallest });
  return this;
};
/************************************************************/


/******************** WAIT FOR **********************/
// waitFor function written by Renzo Kooi, used to poll for completion of
// something, needs to be replaced by event management.
/* usage:
waitFor( function(){return [test if your function finished]}, //=>what
         function(){[function to do something]}, //=>action
         5); //=>maxtime
*/
function waitFor(what,action,maxtime){
    maxtime = (maxtime || 3)*1000;
    var counter = 0;
    function chkwhat() {
      var tst = what instanceof Function ? what() : what;
        if (!tst && counter<maxtime) {
           counter += 10;
           setTimeout(chkwhat,10);
        } else {
          if (counter >= 3000) {
            /* waiting timed out, return a message or something */
            clearTimeout(0);
						return true;
          }
       clearTimeout(0);
       return action();
      }
    }
  chkwhat();
}
/************************************************************/


/******************** JQUERY COOKIE PLUGIN **********************/
/* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* http://plugins.jquery.com/project/Cookie
*/
jQuery.cookie = function(name, value, options) {
  if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
      value = '';
      options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
      var date;
      if (typeof options.expires == 'number') {
        date = new Date();
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
      } else {
        date = options.expires;
      }
      expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    // CAUTION: Needed to parenthesize options.path and options.domain
    // in the following expressions, otherwise they evaluate to undefined
    // in the packed version for some reason...
    var path = options.path ? '; path=' + (options.path) : '';
    var domain = options.domain ? '; domain=' + (options.domain) : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  } else { // only name given, get cookie
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
      var cookies = document.cookie.split(';');
      for (var i = 0; i < cookies.length; i++) {
        var cookie = jQuery.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if (cookie.substring(0, name.length + 1) == (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }
};
/************************************************************/

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* $LastChangedDate: 2007-07-21 18:45:56 -0500 (Sat, 21 Jul 2007) $
* $Rev: 2447 $
*
* Version 2.1.1
*/
(function($) { $.fn.bgIframe = $.fn.bgiframe = function(s) { if ($.browser.msie) { s = $.extend({ top: 'auto', left: 'auto', width: 'auto', height: 'auto', opacity: true, src: 'javascript:false;' }, s || {}); var prop = function(n) { return n && n.constructor == Number ? n + 'px' : n; }, html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="' + s.src + '"' + 'style="display:block;position:absolute;z-index:-1;' + (s.opacity !== false ? 'filter:Alpha(Opacity=\'0\');' : '') + 'top:' + (s.top == 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')' : prop(s.top)) + ';' + 'left:' + (s.left == 'auto' ? 'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')' : prop(s.left)) + ';' + 'width:' + (s.width == 'auto' ? 'expression(this.parentNode.offsetWidth+\'px\')' : prop(s.width)) + ';' + 'height:' + (s.height == 'auto' ? 'expression(this.parentNode.offsetHeight+\'px\')' : prop(s.height)) + ';' + '"/>'; return this.each(function() { if ($('> iframe.bgiframe', this).length == 0) this.insertBefore(document.createElement(html), this.firstChild); }); } return this; }; })(jQuery);