/****************************  Modification History  *****************************
Sl No:	Date		Modified by	  Description
**********************************************************************************
[1]		08/10/10	Sharad		  IRC download data capturing module implementation
**********************************************************************************/
//[3] changes start
		function showPopUp(el,divName) {
			document.body.style.cursor = "default";
			if(divName && divName != 'none')
			{
				//document.getElementById(divName).style.display = "block";
			}
			var dlg = document.getElementById(el);
			//cvr.style.display = "block";
			

			dlg.style.top = getPageHeight();
			//dlg.style.left = (screen.width/2)-(280)+'px';
			//423421:Changing to set the exact width based on document offset	  
            dlg.style.left = (document.body.offsetWidth/2)-(280)+'px'
			dlg.style.display = "block";
			if(document.getElementById("tabcontent7"))
				document.getElementById("tabcontent7").style.display = "none";
			
		}

		//423421:Start:New function added to re-align the popup in centre of the screen on resizing the browser window 
           
	   function resize()
	  {
 	       var dlg;
               if(document.getElementById ('loginLayer') && document.getElementById ('loginLayer').style.display == 'block') {
                  dlg = document.getElementById('loginLayer');
                  dlg.style.top = getPageHeight();
		    dlg.style.left = (document.body.offsetWidth/2)-(280)+'px';
               } else if(document.getElementById ('errorLoginMessage') && document.getElementById ('errorLoginMessage').style.display == 'block') {
                  dlg = document.getElementById('errorLoginMessage');
                  dlg.style.top = getPageHeight();
		    dlg.style.left = (document.body.offsetWidth/2)-(280)+'px';
               } else if(document.getElementById ('nonIrcSubscribedErrMessage') && document.getElementById ('nonIrcSubscribedErrMessage').style.display == 'block') {
                  dlg = document.getElementById('nonIrcSubscribedErrMessage');
                  dlg.style.top = getPageHeight();
		    dlg.style.left = (document.body.offsetWidth/2)-(280)+'px';
               } else if(document.getElementById ('expireSoonMessage') && document.getElementById ('expireSoonMessage').style.display == 'block') {
                  dlg = document.getElementById('expireSoonMessage');
                  dlg.style.top = getPageHeight();
		    dlg.style.left = (document.body.offsetWidth/2)-(280)+'px';
               } else if(document.getElementById ('ssoErrorMessage') && document.getElementById ('ssoErrorMessage').style.display == 'block') {
                  dlg = document.getElementById('ssoErrorMessage');
                  dlg.style.top = getPageHeight();
		    dlg.style.left = (document.body.offsetWidth/2)-(280)+'px';
               } 
                var dark=document.getElementById('darkenScreenObject');
		   if (dark && dark.style.display=='block')
		   {
				 var pageWidth='100%';
			     var pageHeight='100%';
                 dark.style.width= pageWidth;
				 dark.style.height = pageHeight;
                            
		   }
                
             }
          //423421:End:New function added to re-align the popup in centre of the screen on resizing the browser window
		
		function getPageHeight(){
			var pageHeight = '150px';

			if( document.body && document.body.scrollTop) {
				pageHeight = document.body.scrollTop + 150 + 'px';
			} else if( document.documentElement && document.documentElement.scrollTop ) {
			    pageHeight = document.documentElement.scrollTop + 150 +'px';
			} 

			return pageHeight;
		}

		function swapModalLayer(el,e2) {
			//grayOut(true);
			document.body.style.cursor = "default";
			document.getElementById("cancel").disabled=false;
			document.getElementById("submit").disabled=false;
			document.getElementById("requestaccess").disabled=false;
			var dlg1 = document.getElementById(el);
			var dlg2 = document.getElementById(e2);
			dlg1.style.display = "none";
			dlg2.style.top = getPageHeight();
			//dlg2.style.left = (screen.width/2)-(280)+'px';
			//423421:Changing to set the exact width based on document offset 
            dlg2.style.left = (document.body.offsetWidth/2)-(280)+'px'
			//setDownloadURL();

			dlg2.style.display = "block";
			if(document.getElementById("tabcontent7"))
				document.getElementById("tabcontent7").style.display = "none";
		}
		
		function setDownloadURL(){
			
			alert('setDownloadURL - why are you calling me?');
			return;
			
			var ssoFrame = getIframeObject();
			//alert("ssoFrame = "+ssoFrame);
			var form = getIframeFormObject(ssoFrame);
			var downloadResourceUrl = form.downloadResourceUrl.value;

			if(document.getElementById("downloadURL"))
			{
				//document.getElementById("downloadURL").href = "http://download.pearsoned.com/cgidownload/wcm_filedownload.cgi?1ID=105186-1+Gerrig+19e+IM.pdf&fldr=0&smsuserid=23839779";
				document.getElementById("downloadURL").href = downloadResourceUrl;
			}
		}

		function closePopUp(el,divName) {
			grayOut(false);
			if(divName && divName != 'none')
			{
				//document.getElementById(divName).style.display = "block";
			}
			var dlg = document.getElementById(el);
			//cvr.style.display = "none";
			dlg.style.display = "none";
			document.body.style.overflow = "visible";
			if(document.getElementById("tabcontent7"))
				document.getElementById("tabcontent7").style.display = "block";
		}
		function grayOut(vis, options) {
				  // Pass true to gray out screen, false to ungray
				  // options are optional.  This is a JSON object with the following (optional) properties
				  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
				  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
				  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
				  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
				  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
				  // in any order.  Pass only the properties you need to set.
				  var options = options || {}; 
				  var zindex = options.zindex || 10000;
				  var opacity = options.opacity || 50;
				  var opaque = (opacity / 100);
				  var bgcolor = options.bgcolor || '#000000';
				  var dark=document.getElementById('darkenScreenObject');
				  if (!dark) {
					// The dark layer doesn't exist, it's never been created.  So we'll
					// create it here and apply some basic styles.
					// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
					var tbody = document.getElementsByTagName("body")[0];
					var tnode = document.createElement('div');           // Create the layer.
						tnode.style.position='fixed';                 // Position fixed
						tnode.style.top='0px';                           // In the top
						tnode.style.left='0px';                          // Left corner of the page
						tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
						tnode.style.display='none';                      // Start out Hidden
						tnode.id='darkenScreenObject';                   // Name it so we can find it later
					tbody.appendChild(tnode);                            // Add it to the web page
					dark=document.getElementById('darkenScreenObject');  // Get the object.
				  }
				  if (vis) {
					// Calculate the page width and height 
					if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
						var pageWidth = document.body.scrollWidth+'px';
						var pageHeight = document.body.scrollHeight+'px';
					} else if( document.body.offsetWidth ) {
					  var pageWidth = document.body.offsetWidth+'px';
					  var pageHeight = document.body.offsetHeight+'px';
					} else {
					   var pageWidth='100%';
					   var pageHeight='100%';
					}   

					//if (document.body.style.overflow = "hidden") {
						//dark.style.width = "600px"
						//dark.style.height = "100%"
					//}
					//set the shader to cover the entire page and make it visible.
					dark.style.opacity=opaque;                      
					dark.style.MozOpacity=opaque;                   
					dark.style.filter='alpha(opacity='+opacity+')'; 
					dark.style.zIndex=zindex;        
					dark.style.backgroundColor=bgcolor;  
					dark.style.width= pageWidth;
					dark.style.height= pageHeight;
					dark.style.display='block';                          
				  } else {
					 dark.style.display='none';
				  }
				}

				
				function downloadResource(fileUrl, fileName) {	
					var file = fileName;
					var url = fileUrl;
					var fldr;

					if(url == "//" && file == '') {
						var ssoFrame = getIframeObject();
						var form = getIframeFormObject(ssoFrame);
						fldr = form.fldr.value;
						file = form.fileName.value;
						url = '/'+fldr+'/'+file;
					}
					dcsMultiTrack('DCS.dcsuri',url,'WT.ti',file);					
					closePopUp("downloadResource","none");

					//[1] MOD Start
					var productForm = document.getElementById(file);
					var dataCaptureForm = document.getElementById('parentReferISBN13');
					if(dataCaptureForm!=null && document.getElementById('Parent_'+dataCaptureForm.value)!=null){
					var parentForm = document.getElementById('Parent_'+dataCaptureForm.value);
						productForm.parentISBN10.value=parentForm.parentISBN10.value;
						productForm.parentISBN13.value=parentForm.parentISBN13.value;
						productForm.parentLeadAuthorLastName.value=parentForm.parentLeadAuthorLastName.value;
						productForm.parentLeadAuthorFirstName.value=parentForm.parentLeadAuthorFirstName.value;
						productForm.parentTitle.value=parentForm.parentTitle.value;
						productForm.parentEdition.value=parentForm.parentEdition.value;
						productForm.parentCopyRightYear.value=parentForm.parentCopyRightYear.value;
					}
					productForm.iwPreActions.value = "supplementDownloadData";
					productForm.fileFldr.value = fldr;
					productForm.submit();

					// will not need at all
					pausecomp(500);
					//[1] MOD END

					return false;
				}
				
				function getIframeObject()
				{
					var ssoFrame = null;

					var iframeEl = document.getElementById('ssoIframe');
					if ( iframeEl.contentDocument ) { // DOM
						 //alert("In DOM if");
						 ssoFrame = iframeEl.contentDocument;
					} else if ( iframeEl.contentWindow ) { // IE win
						 //alert("In IE Win else");
						 ssoFrame = iframeEl.contentWindow.document;
					}

					return ssoFrame;
				}

				function getIframeFormObject(ssoFrame)
				{
					var form = ssoFrame.getElementById('ssoValues');
					return form;
				}

				function checkLogin(divName,fileUrl,isbn,prodIsbn,fileName) {
					
					var ssoFrame = getIframeObject();
					//alert("ssoFrame = "+ssoFrame);
					var form = getIframeFormObject(ssoFrame);
					//alert("form = "+form);
					
					ssoFrame.getElementById("ssoFlag").value = 'chk_login';
					//ssoFrame.getElementById("iwPreActions").value = 'popupLoginControl';
					form.iwPreActions.value = 'popupLoginControl';
					form.ssoFlag.value = 'chk_login';
					//alert(form.getElementById("iwPreActions").value);
					
					form.fileUrl.value = fileUrl;
					form.fileName.value = fileName;
					form.isbn.value = isbn;
					form.prodIsbn.value = prodIsbn;

					form.submit();
					grayOut(true);
					document.body.style.cursor = "wait";
					var counter = 0;
					showModalLayer('chk_login',counter);
									
				}
				function showModalLayer(param,counter)
				{
					var ssoFrame = getIframeObject();

					//alert("ssoFrame = "+ssoFrame);
					var form = getIframeFormObject(ssoFrame);
					
					
					if(form && form != null)
					{
						var chk1 = form.ssoFlag.value;
						var chk2 = ssoFrame.getElementById("ssoFlag").value;
											
						//alert(chk1+"-"+chk2+"-"+param);
						// if user is not logged in
						if(chk1 == "notLogin" || chk2 == "notLogin")
						{
							if(param == 'chk_login')
							{
								document.getElementById("cancel").disabled=false;
								document.getElementById("submit").disabled=false;
								document.getElementById("requestaccess").disabled=false;

								document.getElementById("password1").value='';
								document.getElementById("loginname").value='';
															
								form.username.value = '';
								form.password.value = '';
								form.ssoFlag.value = '';

								showPopUp('loginLayer','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','downloadResource');
							}
							counter = 61;
						}
						// if user is already logged in
						if(chk1 == "ok" || chk2 == "ok")
						{
							//setDownloadURL();
							if(param == 'chk_login')
							{
								showPopUp('downloadResource','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','downloadResource');
							}
							counter = 61;
						}
						// Some SSO based error occured
						if(chk1 == "error" || chk2 == "error")
						{
							if(param == 'chk_login')
							{
								showPopUp('errorLoginMessage','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','errorLoginMessage');
							}
							counter = 61;
						}
						//[8]change to SSO messaging-383958[PG]
						if(chk1 == "nonIrcSubscribed" || chk2 == "nonIrcSubscribed")
						{
							
							if(param == 'chk_login')
							{
								showPopUp('nonIrcSubscribedErrMessage','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','nonIrcSubscribedErrMessage');
							}
							counter = 61;
						}
						//[8] End
						if(chk1 == "expired" || chk2 == "expired")
						{
							if(param == 'chk_login')
							{
								showPopUp('expiredMessage','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','expiredMessage');
							}
							counter = 61;
						}
						if(chk1 == "soonToExpire" || chk2 == "soonToExpire")
						{
							if(param == 'chk_login')
							{
								showPopUp('expireSoonMessage','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','expireSoonMessage');
							}
							counter = 61;
						}
						if(chk1 == "internalError" || chk2 == "internalError")
						{
							if(param == 'chk_login')
							{
								showPopUp('ssoErrorMessage','none');
							}
							else if(param == 'login')
							{
								swapModalLayer('loginLayer','ssoErrorMessage');
							}
							counter = 61;
						}
					}
					counter = counter +1;
					//alert(counter);

					if(counter <= 60)
						setTimeout ( "showModalLayer('"+param+"',"+counter+")", 1000 );
					else if(counter == 61)
					{	
						if(form == null)
						{
							 window.open("/educator/main_content/session_expire_message.page","_self");
							return false;
						}
						showPopUp('ssoErrorMessage','none');
					}
					
						

				}

				function submitLoginForm()
				{
					//alert(document.getElementById('loginname').value+" - "+document.getElementById('password').value);
					
					var ssoFrame = getIframeObject();
					var form = getIframeFormObject(ssoFrame);
					//alert("form = "+form);

					document.getElementById("cancel").disabled=true;
					document.getElementById("submit").disabled=true;
					document.getElementById("requestaccess").disabled=true;

					form.iwPreActions.value = "popupLoginControl";
					form.ssoFlag.value = 'login';
					form.username.value = document.getElementById('loginname').value;
					form.password.value = document.getElementById('password1').value;
					//alert(form.getElementById("iwPreActions").value);

					form.submit();
					document.body.style.cursor = "wait";
					var counter = 0;
					showModalLayer('login',counter);

				}
				function submitDownloadResourceForm(fileUrl,isbn,prodIsbn,fileName,divName)
				{
				  // alert('in Submit form : '+ fileUrl + isbn);
				    var myForm = document.getElementById('downloadResourcesForm');
				    if (null != myForm)
				    {   

								//alert('Submit Form  : isbn : ' + isbn );
								
								myForm.iwPreActions.value = 'downloadResourcesForm';
								myForm.fileUrl.value = fileUrl;
								myForm.fileName.value = fileName;
								myForm.isbn.value = isbn;
								myForm.prodIsbn.value = prodIsbn;
								checkLogin(divName,fileUrl,isbn,prodIsbn,fileName);
					//myForm.submit();      
				    }
				    return false;
				}   
				function pausecomp(millis) 
				{
					var date = new Date();
					var curDate = null;

					do { curDate = new Date(); } 
						while(curDate-date < millis);
				} 

				
				//[3] changes end
				
				
		

