/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}

	
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initializeLB, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);


/*--------- Edit Profile LightBox start-------------*/

var lightbox      = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl,can) {
		
		if (('INPUT' == ctrl.tagName)) 
		{
			 if(!($('autoCheckvalue') == null)){
		     	this.content   = $('autoCheckvalue').rel;
		     }else{
		     	this.content   = "/bmpsweb/authentication/autoLoginMessage.jsp";
		     }
		     this.activate();
			
		}else
		{
			this.content = ctrl.href;
			Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
			ctrl.onclick = function(){return false;};			
		}
	},


		// changeCountry Event triggered 
	changeCountry: function(event){
		
		
		var co = Event.element(event);
		var selectedCountry = co.options[co.selectedIndex].value;
		
		var st = $('mailingState');
		var vals = ((selectedCountry == "2") ? ca.states : us.states);
	
		st.options.length = vals.length + 1;		
		st.options[0].value = 'AAA';
		st.options[0].text = state;
		
		for (var idx = 0; idx < vals.length; idx++)
		{
			st.options[idx+1].value = vals[idx].id;
			st.options[idx+1].text = vals[idx].name;
		}
	

		if(selectedCountry == "3"){
			$('countryPopDiv1').style.display='block';
			$('countryPopDiv2').style.display='block';
			$('otherStatePopDiv').style.display='block';
			$('statePopDiv').style.display='none';
		}else{
			$('countryPopDiv1').style.display='none';
			$('countryPopDiv2').style.display='none';
			$('statePopDiv').style.display='block';
			$('otherStatePopDiv').style.display='none';
		}
				
	},
	
	// changeCountry Event triggered 
	defaultAddress: function(event){		
		
		var addChkBox = Event.element(event);
		alert(defaultmsg);
		addChkBox.checked =true;
	},
	
	//Function to check Phone no Event
	appendPhoneNo: function(event){
		
		var p;
		var p1;
		var sKeyCode = 0;
		var phone = Event.element(event);
		
			try
    		{
		        sKeyCode = window.event.keyCode;
		    }
    		catch(err){}
		   
		    p = phone.value;
		    
		    if(p.length==3)
		    {        
        		pp=p;
		        d4=p.indexOf('(')
        		d5=p.indexOf('-')
		        if(parseInt(sKeyCode) != 8)
		        {        
		            if(d5==-1)
		            {
        		        pp=pp+"-";
            		}
        		}
		       
        		phone.value = "";
        		phone.value = pp;
        		
    		}	
		    if(p.length>3)
		    {
		        d1=p.indexOf('(')
        		d2=p.indexOf('-')
		        if(parseInt(sKeyCode) != 8)
		        {        
		            if (d2==-1)
		            {            
        		        l30=p.length;
		                p30=p.substring(0,2);
        		        p30=p30+"-"
                		p31=p.substring(4,l30);
		                pp=p30+p31;
                		phone.value = "";
        				phone.value = pp; 
            		}
        		}
    		}
		    if(p.length>5)
		    {
		        p11=p.substring(d1+1,d2);
        		if(p11.length>3)
        		{
		            p12=p11;
        		    l12=p12.length;
		            l15=p.length
        		    p13=p11.substring(0,3);
		            p14=p11.substring(3,l12);
        		    p15=p.substring(d2+1,l15);
		            phone.value= "";
        		    pp="("+p13+")"+p14+p15;
		            phone.value=pp; 
            	}
		        l16=p.length;
        		p16=p.substring(d2+1,l16);
		        l17=p16.length;
        		if(l17>3&&p16.indexOf('-')==-1)
        		{
		            p17=p.substring(d2+1,d2+4);
        		    p18=p.substring(d2+4,l16);
		            p19=p.substring(0,d2+1);
        		    pp=p19+p17+"-"+p18;
        		    phone.value= "";
        			phone.value=pp; 
        		}
    		}
				
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		
		$('overlay').style.display = display;
		
		$('lightbox').style.display = display;
		
		if(display != 'none') this.loadInfo();
		
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
			
		var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
	
		
		//info = "<div id='lbContent'>" + response.responseText + "</div>";
		
		info =  response.responseText ;
		
		if(!$('lbContent')){
			$('lightbox').innerHTML		= '<div id="lbContent"></div>';
		}		
		$('lbContent').update(info);
		
	//	new Insertion.Before($('lbInsertBefore'), info)
		
			
		$('lbLoadingBar').style.display = "none";
		$('lightbox').style.display = "block";
		$('lightbox').className = "done";
			
		this.actions();			
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');		

		selects = document.getElementsByClassName('poupupsel');
		
		for(i = 0; i < selects.length; i++) {
				Event.observe(selects[i], 'change', this.changeCountry.bindAsEventListener(this), false);
		}
		
		phoneTextBox = $('phonePopUp');
		if(phoneTextBox)
			Event.observe(phoneTextBox, 'keypress', this.appendPhoneNo.bindAsEventListener(this), false);
		
		defaultAddress = $('editDefaultAddress');
		if(defaultAddress)
			Event.observe(defaultAddress, 'click', this.defaultAddress.bindAsEventListener(this), false);

		for(i = 0; i < lbActions.length; i++) {
				Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
				lbActions[i].onclick = function(){return false;};			
		}
	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	
		linkVar = Event.element(e);
		
		if (!('A' == Event.element(e).tagName)) 
		{
		     linkVar = Event.element(e).parentNode;	
		}
		popupform = document.getElementsByClassName('popUpAddforms')[0];
			
		var formelem = Form.serialize(popupform);
		
	//	Element.remove($('lbContent'));
	
		$('lbContent').update();
	
		//$('lightbox').className = "loading";
		
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
		
		var myAjax = new Ajax.Request(
			 linkVar.href,
			  {method: 'post', parameters: formelem, onComplete: this.processInfo.bindAsEventListener(this)}
	    );	 
	},

	close: function(e){	
		
		linkVar2 = Event.element(e);
		
		if (!('A' == Event.element(e).tagName)) 
		{
		     linkVar2 = Event.element(e).parentNode;	
		}
		if(!(linkVar2 == null))
		{
			if(linkVar2.id == 'autoAgree' || linkVar2.id == 'autoDontAgree')
			{
				var scope   = document.getElementById("scopeOneSpan");			
				var chkbox  = document.getElementById(lookupIdByTagId("autoCheckLB",scope));
				
				if(linkVar2.id == 'autoAgree'){		
					chkbox.checked = true;
				}else{
					chkbox.checked = false;
				}
			}
		}
		Element.remove($('lbContent'));

		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			//this.setScroll(0,0);
			this.prepareIE("auto", '');
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
		$('lbLoadingBar').style.display = "none";
		
		if($('overlay'))
			Element.remove($('overlay'));	
		if($('lightbox'))
			Element.remove($('lightbox'));
		if($('lbLoadingBar'))
			Element.remove($('lbLoadingBar'));	
		addLightboxMarkup();
		
		
	},

	closePopUp: function(response){
		
		Element.remove($('addList'));

		info = "<div id='addList' >" + response.responseText + "</div>";
		
		new Insertion.Before($('addListAfterDiv'), info)

		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			//this.setScroll(0,0);
			this.prepareIE("auto", '');
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
		$('lbLoadingBar').style.display = "none";
				
		initializeEdit();
					
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(e){
		Element.remove($('lbContent'));

		linkVar1 = Event.element(e);
		
		if (!('A' == Event.element(e).tagName)) 
		{
		     linkVar1 = Event.element(e).parentNode;	
		}
		
		//$('lightbox').className = "loading";
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
	
		var myAjax = new Ajax.Request(
			  linkVar1.href,
			  {method: 'post', parameters: "", onComplete: this.closePopUp.bindAsEventListener(this)}
	    );
	}
}


