var pathname1 = document.location.pathname;
var pathname2 = pathname1.substring(pathname1.indexOf("/")+1,pathname1.length);
var ctxPath = "/" + pathname2.substring(0,pathname2.indexOf("/"));
var temp = "";


/*	function: newOpener - loads URL on the parent window

			newURL: 

*/

function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function newOpener(newURL)

{

	opener.document.location = newURL;

}





/*	function: newWin - opens a new window

			newWin 			takes on 7 values:

			WinURL: 		the URL to be loaded into the new window

			WinNAME:		the NAME of the popup window

			WinWIDTH: 	the WIDTH of the popup window (currently set at 700px)

			WinHEIGHT:	the HEIGHT of the popup window (currently set at 480px)

			WinRESIZE:	the "yes" or "no" value for the resize window property

			WinSCROLL:	the "yes" or "no" value for the scrollbars window property

			WinTOOLBAR: the "yes" or "no" value for the toolbar property

*/



function newWin(WinURL, WinNAME, WinWIDTH, WinHEIGHT, WinRESIZE, WinSCROLL, WinTOOLBAR)

{	

	/*	variables passed by the link calling the function	*/

	var URL = WinURL;

	var WINDOW_NAME = WinNAME;

	var POPUP_WIDTH = WinWIDTH;

	var POPUP_HEIGHT = WinHEIGHT;

	var POPUP_RESIZE = WinRESIZE;

	var POPUP_SCROLL = WinSCROLL;

	var POPUP_TOOLBAR = WinTOOLBAR;



	/*	window properties that determine the window attributes depending on what values are passed by the link calling the function	*/

	var window_toolbar = POPUP_TOOLBAR;

	var window_resize = POPUP_RESIZE;

	var window_scroll = POPUP_SCROLL;

	var window_width = POPUP_WIDTH;

	var window_height = POPUP_HEIGHT;

	var screen_balance_w = ((screen.width)-(window_width))/2;

	var screen_balance_h = ((screen.height)-(window_height))/2;

	var set_top = screen_balance_h;

	var set_left = screen_balance_w;	



	win = window.open(URL,WINDOW_NAME,'resizable=' + window_resize + ',toolbar=' + window_toolbar + ',scrollbars=' + window_scroll + ',width=' + window_width + ',height=' + window_height + ',top=' +  set_top + ',left=' + set_left + '');

	win.focus();

}

/*to check for the handout notes size*/
function checkNotesSize(txtArea)
{
	var maxLen = 180; 
    if (txtArea.value.length > maxLen) 
        {
            var msg = "You have exceeded the Size limit for Notes. Please limit the notes to 180 characters.";
            alert(msg);
            txtArea.value = txtArea.value.substring(0, maxLen);
         }
}

function newWinAddress(WinURL, WinNAME, WinWIDTH, WinHEIGHT, WinRESIZE, WinSCROLL, WinTOOLBAR)

{	

	/*	variables passed by the link calling the function	*/

	var URL = WinURL;

	var WINDOW_NAME = WinNAME;

	var POPUP_WIDTH = WinWIDTH;

	var POPUP_HEIGHT = WinHEIGHT;

	var POPUP_RESIZE = WinRESIZE;

	var POPUP_SCROLL = WinSCROLL;

	var POPUP_TOOLBAR = WinTOOLBAR;



	/*	window properties that determine the window attributes depending on what values are passed by the link calling the function	*/

	var window_toolbar = POPUP_TOOLBAR;

	var window_resize = POPUP_RESIZE;

	var window_scroll = POPUP_SCROLL;

	var window_width = POPUP_WIDTH;

	var window_height = POPUP_HEIGHT;

	var screen_balance_w = ((screen.width)-(window_width))/2;

	var screen_balance_h = ((screen.height)-(window_height))/2;

	var set_top = screen_balance_h;

	var set_left = screen_balance_w;	



	win = window.open(URL,WINDOW_NAME,'resizable=' + window_resize + ',toolbar=' + window_toolbar + ',scrollbars=' + window_scroll + ',width=' + window_width + ',height=' + window_height + ',top=' +  set_top + ',left=' + set_left + ',location=yes');

	win.focus();

}





/*	function: closeWin - closes the child window	*/



function closeWin()

{

	top.window.close();

}





/*	

THIS JAVASCRIPT ALLOWS FOR AUTOMATIC FIELD TABBING FOR PHONE NUMBERS AND STUFF 

*/

var downStrokeField;

function autojump(fieldName,nextFieldName,fakeMaxLength)

{

var myForm=document.forms[document.forms.length - 1];

var myField=myForm.elements[fieldName];

myField.nextField=myForm.elements[nextFieldName];



if (myField.maxLength == null)

   myField.maxLength=fakeMaxLength;



myField.onkeydown=autojump_keyDown;

myField.onkeyup=autojump_keyUp;

}



