jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

// get outer XML
jQuery.fn.outer = function() { 
  return $( $('<div></div>').html(this.clone()) ).html(); 
} 

var msgBoxFaded = false;
var msgBoxSourceElement = null;
var msgBoxDestLink = null;
var inSupplierHub = false;
var ribbonCurrentState=-1;
var treeViewCurrentState=-1;

function SubmitLoginRequest(action) {
    var theform = document.forms[0]; 
    var thing = document.getElementById('InvalidPassword');
    if ( (theform.GlobalSite_LoginID.value == '') || 
        (theform.GlobalSite_LoginID.value == 'email or username') || 
        (theform.GlobalSite_Password.value == '') ) { 
            if (thing != null)
                { thing.innerHTML = 'please enter a valid username/password'; } 
            else 
                { alert('please enter a valid username/password'); }
        } 
    else { 
        document.forms[0].action= action; 
        document.forms[0].submit();
        }
    }
    

function CheckForReturnKeyInLoginControl(action) {
	 if (document.all) {
        if (event.keyCode == 13) {
            event.returnValue = false;
            event.cancel = true;
			SubmitLoginRequest(action);
        } 
     }
}

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + '=' + escape(value) +
		((expires) ? '; expires=' + expires.toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
}


function openWindow(url, name, height, width) {
	var options = 'location=0,menubar=0,directories=0,fullscreen=0,resizable=1,scrollbars=1,status=0,toolbar=0,top=65,left=65,width=' + width + 'px,height=' + height + 'px';
	var remote = window.open(url, name, options);
}


function showMoreSuppliers() {
	$('#divSupplierList2').animate({width: '100%'}, 1000);
}

function showLessSuppliers() {
	$('#divSupplierList2').animate({width: '0px'}, 1000);
}

function SubmitPasswordChangeRequest(processPwdChangeURL) {	
    var thing = document.getElementById('pwError');
    if (document.getElementById('chkTermsAndConditions')) {
		if (!document.getElementById('chkTermsAndConditions').checked) {
			var errText = 'You must accept the terms and conditions before proceeding';
			if (thing != null) {
				thing.innerHTML = errText;
				}
			else {
				alert(errText);
				}			
			return false;
		}
	}
    var theform = document.forms[0]; 
    var re = /^[A-Za-z0-9]\w{5,}$/; 
    var text = theform.MyAccount1_Password1.value; 
    if (!re.test(text) ) { 
		errText = 'Your password should contain alphanumeric characters only and be at least 6 characters in length'; 
		if (thing != null) {
			thing.innerHTML = errText;
			}
		else {
			alert(errText);
			}			
		return; 
		}  
    if ( (theform.MyAccount1_Password1.value.length <= 5) || (theform.MyAccount1_Password2.value.length <= 5) ||
        (theform.MyAccount1_Password1.value != theform.MyAccount1_Password2.value) ) 
        { 
		    errText = 'You need to enter the same password (minimum of 6 letters/numbers) twice';    
            if (thing != null)
                { thing.innerHTML = errText; } 
            else 
                { alert(errText); }
        } 
    else { 
        theform.action= processPwdChangeURL; 
        theform.submit();
        }
    }

function FindControlByID(targ) {
		re = new RegExp(':' + targ + '$')
		for (x = 0; x <document.forms.length; x++) {
			for(y = 0; y < document.forms[x].elements.length; y++) {
				elm = document.forms[x].elements[y]			
				if (re.test(elm.name)) {return elm}			
			}			
		}	
	}

function blankbox() {
	for (i=0;i<arguments.length;i++) {
		FindControlByID(arguments[i]).value = ''
		}
	}
	
	
function displaySiteMessageBox(Title, Message, Buttons, fadeBackground, sourceElement,style,width, destLink) {
	if (style != null) { 
		$('#MainSiteMessaging').addClass(style);
	}
	if (width != null) $('#MainSiteMessaging').width(width);
	if (fadeBackground) {
		msgBoxFaded = true;	
		$('#MainSiteMessaging').hide();
		$("DIV#main_page > *").fadeTo("fast", 0.33);
		$("DIV#mainBodyContainer > *").fadeTo("fast", 0.33, NowDisplay_MessageBox(Title, Message, Buttons));
	}
	else {
		//IP this function used to have a modalbox parameter - I removed it as couldn't see it's purpose
		NowDisplay_MessageBox(Title, Message, Buttons);
	}
	if (sourceElement != null) {
		msgBoxSourceElement = sourceElement;
	}
	if (destLink != null) {
		msgBoxDestLink = destLink;
	}
	return false;
}

function NowDisplay_MessageBox(Title, Message, Buttons) {
	
	var titleElm = jQuery('#MainSiteMessaging_Title')[0];
	var msgElm = jQuery('#MainSiteMessaging_Message')[0];
	var buttonElm = jQuery('#MainSiteMessaging_Buttons')[0];	
	titleElm.innerHTML = Title;
	msgElm.innerHTML = Message;
	buttonElm.innerHTML = Buttons;	
	$('#MainSiteMessaging').css('z-index','5000').show();	
}