/*--------- Edit Profile LightBox end-------------*/



/*--------- Email to friend AURA  LightBox start-------------*/

var lightboxAuraEmail = Class.create();

lightboxAuraEmail.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.content = ctrl.href;
		//alert("ctrl:  " + ctrl);	
		//alert("content:  " + this.content);	
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},		
	
	// Turn everything on - mainly the IE fixes
	activate: function(){//alert("in activate");
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		
		$('overlay').style.display = display;
		
		$('lightbox').style.display = display;
		
		if(display != 'none') this.loadInfo();
		
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
			
		var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		
		info = "<div id='lbContent'>" + response.responseText + "</div>";
		
		new Insertion.Before($('lbInsertBefore'), info)
			
		if(document.getElementsByClassName('lbActionClose')[0]){
			this.close(this);			
		}else{				
						
			$('lbLoadingBar').style.display = "none";
			$('lightbox').style.display = "block";
			$('lightbox').className = "done";
				
			this.actions();	
		}		
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
				
		lbActions = document.getElementsByClassName('lbAction');
		for(i = 0; i < lbActions.length; i++) {
				Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
				lbActions[i].onclick = function(){return false;};			
		}
	},
	
	// Example of creating your own functionality once lightbox is initiated
	insert: function(e){
	
		linkVar = Event.element(e);
		
		if (!('A' == Event.element(e).tagName)) 
		{
		     linkVar = Event.element(e).parentNode;	
		}
		popupform = document.getElementsByClassName('popUpEmailform')[0];
		
		var formelem = Form.serialize(popupform);
		
		Element.remove($('lbContent'));
		
		//$('lightbox').className = "loading";
		
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
		
		var myAjax = new Ajax.Request(
			 linkVar.href,
			  {method: 'post', parameters: formelem, onComplete: this.processInfo.bindAsEventListener(this)}
	    );	 
	},

	close: function(e){
	
		Element.remove($('lbContent'));

		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			//this.setScroll(0,0);
			this.prepareIE("auto", '');
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
		$('lbLoadingBar').style.display = "none";
		
		if($('overlay'))
			Element.remove($('overlay'));	
		if($('lightbox'))
			Element.remove($('lightbox'));
		if($('lbLoadingBar'))
			Element.remove($('lbLoadingBar'));	
		addLightboxMarkup();
		
		
	},

	closePopUp: function(response){
		
		Element.remove($('addList'));

		info = "<div id='addList' >" + response.responseText + "</div>";
		
		new Insertion.Before($('addListAfterDiv'), info)

		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			//this.setScroll(0,0);
			this.prepareIE("auto", '');
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
		$('lbLoadingBar').style.display = "none";
				
		initializeEdit();
					
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(e){
		Element.remove($('lbContent'));

		linkVar1 = Event.element(e);
		
		if (!('A' == Event.element(e).tagName)) 
		{
		     linkVar1 = Event.element(e).parentNode;
		}		
		//$('lightbox').className = "loading";
		$('lightbox').style.display = "none";
		$('lbLoadingBar').style.display = "block";
	
		var myAjax = new Ajax.Request(
			  linkVar1.href,
			  {method: 'post', parameters: "", onComplete: this.closePopUp.bindAsEventListener(this)}
	    );
	}
}


