﻿// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

/* begin - Primary Navigation */
var primaryNavigation = {
  init: function() {
  	var c = this, n = $('#NavigationPrimary');
    if ($.browser.msie) // iframe to fix dropdowns over select items
        $('> li', n).append('<iframe style="display: none;" src="javascript:false;" frameBorder="0" scrolling="no"></iframe>');
	
	$('> li', n).each(function() { c.markRelationships(this); });
    $('li', n).hover(
      function() {
        $(this).addClass('over').children('a').addClass('over');
  	    if ($.browser.msie) {
  				var h = $(this).children('ul').height();
  				var w = $(this).children('ul').width();
  				var i = $(this).children('iframe');
  				if (i.length > 0 && h != null && w != null)
  				  i.height(h).width(w).show();
		    }
      },
      function() {
        $(this).removeClass('over').children('a').removeClass('over');
        if ($.browser.msie)
          $(this).children('iframe').hide();
      }
    );
  },
  markRelationships: function(li) {
  	  var c = this;
  	  if ($('> ul > li > ul', li).size() > 0)
  	  	  $(li).addClass('grandparent').find('> a').addClass('grandparent');
  	  if ($('> ul', li).size() > 0)
  	  	  $(li).addClass('parent selfClear').find('> a').addClass('parent').end().find('> ul > li').each(function() { c.markRelationships(this); });
  }
};
$(function() { primaryNavigation.init(); });
/* end - Primary Navigation */

/* begin - Case Studies Accelerator */
var CaseStudiesAccelerator = {
  termCookie: 'openTerms',
  termSeparator: '||',
  expireDate: 365 * 3,
  init: function() {
    var t = this, c = $('#CaseStudiesAccelerator');
    $('dd', c).hide();
    $('dt em', c).before('<a href="#" class="expand">Expand</a><a href="#" class="collapse" style="display: none;">Collapse</a>');
    $('a.expand', c).click( t.expand );
    $('a.collapse', c).click( t.collapse );
    var terms = t.terms();
    for (var i=0; i<terms.length; i++)
	{
		var d = $('#' + terms[i]);
		/*old code
		$('a.expand', d).hide().next().show().parent().next().show();
		*/
		$('a.expand', d).hide().next().show().parent().addClass('expanded').next().show();
	}
  },
  expand: function() {
    $(this).hide().next().show().parent().addClass('expanded').next().addClass('expanded').slideDown();
    CaseStudiesAccelerator.addTerm($(this).parent().attr('id'));
    return false;
  },
  collapse: function() {
    $(this).hide().prev().show().parent().removeClass('expanded').next().removeClass('expanded').slideUp();
    CaseStudiesAccelerator.removeTerm($(this).parent().attr('id'));
    return false;
  },
  getCookie: function() {
  	//var cookie =  '';
  	//if (cookie == null)
  		return '';
  	//return cookie;
  },
  terms: function() {
  	var cookieVal = this.getCookie();
  	if (cookieVal == '')
  		return new Array();
  	return cookieVal.split(this.termSeparator);
  },
  setCookie: function(terms) {
  	var cookieValue = terms.join(this.termSeparator);
  	return $.cookie(this.termCookie, cookieValue, { expires: this.expireDate, path: '/' });
  },
  addTerm: function(term) {
	var terms = this.terms();
	var termIndex = this.termIndex(terms, term);
	if (termIndex == null)
	{
		terms.push(term);
		this.setCookie(terms);
	}
  	
  },
  removeTerm: function(term) {
  	  var terms = this.terms();
  	  var termIndex = this.termIndex(terms, term);
  	  if (termIndex == null)
  	  	return;
  	  terms.splice(termIndex, 1);
  	  this.setCookie(terms);
  },
  termIndex: function(terms, term) {
	for (var i=0; i<terms.length; i++)
	{
		if (terms[i] == term)
			return i;
	}
	return null;
  }
};
$(function() { CaseStudiesAccelerator.init(); });
/* end - Case Studies Accelerator */