function autojump_keyDown()

{

this.beforeLength=this.value.length;

downStrokeField=this;

}



function autojump_keyUp()

{

if (

   (this == downStrokeField) && 

   (this.value.length > this.beforeLength) && 

   (this.value.length >= this.maxLength)

   )

   this.nextField.focus();

downStrokeField=null;

}



function check_field(f){

illegal = /[^\w._\-]/

email = f.EMAILADDR.value;



if (email.length<6 || email.indexOf('@')==-1 || email.indexOf('.')==-1)

	email="";

else { 

	At = email.indexOf('@');

	Period = email.lastIndexOf('.');

	DNS1 = email.substring(0,At);

	if (DNS1.length<1 || DNS1.match(illegal)!=null) email="";

		DNS2 = email.substring(At+1,Period);

	if (DNS2.length<1 || DNS2.match(illegal)!=null) email="";

		DNS3 = email.substring(Period+1,email.length);

	if (DNS3.length<2 || DNS3.match(illegal)!=null) email="";}



if (email.length==0) {

	alert("The email address you have entered is not valid.\nPlease re-enter a valid  email address.");

	f.EMAILADDR.focus();

	return false;} 

	

if(f.FIRSTNAME.value == ''){

 alert("Please enter your first name");

	f.FIRSTNAME.focus();

 return false;}

if(f.LASTNAME.value == ''){

 alert("Please enter your last name");

	f.LASTNAME.focus();

 return false;} 



return true;

}



function newExitWin(WinURL, WinNewUrlParam, WinNewUrl, WinNAME, WinWIDTH, WinHEIGHT, WinRESIZE, 

WinSCROLL, WinTOOLBAR) 

	{       

	        /*      variables passed by the link calling the function       */ 

	        var newUrl = removeSpaces(WinNewUrl); 

	        newUrl = newUrl.replace('?','%3F'); 

	        while (newUrl.indexOf('=') > -1){ 

	                newUrl = newUrl.replace('=','%3D'); 

	        } 

	        while (newUrl.indexOf('&') > -1){ 

	                newUrl = newUrl.replace('&','%26'); 

	        } 

	        

	        var URL = WinURL+'?'+WinNewUrlParam+'='+newUrl; 

	        var WINDOW_NAME = WinNAME; 

	        var POPUP_WIDTH = WinWIDTH; 

	        var POPUP_HEIGHT = WinHEIGHT; 

	        var POPUP_RESIZE = WinRESIZE; 

	        var POPUP_SCROLL = WinSCROLL; 

	        var POPUP_TOOLBAR = WinTOOLBAR; 



	        /*      window properties that determine the window attributes depending on what values 

are passed by the link calling the function     */



	      /*  var window_toolbar = POPUP_TOOLBAR; 

	        var window_resize = POPUP_RESIZE; 

	        var window_scroll = POPUP_SCROLL; 

	        var window_width = POPUP_WIDTH; 

	        var window_height = POPUP_HEIGHT; 

	        var screen_balance_w = ((screen.width)-(window_width))/2; 

	        var screen_balance_h = ((screen.height)-(window_height))/2; 

	        var set_top = screen_balance_h; 

	        var set_left = screen_balance_w; 



	        win = window.open(URL,WINDOW_NAME,'resizable=' + window_resize + ',toolbar=' + 

window_toolbar + ',scrollbars=' + window_scroll + ',width=' + window_width + ',height=' + window_height + 

',top=' +  set_top + ',left=' + set_left + '');



	        win.focus(); */
	        tb_show('Title', WinURL+'?modal=true&'+WinNewUrlParam+'='+newUrl, false);

	} 
	
function newWinExit(WinURL, WinNewUrlParam, WinNewUrl, WinNAME, WinWIDTH, WinHEIGHT, WinRESIZE, 

WinSCROLL, WinTOOLBAR) 

	{       
	        /*      variables passed by the link calling the function       */ 

	        var newUrl = removeSpaces(WinNewUrl);

	        newUrl = newUrl.replace('?','%3F'); 

	        while (newUrl.indexOf('=') > -1){ 

	                newUrl = newUrl.replace('=','%3D'); 

	        } 
              while (newUrl.indexOf('&') > -1){ 

	                newUrl = newUrl.replace('&','%26'); 

	        } 


	        var URL = WinURL+'?'+WinNewUrlParam+'='+newUrl; 

	        var WINDOW_NAME = WinNAME; 
	         var POPUP_WIDTH = 800; 

	        var POPUP_HEIGHT = 600;
	        var w=800;
	        var h=600; 
            if (window.screen) { 
				w = window.screen.availWidth; 
				h = window.screen.availHeight; 
		     } 
 	        var POPUP_RESIZE = WinRESIZE; 

	        var POPUP_SCROLL = WinSCROLL; 

	        var POPUP_TOOLBAR = WinTOOLBAR; 

            //alert('tt:' + WinURL+'?modal=true&'+WinNewUrlParam+'='+newUrl +'&width='+w+'&height='+h);
	         tb_show('Title', WinURL+'?modal=true&'+WinNewUrlParam+'='+newUrl +'&w='+w+'&h='+h, false);
	        //tb_show('Title', WinURL+'?modal=true&'+WinNewUrlParam+'='+newUrl, false);
		} 	