/*--------- Email to friend AURA LightBox end-------------*/



/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initializeLB(){
	
	if($('overlay'))
		Element.remove($('overlay'));	
	if($('lightbox'))
		Element.remove($('lightbox'));
	if($('lbLoadingBar'))
		Element.remove($('lbLoadingBar'));
	
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
	
	lboxAuraEmail = document.getElementsByClassName('lbOnAuraEmail');
	
	for(i = 0; i < lboxAuraEmail.length; i++) {
		valid = new lightboxAuraEmail(lboxAuraEmail[i]);
	}
	
}

function initializeEdit(){
	
	if($('overlay'))
		Element.remove($('overlay'));	
	if($('lightbox'))
		Element.remove($('lightbox'));
	if($('lbLoadingBar'))
		Element.remove($('lbLoadingBar'));
	
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
			
	for(i = 0; i < lbox.length; i++) {
		if(lbox[i].id == "editAddressPopUp")
		 	valid = new lightbox(lbox[i]);
	}	
}

function initializeCheckBoxLB(ctrl){
	
	if($('overlay'))
		Element.remove($('overlay'));	
	if($('lightbox'))
		Element.remove($('lightbox'));
	if($('lbLoadingBar'))
		Element.remove($('lbLoadingBar'));
	if($('lbContent'))
		Element.remove($('lbContent'));
	
	addLightboxMarkup();
	if(ctrl.checked==true){
		valid = new lightbox(ctrl);
	}	
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {
	bod 				= document.getElementsByTagName('body')[0];
	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';
	lb					= document.createElement('div');
	lb.id				= 'lightbox';
	lb.innerHTML		= '<div id="lbInsertBefore"></div>'
	
	lbProgressBar		= document.createElement('div');
	lbProgressBar.id 	= 'lbLoadingBar';
	lbProgressBar.style.display = 'none';
	
	
	lbProgressBar.innerHTML = '<div id="lbLoadingMsg" style="position: absolute;top:50%;left:40%;z-index:9999;">' +
						  		'<img src="/bmpsweb/framework/skins/flashmenu/images/progressbar_green.gif">' +
						  	  '</div>'
	bod.appendChild(overlay);
	bod.appendChild(lb);
	bod.appendChild(lbProgressBar);
}

function getLbWindowWidth()
{
  var myLbWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myLbWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myLbWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myLbWidth = document.body.clientWidth;
  }	
  return myLbWidth;
}

