// JavaScript Document
/*

This file creates a sign-in modal that would appear with the page content remaining under a gray overlay. This sign-in experience 
keeps the user on the page of interest while overlaying the complete sign-in process.  This modal also provides customizable 
content for the header and right-half of the modal.

The Sign in service requires either live Cart servers or the folowing host entry: 10.1.167.130  cart2.barnesandnoble.com   # DNYOPDEMO03.

This was inspried by the original lightbox code was created by : Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

And by leightbox, Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

*/


	var imagehost_url = "http://images.barnesandnoble.com";
	var imagehost_css_path = '/presources/css/';
	var signin_test_login = "";
	var signin_test_login_pword = "";
	
	

	var testing_signin = false;

	
	var sign_in_source_ready = false;
	var is_modal_signed_in = false;
	
	var signin_original_scroll_X = 0;
	var signin_original_scroll_Y = 0;
	
	var signin_script_domain = "https://cart2.barnesandnoble.com";
	var signin_script_path = "/services/service.asp";
	var signin_script_qs = "?stage=signIn&UIaction=signIn&outFormat=1&callbackfunc=ConfirmSignIn";
	var sign_in_destination = "";
	var sign_in_key = "default";
	
	var signin_script_URL = "";	
	
	var Signin_Headlines = new Object();
	var Signin_Right_Widget = new Object ();
	
	
	// This populates html that will be presented in the actual sign in box
	
	var signin_css_source = + '#signin_header_container h1 {'
		+ 'font-family: Arial, Helvetica, sans-serif;'
		+ 'font-size: 20px;'
		+ 'font-weight: normal;'
		+ 'color:#999999;'
		+ 'padding: 0 0 0 14px;'
		+ 'margin: 0;'
		+ '}'
		+ '#signin_content_code h2{'
		+ 'padding-top: 10px;'
		+ '/*color: #3e3b2c;*/'
		+ 'color: #827c76;'
		+ 'font-weight: bold;'
		+ '}'
		+ '.signin_error_obj {'
		+ 'background-color: #F1F1F1;'
		+ 'color: #CC0000;'
		+ 'padding: 8px;'
		+ 'margin: 5px 0 5px 0;'
		+ '}'
		+ ' '
		+ '#signin_right_widget_obj h2{'
		+ 'margin-bottom: 30px;'
		+ '}'
		+ ' '
		+ '#signin_right_widget_obj {'
		+ 'float: right; '
		+ 'width: 365px;'
		+ 'padding-left: 25px;'
		+ 'background-image:url(http://images.barnesandnoble.com/presources/images/grey_dot.gif);'
		+ 'background-repeat:repeat-y;'
		+ '}'
		+ '.signin_ulist li {'
		+ 'margin: 4px;'
		+ ' '
		+ '}'
		+ '#signin_header_container {'
		+ 'background:url(http://images.barnesandnoble.com/presources/images/signin_header_background.jpg);'
		+ 'background-repeat:repeat-x;'
		+ 'background-color: #e5e5e3;'
		+ 'padding: 8px;'
		+ '}'
		+ ' '
		+ '.bulletDots {'
		+ 'background-image: url(http://images.barnesandnoble.com/presources/images/UL_signin_dots.gif);'
		+ 'background-position: 2px 6px;'
		+ 'background-repeat: no-repeat;'
		+ 'color: #333333;'
		+ 'font-family: verdana;'
		+ 'font-size: 11px;'
		+ 'line-height: 15px;'
		+ 'padding: 0px 0px 0px 24px;'
		+ '}'
		+ ' '
		+ '#signin_anchor { '
		+ 'display: none;'
		+ '}'
		+ ' '
		+ '.leightbox {'
		+ 'color: #333;'
		+ 'display: none;'
		+ 'position: absolute;'
		+ 'top: 25%;'
		+ 'left: 20%;'
		+ 'width: 740px;'
		+ ' '
		+ 'border: 2px solid #90c086;'
		+ 'background-color: white;'
		+ 'text-align: left;'
		+ 'z-index:1001;'
		+ 'overflow: auto;'
		+ '}'
		+ ' '
		+ '.leightbox_FF {'
		+ 'color: #333;'
		+ 'display: none;'
		+ 'position: fixed;'
		+ 'top: 150px;'
		+ 'left: 250px;'
		+ 'width: 740px;'
		+ 'border: 2px solid #90c086;'
		+ 'background-color: white;'
		+ 'text-align: left;'
		+ 'z-index:1001;'
		+ 'overflow: auto;'
		+ '}'
		+ ' '
		+ '#overlay_signin{'
		+ 'display:none;'
		+ 'position:absolute;'
		+ 'top:0pt;'
		+ 'left:0pt;'
		+ 'width:100%;'
		+ 'height: 100%;'
		+ 'z-index:1000;'
		+ 'background-color:#FFFFFF;'
		+ '-moz-opacity: 0.4;'
		+ 'opacity: 0.4;'
		+ 'filter: alpha(opacity=40);'
		+ '}'
		+ ' '
		+ '#signin_widget_container {'
		+ ' '
		+ 'float: left; '
		+ 'width:300px; '
		+ 'height: 220px; '
		+ 'padding-left: 15px;'
		+ ' '
		+ '}'
		+ ' '
		+ '#overlay_part_1, #overlay_part_2, #overlay_part_3, #overlay_part_4 {'
		+ 'overflow: auto;'
		+ 'position: fixed;'
		+ ' '
		+ 'display:none;'
		+ ' '
		+ 'background-color:#FFFFFF;'
		+ '-moz-opacity: 0.4;'
		+ 'opacity:.40;'
		+ 'filter: alpha(opacity=40);'
		+ '}'
		+ ' '
		+ '#overlay_part_1{'
		+ ' '
		+ 'top: 0;'
		+ 'left:0;'
		+ 'width:100%;'
		+ 'height:187px;'
		+ ' '
		+ '}'
		+ '#overlay_part_2{'
		+ ' '
		+ 'top: 187px;'
		+ 'left:0;'
		+ 'width:270px;'
		+ 'height:250px;'
		+ ' '
		+ '}'
		+ '#overlay_part_3{'
		+ ' '
		+ 'top: 187px;'
		+ 'left:980px;'
		+ 'width:370px;'
		+ 'height:250px;'
		+ ' '
		+ '}'
		+ '#overlay_part_4{'
		+ 'top: 437px;'
		+ 'left:0;'
		+ 'width:100%;'
		+ 'height:100%;'
		+ '}'
		+ ' '
		+ '.leightbox_FF #signin_email_address{'
		+ 'z-index: 1002;'
		+ '}'
		+ ' '
		+ ' '
		+ '.lightbox[id]{ '
		+ '/* IE6 and below Can not See This */    '
		+ 'position:fixed;    '
		+ '}'
		+ ' '
		+ '#overlay[id]{ '
		+ '/* IE6 and below Can not See This */    '
		+ 'position:fixed;    '
		+ '}';

	
	var signin_html_source = '<div id="signin_content_code">'