function cancelMessage(continueClick) {
	if (msgBoxFaded) {
		$("DIV#main_page > *").fadeTo("fast",1.0);
		$("DIV#mainBodyContainer > *").fadeTo("fast",1.0);
	}
	if (msgBoxSourceElement) {
		if (continueClick) {
			document.onclick = null;
			window.onbeforeunload = null; 
			if (msgBoxSourceElement.click) {
				msgBoxSourceElement.click();	
			}
			else {
				window.location.href = msgBoxSourceElement.getAttribute('href');
			}
		}
	}
	if (msgBoxDestLink != null) {
		if (continueClick) {
			window.onbeforeunload = null; 
			window.location.href = msgBoxDestLink;
		}
	}
	$('#MainSiteMessaging').hide();		
}


	
function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function fnTrapKD(e,gblInputButtonToUse) {
	if (window.event) {
		if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
			document.getElementById(gblInputButtonToUse).click();
		} 
	} 
	else {
		if (e.keyCode == 13) {
			e.preventDefault(true);
			e.stopPropagation(true);
			document.getElementById(gblInputButtonToUse).click();
		} 
	} 
}

function replacePortalSensitiveImages(targ) {
	

	if (targ == '') return;

	if (targ == 'coors' || targ == 'coorsdirect') {

		$("a").each(function() {
			if ($(this).text() == 'Ordering') {
				$(this).before('<span style=\'margin-left:15px\'>' + $(this).text() + '</span>').remove();
			}	
		})	
		
	}
		
}

function allow_firefox_users_to_click_hub_links_in_content_pages () {
	jQuery(document).ready(function() {
	 //alert(jQuery.browser.msie);
	 if (jQuery.browser.mozilla || jQuery.browser.opera || jQuery.browser.safari) $('#container_wide').css('position','static')
	})
}


function getCookie(cookieName)
{
if (document.cookie.length>0)
  {
  var posCookieStart=document.cookie.indexOf(cookieName + "=");
  if (posCookieStart!=-1)
    { 
    posCookieStart=posCookieStart + cookieName.length+1; 
    var posEnd=document.cookie.indexOf(";",posCookieStart);
    if (posEnd==-1) posEnd=document.cookie.length;
    return unescape(document.cookie.substring(posCookieStart,posEnd));
    } 
  }
return "";
}

// Left hand navigation 
function ln_ToggleSection(frmObjectId){
	var id = frmObjectId;
	var frmObject = document.getElementById(id);
	
	// find the pattern Ldddd or Cdddd and get that 
	//var myRegExp = /[CL]\d{4,5}/g;
	var myRegExp = /(C|L)\d{4,5}/g;
	var pos = id.search(myRegExp);
	
	
	
	if (pos>=0){
		var menuLayer = document.getElementById(id.substr(pos) + '_p');
		//var currentCSSClassName = $(frmObject).css('class');
		var currentCSSClassName =frmObject.className;
		var newCssImageClass;
		
		//alert("elemis " + id.substr(pos) + '_p' + " pos is " + pos + "menu layer " + menuLayer + " frmObjId="  + frmObjectId + " curr class name=" + currentCSSClassName) ;
		
		// Change the background + and - images		
		if (currentCSSClassName!=null && currentCSSClassName !='undefined'){
				if (currentCSSClassName.indexOf('_imgPlus')>=0){
					newCssImageClass = currentCSSClassName.replace(/_imgPlus/,'_imgMinus');
					frmObject.className = newCssImageClass;
				}
				else{
					newCssImageClass = currentCSSClassName.replace(/_imgMinus/,'_imgPlus');
					frmObject.className = newCssImageClass;
				}
		}
		// toggle the current layer
		$(menuLayer).toggle('fast');
	}
}

function ln_ChangeStyle(divID,action){
/*
	var frmObj = document.getElementById(divID);
	var a = frmObj.getElementsByTagName("a");
	
	if (action=='over'){

		if (a.length==1){
			a[0].className='ln_L2_Link_MouseOver';
			var newCssImageClass = frmObj.className.replace(/_Fl/,'_Fl_MouseOver');
			frmObj.className = newCssImageClass;
			
		}
	}
	else{
		if (a.length==1){
			a[0].className='ln_L2_Link';
			var newCssImageClass = frmObj.className.replace(/_Fl_MouseOver/,'_Fl');
			frmObj.className = newCssImageClass;
		}
	}

*/

}



// End of Left hand navigation

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function showHoverMessage(hoverItem, title, msg) {
	var posLeft = ($('#' + hoverItem).offset().left - 200) + 'px';
	var posTop = ($('#' + hoverItem).offset().top + 25) + 'px';			
	$('#MainSiteMessaging').css({'left': posLeft, 'top': posTop});
	displaySiteMessageBox(title, msg, '', false);
}