/* begin - appliance flyout */

		var Appliances = {
		  mouseover: function() {
				$(this).children().addClass('over');
		    $(this).children().next().show();
				$(this).children().next().next().show();
		  },
		  mouseout: function() {
				$(this).children().removeClass('over');
				$(this).children().next().hide().next().hide();
				$(this).children().next().next().hide();
		  },
		  init: function() {
				$('div.flyout').hide();
				$('div.flyoutFooter').hide();
		    $('#Appliances > li').hover(this.mouseover, this.mouseout);
		  }
		}
		$(function(){ Appliances.init(); });

/* end -  appliance flyout */

/* begin - Drop Shadow code to fix IE 5.5 and IE 6 issues */
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	document.writeln('<style type="text/css">img { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			var src = img.src;
			var div = document.createElement("DIV");
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			div.className = 'wasPNGImage';
			img.replaceNode(div);
		}
		img.style.visibility = "visible";
	}
}
/* end - Drop Shadow code to fix IE 5.5 and IE 6 issues */

/* begin - Form support */

// Function to trim the whitespace from field values
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function GetParam(name){
	var start=location.search.indexOf('?'+name+'=');
	if (start<0) start=location.search.indexOf('&'+name+'=');
	if (start<0) return '';
	start += name.length+2;
	var end=location.search.indexOf('&',start)-1;
	if (end<0)end=location.search.length;
	var result=location.search.substring(start,end);
	var result='';
	for(var i=start;i<=end;i++) {
		var c=location.search.charAt(i);
		result=result+(c=='+'?' ':c);
	}
	return unescape(result);
}