+ ' <div id="signin_header_container">'
+ '		<div style="float: right">'
+ '			<input type="image" src="http://images.barnesandnoble.com/presources/images/signin_close_button.gif" hight="15" width="14" onClick="return Close_Sign_In();"/>'
+ '		</div>'
+ '		<h1 id="signin_headline_obj">'
+ '				Sign In to Barnes&Noble.com'
+ '			</h1>'
+ '	</div>'
+ '	<div style="margin-top: 10px; margin-left: 10px; margin-right: 10px;">'
+ '	'
+ '		<div id="signin_error_container">'
+ '		</div> '
+ '	'
+ '			<div id="signin_widget_container">'
+ '				<form action="" method="post" onsubmit="return  Run_Sign_In ();">'
+ '					<div style="height: 180px; text-align: left;">'
+ '						<h2 style="height: 40px;">'
+ '							Returning Customers'
+ '						</h2>'
+ '	'
+ '						<div style="float:left; width: 110px; height: 50px;">'
+ '							Email Address:'
+ '						</div>'
+ '						<div>'
+ '							<input type="text"  name="emailAddress" id="signin_email_address" value="' + signin_test_login + '" style="width: 170px;"/>'
+ '							'
+ '							<a href="https://cart2.barnesandnoble.com/account/op.asp?step=updateAccEmail">Update email address</a>'
+ '						</div>'
+ '						<br/>'
+ '						<br/>'
+ '						<div style="float: left; width: 110px; height: 40px;  ">'
+ '							Password:<br/>'
+ '							(case sensitive) '
+ '						</div>'
+ '						<div>'
+ '							<input  type="password" name="acctPassword" id="signin_acct_password" autocomplete="OFF" value="' + signin_test_login_pword + '"  style="width: 170px;"/>'
+ '							'
+ '							<a href="https://cart2.barnesandnoble.com/account/op.asp?step=forgotPassword">Forgot password?</a>'
+ '						</div>'
+ '					</div>'	
+ '					<div style="text-align:right; padding-right: 20px;">'
+ '						<input type="image" src="http://images.barnesandnoble.com/presources/images/signin_button.gif" height="27" width="85" onclick="" /> '
+ '					</div>'
+ '				</form>'