function handleDropDown(sellist) {

	if ( sellist.selectedIndex != 0 )

		window.open(sellist.options[sellist.selectedIndex].value);

	sellist.selectedIndex = 0;

}

//-->
/*
Ajax Functions to Add content /Remove content towards My-library 
*/

function addSection(image, id, parentid,sectiondocid,isSupermodule,readerLevel,langCode,libCountTemp) {
	var url = ctxPath + "/addSection/"+id+"/"+parentid+"/"+sectiondocid+"/"+readerLevel+"/"+langCode+"/"+isSupermodule+".html"	
	var req = newXMLHttpRequest();
	req.onreadystatechange = getReadyStateHandlerForText(req, showSectionPopup);
	req.open("POST", url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("");
//	alert("libCountTemp::"+libCountTemp);
	if(libCountTemp == null || libCountTemp== ''){
		temp = "1";
		
	}
	//var imgs = document.getElementsByName("addremovelibrarybtn");
	//for (var i=0; i<imgs.length; i++) {
 	 	//imgs[i].src= ctxPath + "/images/btn_removefrommylibrary.gif";
 	 	image.className="removefrommylibrary";
 	 	image.title = "Remove this page from My Library";
 	 	image.onclick = function(){removeSection(this, id, parentid, sectiondocid, isSupermodule,readerLevel,langCode,libCountTemp); return false;};
 	 //}
}

function removeSection(image, id, parentid, sectiondocid, isSupermodule,readerLevel,langCode,libCountTemp) {
	var url = ctxPath + "/removeSection/"+id+"/"+parentid+".html"
	var req = newXMLHttpRequest();
	req.onreadystatechange = getReadyStateHandlerForText(req, updateContents);
	req.open("POST", url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("");
	if(libCountTemp==1 || temp=="1"){
		temp = "0";
		
	}
	//var imgs = document.getElementsByName("addremovelibrarybtn");
	//for (var i=0; i<imgs.length; i++) {
		//imgs[i].src = ctxPath + "/images/btn_addtomylibrary.gif";
		image.className="addtomylibrary";
		image.title = "Add this page to My Library";
		image.onclick = function(){addSection(this, id, parentid,sectiondocid, isSupermodule,readerLevel,langCode); return false;};
	//}
}

function updateContents(){
	return true;
}

function showSectionPopup(retVal){
var user_logged = document.cookie.indexOf("user_logged=1");
	if(retVal.substring(0,4) == 'true')
	{		
		tb_show('Title', ctxPath +'/addanonyuserctntpopup.html?modal=true&height=490&width=650', false);		
	}else if(retVal.substring(0,4) == 'noti')
	{		
		tb_show('Title', ctxPath+'/sessionTimeoutPopup.html?modal=true&isLib=true&userlogged='+user_logged, false);	
	}
	return true;
}

function getReadyStateHandlerForDisease(req, responseTextHandler,url) {
	// Return an anonymous function that listens to the XMLHttpRequest instance
	return function () {
		// If the request's status is "complete"
		if (req.readyState == 4) {
			// Check that we received a successful response from the server
			if (req.status == 200) {
				// Pass the XML payload of the response to the handler function.
				responseTextHandler(req.responseText,url);
			} 
			else {
				// An HTTP problem has occurred					
			}
		}
	}
 }
 
 function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}
function handlecontentIds(obj)
{
    var newContentIdsStr = '';
    var j = 0;
    var contentIdsArr = contentIdsStr.split(",");
    if(obj.checked == false)
	{
		
		for(i=0; i<(contentIdsArr.length); i++)
		{
			if(obj.id == contentIdsArr[i] || contentIdsArr[i] == '')
			 continue;
			 newContentIdsStr = newContentIdsStr+contentIdsArr[i]+ ",";
			 
		}
		contentIdsStr = newContentIdsStr;
	}
	else
	{		
		for(i=0; i<(contentIdsArr.length); i++)
		{			
			if(obj.id == contentIdsArr[i])
			 return;
		}
		
		if(contentIdsStr.substr(contentIdsStr.length-1, 1) != "," && contentIdsStr.length > 0)
		{
			contentIdsStr = contentIdsStr + ",";
		}
		
		contentIdsStr = contentIdsStr + obj.id + ",";
		
	}
}