function getLbWindowHeight()
{
  var myLbHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) 
  {
    //Non-IE
    myLbHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myLbHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myLbHeight = document.body.clientHeight;
  }	
  return myLbHeight;
}

function createLB()
{
	if($('overlay'))
		Element.remove($('overlay'));	
	if($('lightbox'))
		Element.remove($('lightbox'));
	if($('lbLoadingBar'))
		Element.remove($('lbLoadingBar'));
	
	addLightboxMarkup();
}

function openEmailToFriendLB(emailTitle, emailURL, emailLang, emailBannerImagePath)
{	
	createLB();
	
	var decodeEmailTitle      = decodeURIComponent(emailTitle);
	var decodeEmailURL        = decodeURIComponent(emailURL);
	var decodeEmailBannerPath = decodeURIComponent(emailBannerImagePath);
	
	var emailToFriendUrl  = '/bmpsweb/email/emailItemLBForm.do';
		emailToFriendUrl += '?emailTitle=' 			 + encodeURIComponent(decodeEmailTitle);
		emailToFriendUrl += '&emailURL=' 			 + encodeURIComponent(decodeEmailURL);
		emailToFriendUrl += '&emailLang='            + emailLang;
		emailToFriendUrl += '&emailBannerImagePath=' + encodeURIComponent(decodeEmailBannerPath);
		
		
	var a = new Element('a', { href: emailToFriendUrl });
	
	var lbEmail = new lightboxAuraEmail(a);
	lbEmail.activate();
}

function openConfirmPasswordLB(editurl,resetpassurl)
{
	var decodeurl     = decodeURIComponent(editurl);
	var decodeurl2    = decodeURIComponent(resetpassurl);
	
	createLB();
	var confirmPasswordUrl  = '/bmpsweb/bm/userProfileManagement/goToConfirmPassword.do';
		confirmPasswordUrl += '?editURL=' 			 + encodeURIComponent(decodeurl);
		confirmPasswordUrl += '&resetpassURL=' 		 + encodeURIComponent(decodeurl2);	
		
	var a = new Element('a', { href: confirmPasswordUrl });
	
	var lbEmail = new lightboxAuraEmail(a);
	lbEmail.activate();
}

function openLB(contentURL)
{	
	createLB();
	
	var a = new Element('a', { href: contentURL });
	
	var lbEmail = new lightboxAuraEmail(a);
	lbEmail.activate();
}