+ '			</div>'
+ '			'
+ '			<div id="signin_right_widget_obj">'
+ '				<div style="height: 180px; text-align: left;">'
+ '					<h2>'
+ '						Sign up now and enjoy these benefits:'
+ '					</h2>'
+ '					<div class="bulletDots">'
+ '						Access your order history'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Check out faster'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Store multiple credit cards on file'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Save multiple shipping locations in your personal address book'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Save items to a Gift &amp; Wish Lists'
+ '					</div>'
+ '				</div>'		
+ '				<div style="text-align:right; padding-right: 20px;">'	
+ '						<input  onclick="window.location=\'https://cart2.barnesandnoble.com/account/op.asp?step=createAccount\'"'
+ '							type="image" src="http://images.barnesandnoble.com/presources/images/create_acct_button_long.gif" height="28" width="147" onclick="" />'
+ '				</div>'
+ '			</div>'
+ '	</div>'
+ '	</div>'
+ '	<br/>'
+ '	'
+ '	<div style="clear:both; height: 15px;">'
+ '	'
+ '	</div>';

	if (false && testing_signin) {
		signin_html_source += '		<div id="signin_notice" style="clear:both; color:#660000;">'
			+ '		'
			+ '		</div>';
			
	}


	
	Signin_Headlines.WriteAReview = "To write a review you must log in or create an account";
	Signin_Right_Widget.WriteAReview =  '<div style="height: 180px; text-align: left;">'
