// JavaScript Document - Global functions


//preload images
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

var swap_text_boxes = [];
function init_swap_text_boxes(){
  $('input[type=text][value].bmg_swaptextbox').each(function() {
    swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
  });
  $('input[type=text][value].bmg_swaptextbox').bind('focus', function() {
    if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
      $(this).val('');
  	  $(this).select(); 
	  if ($(this).hasClass("bmg_swap_color")){
	  	 $(this).addClass("bmg_swaptextboxMod");
	  }	
    }
  });
  $('input[type=text][value].bmg_swaptextbox').bind('blur', function() {
    if($(this).val() == '') {
      $(this).val(swap_text_boxes[$(this).attr('id')]);
	if ($(this).hasClass("bmg_swap_color")){
	  	 $(this).removeClass("bmg_swaptextboxMod");
	  }	  	  
    }
  });
}

var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};
UTIL.popup.open = function (href, params)
{
   var defaultParams = {
       "width":       "600",   
       "height":      "520",   
       "top":         "0",     
       "left":        "0",     
       "directories": "no",   
       "location":    "no",    
       "resizeable":  "yes",   
       "menubar":     "no",    
       "toolbar":     "no",    
       "scrollbars":  "yes",   
       "status":      "no"     
   };
    var windowName = params["windowName"] || "new_window";
    var i, useParams = "";
   for (i in defaultParams)
   {
       useParams += (useParams === "") ? "" : ",";
       useParams += i + "=";
       useParams += params[i] || defaultParams[i];
   }
    return window.open(href, windowName, useParams);
};

$(document).ready(function(){
	$.preloadImages("/qcms/qCmsRepository/Global/largeBusiness/images/loading-circle.gif","/qcms/qCmsRepository/Global/largeBusiness/images/loading-image-bg.gif");						   
  	$("a.bmg_popup").each(function (i){
       $(this).click(function(event) {
           event.preventDefault();
           var params = $(this).data("bmg_popup") || {};            
           if ($(this).attr("target"))
           {
               params.windowName = $(this).attr("target");
           }
           var windowObject = UTIL.popup.open(this.href, params);
           $(this).data("windowObject", windowObject);
       });
   });
			$.fn.alphanumeric = function(pq) { 
		pq = $.extend({
			ichars: "!@#$%^&*()_-+=[]\\\';,/{}|\":<>?~`. ",
			nchars: "",
			allow: ""
		  }, pq);	
		return this.each
			(
				function() 
				{
					if (pq.nocaps) pq.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (pq.allcaps) pq.nchars += "abcdefghijklmnopqrstuvwxyz";					
					sq = pq.allow.split('');
					for ( i=0;i<sq.length;i++) if (pq.ichars.indexOf(sq[i]) != -1) sq[i] = "\\" + sq[i];
					pq.allow = sq.join('|');		
					var reg = new RegExp(pq.allow,'gi');
					var ch = pq.ichars + pq.nchars;
					ch = ch.replace(reg,'');
					 $(this).unbind("keypress");
					$(this).keypress
						(
							function (e)
								{
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();	
								}						
						);
					$(this).bind('contextmenu',function () {return false});				
				}
			);
	};
	$.fn.numeric = function(pq) {
	
		var atoz = "abcdefghijklmnopqrstuvwxyz";
		atoz += atoz.toUpperCase();

		pq = $.extend({
			nchars: atoz
		  }, pq);
		return this.each (function()
			{
				$(this).alphanumeric(pq);
			}
		);
	};
	$.fn.alpha = function(pq) {
		var nm = "1234567890";
		pq = $.extend({
			nchars: nm
		  }, pq);
		return this.each (function()
			{
				$(this).alphanumeric(pq);
			}
		);
			
	}; 
	
try { 
	document.execCommand('BackgroundImageCache', false, true);
} 
catch(e) {}
	
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		$('.ieFrame').bgiframe({ opacity: true, top: '-4px',  right: '-1em' });
	}


/*function preloadImages(images) {
	var i,newImage;
	for (i=0;i<images.length;i++){
	newImage = new Image();
        newImage.src = images[i];
}}
preloadImages([    
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBgSides_hover.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBg.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBgSides.gif',	
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_menuBg_hover.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_button_arrow_icon_wt.gif',
	'/qcms/qCmsRepository/Global/largeBusiness/images/menu/bmg_button_background_wt.gif'	
]);
*/
	init_swap_text_boxes();				   

	$("body").click(function(){
	$("div.bmg_subMenu").slideUp("fast");
});
	$("div.bmg_itemMenu a").focus(function(){
	$(this).parent("div").find("div.bmg_subMenu").slideDown("fast");
	$(this).parent("div").siblings().find("div.bmg_subMenu").slideUp("fast");
});
	$(".bmg_nav_search_icon").focus(function(){
	$("div.bmg_subMenu").slideUp("fast");

});
	$("div.bmg_itemMenu").hoverIntent(function () {
//	$(this).children().addClass("bmg_showHide").slideDown("fast");
    $(this).children().slideDown("fast");
	
	if ($.browser.mozilla) {
	$(".bmg_car_button_all").hide();
	}
}, 
	function () {
//	$(this).children("div").removeClass("bmg_showHide").slideUp("fast"); 
	$(this).children("div").slideUp("fast");
	if ($.browser.mozilla) {
	$(".bmg_car_button_all").show();
	}
});
$("ul.bmg_main_menu ul li").mouseover(function(){
	$(this).addClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover");
});
$("ul.bmg_main_menu ul li.last").mouseover(function(){
	$(this).addClass("bmg_menu_hover_last");
	$(this).removeClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover_last");
});
$("ul.bmg_main_menu ul li").mouseover(function(){
	$(this).addClass("bmg_menu_hover");
}).mouseout(function(){
	$(this).removeClass("bmg_menu_hover");
});

try {
	if (productsSelected) {
		$("#bmg_products").addClass("bmg_itemMenuActive");
		$("#bmg_resourceCenter").removeClass("bmg_itemMenuActive"); 
	} else if (resourceSelected) {
		$("#bmg_resourceCenter").addClass("bmg_itemMenuActive");
		$("#bmg_products").removeClass("bmg_itemMenuActive");
	}
	else {
		$(".bmg_topMenuAll").children().removeClass("bmg_itemMenuActive"); 
	}
}
catch (err) {
	log.error("menuSelection",""+err);
}

}); 
/*function stopError(errMsg, errScript, errLine){  
	try{
		log.error("GlobalError"+errMsg,"Global Error: " + errMsg +"\n > script: " + errScript + "\n > line: " + errLine);
	} catch(e){}  
	return true;  
}   
window.onerror = stopError; */
/* Google Website Optimizer starts - Oct14, 2009 */