function IsNumeric() {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < document.webcap.employees.value.length && IsNumber == true; i++) { 
		Char = document.webcap.employees.value.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

// Function to validate required fields as well as special validation for email fields
function checkForm()
{
	// Find input textboxes that are required
	var error = 0;
	$('label').removeClass('form-error');
	$('input.required').each(
		function() {  
			if (trim(this.value) == '') {
				$(this).prev().addClass('form-error');
				error++; 
			}
		}
	);
	// Find select pulldowns that are required
	$('select.required').each(
		function() {  
			if (this.value == "") {
				$(this).prev().addClass('form-error');
				error++; 
			}
		}
	);
	// Find a div that contains required checkboxes
	$('label[span.form-required] + div.form-checkboxes').each(
	  function() {
	    // 'this' is the div
	    var checks = 0;     
	
	    // Loop over all the checkboxes
	    $('input', $(this)).each(
	      function() {
	        // 'this' is the checkbox
	        if (this.checked == true)
	          checks++;
	      }
	    );
	    if (checks == 0) {
	      $(this).prev().addClass('form-error'); 
				error++;
			}
	  }
	);
	// Find a div that contains required radio buttons
	$('label[span.form-required] + div.form-radios').each(
	  function() {
	    // 'this' is the div
	    var checks = 0;     
	
	    // Loop over all the radio buttons
	    $('input', $(this)).each(
	      function() {
	        // 'this' is the radio button
	        if (this.checked == true)
	          checks++;
	      }
	    );
	    if (checks == 0) {
	      $(this).prev().addClass('form-error'); 
				error++;
			}
	  }
	);
	// Validate email fields
	$('div#webform-component-email input').each(
		function() {
			if (! (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(this.value))) {
				$(this).prev().addClass('form-error');
				error++;
			}
		}
	);
	
	if (error) { return false; }
	return true;
}
$(function() { $('#webform-client-form').bind('submit', function(event) { return checkForm(); }); });

	//THIS FUNCTION REMOVES THE YELLOW BACKGROUND COLOR FROM TEXT INPUTS THAT ARE MARKED AS AUTO-FILL (IE/WINDOWS)
  	function setListeners(){
  	  inputList = document.getElementsByTagName("INPUT");
  	  for(i=0;i<inputList.length;i++){
  	    inputList[i].attachEvent("onpropertychange",restoreStyles);
  	    inputList[i].style.backgroundColor = "";
  	  }
  	  selectList = document.getElementsByTagName("SELECT");
	    for(i=0;i<selectList.length;i++){
      	selectList[i].attachEvent("onpropertychange",restoreStyles);
    	  selectList[i].style.backgroundColor = "";
   	 }
  	}

  	function restoreStyles(){
    	if(event.srcElement.style.backgroundColor != "")
     	 event.srcElement.style.backgroundColor = "";
 	 }
	//-->

/* end - Form support */

/* Homepage promos */
var HomepagePromos = {

	init: function() {
		$('body.homepage ul.promos li').click(HomepagePromos.click);
	},
	click: function(e) {
		var handled = false;
		$(this).find('a.clickBound').each( function() { $(this).click(); handled = true; } );
		if (! handled) {
			var url = '';
			$(this).find('a').each( function() { url = this.getAttribute("href"); } );
			window.location.href = url;
		}
	}
};
$(function() { HomepagePromos.init(); });
/* end - Homepage promos */

/* Partnerpage promos */
var PartnerpagePromos = {

	init: function() {
		$('body.partnerpage ul.promos li').click(PartnerpagePromos.click);
	},
	click: function(e) {
		var handled = false;
		$(this).find('a.clickBound').each( function() { $(this).click(); handled = true; } );
		if (! handled) {
			var url = '';
			$(this).find('a').each( function() { url = this.getAttribute("href"); } );
			window.location.href = url;
		}
	}
};
$(function() { PartnerpagePromos.init(); });
/* end - Homepage promos */

var CaseStudiesTabs = {
	cookieName: 'caseStudyTab',
	expireDate: 365 * 3,
	activeTab: function(a, aDiv, hideDiv) {
		var t = CaseStudiesTabs;
		var href = $(a).attr('href');
		$.cookie(t.cookieName, href, { expires: t.expireDate, path: '/' });
		return true;
	},
	init: function() {
		//var href = $.cookie(this.cookieName);
		//if (href == null)
			//return;
		//var c = $('#CaseStudiesAccelerator');
		//$('ul.navigation a', c).each( function(i) {
			//if ($(this).attr('href') == href)
			//{
				//c.triggerTab(i + 1);
			//	return false;
			//}
		//} );
	}
};

$(function() {
  $('#FeaturedLinks').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  $('#CaseStudiesAccelerator').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected', onClick: CaseStudiesTabs.activeTab } );
  $('#ContentChannel div.tabbedContent').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  $('body.integrationSolutions #ContentChannel').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  $('body.integrationSolutions #ContentChannel a.toOverview').click( function() { $('#ContentChannel').triggerTab(1); } );
  $('body.integrationSolutions #ContentChannel a.toBenefits').click( function() { $('#ContentChannel').triggerTab(2); } );
  $('body.integrationSolutions #ContentChannel a.toProblemsSolved').click( function() { $('#ContentChannel').triggerTab(3); } );
  $('body.integrationSolutions #ContentChannel a.toHowItWorks').click( function() { $('#ContentChannel').triggerTab(4); } );
  $('body.integrationSolutions #ContentChannel a.toComponents').click( function() { $('#ContentChannel').triggerTab(5); } );
  $('body.integrationSolutions #ContentChannel a.toGettingStarted').click( function() { $('#ContentChannel').triggerTab(6); } );

	// $('#ProblemSnapshot a').popupwindow(); // Make the problem snapshot open in a new window		
	// $('#SolutionSnapshot a').popupwindow(); // Make the problem snapshot open in a new window		
	// $('#Footer a.siteCredits').popupwindow(); // Make the problem snapshot open in a new window		
	// $('#WMC a').popupwindow(); //Make the Web Management Console component screenshots open in a new window 	
	
	$('div.contentCustomerLogoText img').lazyload({ threshold : 150 }); // Lazyload images on the customer page	
});

// Fixes for IE6 which seems to have trouble with activating on DOM ready.
window.onload = function() {  
  if (! $('#NewsLinks').is('.tabs-container'))
    $('#FeaturedLinks').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  if (! $('#ViewCaseStudyBusinessProblem').is('.tabs-container'))
	$('#CaseStudiesAccelerator').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  if (! $('body.integrationSolutions #ContentChannel li.first').is('.tabs-container'))
	$('body.integrationSolutions #ContentChannel').tabs(1, {navClass: 'navigation', selectedClass: 'tabSelected'} );
  CaseStudiesTabs.init();
};

function delayer(){
    window.location = "index.html";
    }