+ '					<h2>'
+ '						Sign up now and enjoy these benefits:'
+ '					</h2>'
+ '					<div class="bulletDots">'
+ '						Access your order history'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Check out faster'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Store multiple credit cards on file'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Save multiple shipping locations in your personal address book'
+ '					</div>'
+ '					<div class="bulletDots">'
+ '						Save items to a Gift &amp; Wish Lists'
+ '					</div>'
+ '				</div>'		
+ '				<div style="text-align:right; padding-right: 20px;">'	
+ '						<input  onclick="window.location=\'https://cart2.barnesandnoble.com/account/op.asp?step=createAccount\'"'
+ '							type="image" src="http://images.barnesandnoble.com/presources/images/create_acct_button_long.gif" height="28" width="147" onclick="" />'
+ '				</div>';

	
	function Test_Out (comment) {
		if (testing_signin){
			document.getElementById('signin_notice').innerHTML += comment + "<br/>";
		}
	}
	
	
	function Ready_SignIn_CSS () {
		
		if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) { // If this is internet explorer
			document.createStyleSheet (signin_css_source);			
		}
		else {
			
			
			/////// PRE-LOADING CSS NOT WORK YET....
			
			var lightboxCSS = document.createElement("style");
			lightboxCSS.setAttribute("type", "text/css");
			(document.getElementsByTagName("head")[0]).appendChild(lightboxCSS);
			var lightboxCSS_content = document.createTextNode(signin_css_source)
			var style_obj = (document.getElementsByTagName("style")[document.getElementsByTagName("style").length - 1]);
			style_obj.innerText = (signin_css_source);
		}

		/*var lightboxCSS = document.createElement("link");
			lightboxCSS.setAttribute("rel", "stylesheet");
			lightboxCSS.setAttribute("type", "text/css");
			lightboxCSS.setAttribute("href", imagehost_url + imagehost_css_path + 'signin_modal_screen.css');
			document.getElementsByTagName("head")[0].appendChild(lightboxCSS);
			
			
		*/
		
		

		return false;
	}
	
	
	function Create_Sign_In_Source () {

		// The following was written by Biagetti to add the needed HTML to the parent page to facilitate lightbox signin 

		if (! sign_in_source_ready ) {
			
			Ready_SignIn_CSS ();
			
			var lightboxCSS = document.createElement("link");
			lightboxCSS.setAttribute("rel", "stylesheet");
			lightboxCSS.setAttribute("type", "text/css");
			lightboxCSS.setAttribute("href", imagehost_url + imagehost_css_path + 'signin_modal_screen.css');
			document.getElementsByTagName("head")[0].appendChild(lightboxCSS);
			
			// create the hidden div element that will hosue the sign-in html	
			lightboxObj	= document.createElement('div');
			lightboxObj.setAttribute('id','lightbox1');
			lightboxObj.setAttribute('class','lbOn'); 
	
				
			if (testing_signin) document.getElementById('signin_notice').innerHTML=('Creating code...');
		
	
		
			// create a div tag to server as an anchor at the bottom of the page (IE needs this to extend the overlay across the entire page content ) 
			end_anchor = document.createElement('div');
			end_anchor.setAttribute('id','leightbox_end_anchor');
	
			// add the new elements to the body
			theBody	= document.getElementsByTagName('body')[0];
			theBody.appendChild(lightboxObj);
			theBody.appendChild(end_anchor);

			if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
				
				document.getElementById('lightbox1').className = ('leightbox_FF');

			}
			else {
				document.getElementById('lightbox1').className = ('leightbox');
			}
			
				theBody	= document.getElementsByTagName('body')[0];
		
				overlay 			= document.createElement('div');
				overlay_1 			= document.createElement('div');
				overlay_2 			= document.createElement('div');
				overlay_3 			= document.createElement('div');
				overlay_4 			= document.createElement('div');
			
				overlay.id				= 'overlay_signin';
				overlay_1.id			= 'overlay_part_1';
				overlay_2.id			= 'overlay_part_2';
				overlay_3.id			= 'overlay_part_3';
				overlay_4.id			= 'overlay_part_4';
			
				if (navigator.userAgent.toLowerCase().indexOf('firefox') == -1) { // if not using a firefox browser
					theBody.appendChild(overlay);
				} 
				else { // else, in firefox, four separate overlays must be created. This is a work around for the fix that the cursor in firefox can disapear when using more then one positioned element on top of another.  see https://bugzilla.mozilla.org/show_bug.cgi?id=167801
					theBody.appendChild(overlay_1);
					theBody.appendChild(overlay_2);
					theBody.appendChild(overlay_3);
					theBody.appendChild(overlay_4);	
				} 

			
			
				sign_in_source_ready = true;
		}

		// now add the signin-html itself to the new lightbox div
		document.getElementById('lightbox1').innerHTML=(signin_html_source);

	}


	function Start_SignIn (destination, key) {


		if (testing_signin) document.getElementById('signin_notice').innerHTML=('Signed In ?:' + is_modal_signed_in);


		// gather the hmtl code the witll be displayed in the sign box 
		Create_Sign_In_Source (); // defined in this document

		
		
			
		// activate the box itself

			display = 'block';
			
			if (navigator.userAgent.toLowerCase().indexOf('firefox') == -1) { // if not using a firefox browser
				document.getElementById('overlay_signin').style.display = display;
			} 
			else { // else, in firefox, four separate overlays must be created. This is a work around for the fix that the cursor in firefox can disapear when using more then one positioned element on top of another.  see https://bugzilla.mozilla.org/show_bug.cgi?id=167801
				document.getElementById('overlay_part_1').style.display = display;
				document.getElementById('overlay_part_2').style.display = display;
				document.getElementById('overlay_part_3').style.display = display;
				document.getElementById('overlay_part_4').style.display = display;		
			} 
			
			document.getElementById('lightbox1').style.display = display;



			if (navigator.userAgent.toLowerCase().indexOf('msie') != -1) { // If this is internet explorer
				
				if (document.getElementById('leightbox_end_anchor').offsetTop > 800) { 
					document.getElementById('overlay_signin').style.height = document.getElementById('leightbox_end_anchor').offsetTop;
					//alert ('Using Anchor');
				}
				else {
					//alert ('Using CSS 100% (leightbox_end_anchor.offsetTop): '+ document.getElementById('leightbox_end_anchor').offsetTop + ' and window.innerHeight: '+ window.innerHeight + ', and document.body.clientHeight: '+ document.documentElement.clientHeight +')')
					document.getElementById('overlay_signin').style.height = document.documentElement.clientHeight;
				}

				signin_selects = document.getElementsByTagName('select');
				for(i = 0; i < signin_selects.length; i++) {
					signin_selects[i].style.visibility = visibility;
				}
		
		
			}
		
		if (self.pageYOffset) {
			this.signin_original_scroll_Y = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.signin_original_scroll_Y = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.signin_original_scroll_Y = document.body.scrollTop;
		}


		if (self.pageXOffset) {
			this.signin_original_scroll_X = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollLeft){
			this.signin_original_scroll_X = document.documentElement.scrollLeft; 
		} else if (document.body) {
			this.signin_original_scroll_X = document.body.scrollLeft;
		}
	
			
		window.scroll(0, 0); 
		
	
		
		// if a destination was provided, save it to use after sign-in is complete
		if (Start_SignIn.arguments.length > 0) {
			Test_Out ('Destination: ' + destination);
			sign_in_destination = String(destination);
			
			if (sign_in_destination.indexOf('javascript:') == 0) {
				sign_in_destination = 'take_no_action';
				alert('Script execution is not allowed in the Sign-In Modal ( ' + sign_in_destination + ' )');				
			}

		} 

		
		

		if (Start_SignIn.arguments.length > 1) {
			Test_Out ('key: ' + key);
			sign_in_key = String(key);
		
			if (typeof Signin_Headlines[sign_in_key] != 'undefined') {
				document.getElementById('signin_headline_obj').innerHTML=(Signin_Headlines[sign_in_key]);
			}
			
			if (typeof Signin_Right_Widget[sign_in_key] != 'undefined') {
				document.getElementById('signin_right_widget_obj').innerHTML=(Signin_Right_Widget[sign_in_key]);
			}
		}
	
		
		return false;
	}
	


	function Start_SignIn_Static (key) {
	
		Start_SignIn ('take_no_action', key)
		
		return false;
	}
	

	function Clear_Signin_Error () {
		document.getElementById('signin_error_container').innerHTML=('');
	}
	
	function Show_Signin_Error_Message (error_message) {
		
		SignInErrorObj = document.createElement('div');
		SignInErrorObj.className = 'signin_error_obj';
		SignInErrorObj.setAttribute('id','signin_error');
		document.getElementById('signin_error_container').appendChild (SignInErrorObj);
		document.getElementById('signin_error').innerHTML=('! ' + error_message);
		
	}
	
	function ConfirmSignIn (result) {
		
		var error_re = /<error>([^<]*)<\/error>/g;  //
		
		var error_text = "";


		Test_Out ("Result received:");

		
		error_text = error_re.exec (String (result.html));
		
		Test_Out ("(result full: " + String (result.html) + ", " + error_re.exec (String (result.html)) + ")");		
		
		if ((typeof error_text) == "Array") {
			Test_Out ("error RE: ");
			for (var error_count = 1; error_count < error_text.length;  error_count ++ ) {
				Test_Out (error_text[error_count]);
			}
		}

				
		// Are we signed in?
	
		if (result.html.indexOf("1</data></stateData>") != -1 ) {	// Sign in succesful
			Test_Out("Signed IN");

			
			is_modal_signed_in = true;
			
			if (sign_in_destination.indexOf('javascript:') == 0) {
				alert('Script execution is not allowed in the Sign-In Modal ( ' + sign_in_destination + ' )');
			}
			else if (sign_in_destination == "take_no_action") {
				// then do nothing (eg don't reload or redirect the browser)
				sign_in_destination = ""; // reset the flag incase the SignIn modal is called again in this page session
				Close_Sign_In ();
			}
			else if (sign_in_destination != "" && sign_in_destination != 'reload' && sign_in_destination != 'refresh') {
				Close_Sign_In_No_Return ();
				window.location.hash = "temp";
				window.location = sign_in_destination;
			}
			else {
				Close_Sign_In_No_Return ();
				window.location.reload(true);
			}

			
		}
		else if (result.html.indexOf("0</data></stateData>") != -1 ) { // Sign in Not successful

			// Show error message
			Test_Out("Not signed in");
			if (error_text && error_text.length > 1) {
				for (var error_count_2 = 1; error_count_2 < error_text.length;  error_count_2 ++ ) {
					Show_Signin_Error_Message (error_text[error_count_2]);
				}
			}
		}
		
		return false;
	}
	

	function Run_Sign_In () {
			
		Test_Out ("Running Sign in. ");
				
		// In case this is the second+ time the user is attempting sign in, any old error message can be removed.
		Clear_Signin_Error();		
		
		var user_info = "&emailAddress=" + document.getElementById('signin_email_address').value + "&acctPassword=" + document.getElementById('signin_acct_password').value;
		
		signin_script_URL = signin_script_domain + signin_script_path + signin_script_qs + user_info; 
		
		var new_script = document.createElement ("script");
		new_script.type = "text/javascript";
		new_script.src = signin_script_URL;
		new_script.id = "dynamic_script_signin_call"; 
		document.getElementsByTagName('body')[0].appendChild(new_script);
		if (testing_signin && signin_script_URL.length > 60) {
			Test_Out("Calling " + signin_script_URL.substr(0,59) + '...');
		}
		else {
		
			Test_Out("Calling " + signin_script_URL);
		}
		
		Test_Out ("Done running Sign in.");

		return false;
	}


	
	function Close_Sign_In () {
	
		window.scroll(signin_original_scroll_X, signin_original_scroll_Y);
		
		Close_Sign_In_No_Return ();
	
	}
	
	function Close_Sign_In_No_Return () {
		
		var old_script = "";

		var display = 'none';
		
		if (navigator.userAgent.toLowerCase().indexOf('firefox') == -1) { // if not using a firefox browser
			document.getElementById('overlay_signin').style.display = display;
		} 
		else {  // else, in firefox, four separate overlays must be created. 
				//  This is a work around for the fix that the cursor in firefox 
				//  can disapear when using more then one positioned element on top 
				//  of another.  see https://bugzilla.mozilla.org/show_bug.cgi?id=167801
			document.getElementById('overlay_part_1').style.display = display;
			document.getElementById('overlay_part_2').style.display = display;
			document.getElementById('overlay_part_3').style.display = display;
			document.getElementById('overlay_part_4').style.display = display;		
		} 
		
				
		
		document.getElementById('lightbox1').style.display = display;

		document.getElementById('lightbox1').innerHTML=(signin_html_source);	

		if (testing_signin) document.getElementById('signin_notice').innerHTML +=('SignIn Reset');
	
		
	
		return false;	
	}
	
	