function gotoContactUs(){
	/*
	<a href="/business/contact-us.html" 
		onclick="gotoContactUs(); return false">
	*/
	var gwoweight = 50; var rand=0;
		log.info("gotoContactUs","(bmg_global)selChannel: "+selChannel,5)	
	
	if(selChannel!="" && selChannel=="Large Business"){
		location.href=  "/business/contact-us.html";
		/*var randNumCookie=$.cookie('randomNumber');
		log.info("gotoContactUs","(bmg_global)randNumCookie: "+randNumCookie,5)
		if(randNumCookie==null){
			rand = Math.round(Math.random()*100);
			log.info("gotoContactUs","(bmg_global)Math.random Number generated: "+rand,5);
			//alert("non-flash pages test, gotoContactUs() function called from bmg_global.js, random number just generated: "+rand);
		 	bmg_contactUsFlow_cookieSave(rand);
		}
		else{
			rand=randNumCookie; //alert("non-flash pages test, gotoContactUs() function called from bmg_global.js, random number: "+rand);
		}
		if(rand < gwoweight){
				log.info("gotoContactUs","(bmg_global)/business/contact-us-v1b.html",5);
			location.href= "/business/contact-us-v1b.html";
		} 
		else{log.info("gotoContactUs","(bmg_global)/business/contact-us.html",5);
			location.href=  "/business/contact-us.html"; //URL
		}*/
	}
    // Go to standard/default contact page.
	else{log.info("gotoContactUs","(bmg_global)/business/contact-us.html",5);
		location.href=  "/business/contact-us.html"; //URL
	}
	
	
}
/* Google Website Optimizer ends - Oct14, 2009 */

/* Random number saving in Cookie */
/*function bmg_contactUsFlow_cookieSave(rand){
	var randomNumber=rand;
	$.cookie('randomNumber', randomNumber,{path:'/business/'});
	var temp = $.cookie('randomNumber');
	//log.info("bmg_contactUsFlow_cookieSave","(bmg_global)Cookie info saved(randomNumber): "+temp,5)
	
}

*/