//****************************************************************************
//page:		checkLink.js
//Author:	Y.Grave
//Date:		15-06-2006
//Purpose:	
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//****************************************************************************
// return querystring variable.
function querystring(name)
{
	var tmp = unescape(location.search.substring(1));
	var i = tmp.toUpperCase().indexOf(name.toUpperCase() + "=");

	if (i >= 0)
	{
        tmp = tmp.substring(name.length + i + 1);
        i = tmp.indexOf("&");
        return(tmp = tmp.substring(0, (i >= 0) ? i : tmp.length));
	}

	return("");
}

//=============================================================================
// function: checkLink
// purpose: close mediaContainer 
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
function gotoMediaSetting(objLink)
  {
		if(mediaContainer.getState()=='collapsed')
		{
			mediaContainer.expand();
			checkState(objLink);
		}
		else
		{
			//mediaContainer.collapse();
			checkState(objLink);
		}
		
  }

  //=============================================================================
// function: checkLink
// purpose: close mediaContainer 
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
function gotoInfoSetting(objLink)
  {
	  //alert(mediaContainer.getState());
	  if (mediaContainer!=null)
	  {
		if(mediaContainer.getState()=='expanded')
		{
			//mediaContainer.collapse();
			checkState(objLink);
		}
		else {
			checkState(objLink);
		}
	  }
	  else {
		  document.location.href=objLink;
	  }
  }

  //=============================================================================
// function: checkFlashLink
// purpose: check whether link clicked from flash menu is in mediasetting, then close mediaContainer 
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
function checkFlashLink(objLink)
  {
		var obj_mediaSetting = false
		objLink = objLink.toLowerCase();
		if(objLink.indexOf('chapterid') != -1)
		{
			
			var chapterID = objLink.substring(objLink.indexOf('chapterid')+10,objLink.indexOf('chapterid')+14);
			if(chapterID.length>0)
			{
				if(checkChapterID(chapterID))
				{
					if(objLink.indexOf('contentid') != -1)
					{
						var contentID = objLink.substring(objLink.indexOf('contentid')+10,objLink.indexOf('contentid')+15);
						if(checkContentID(contentID))
						{
							obj_mediaSetting = true;
						}
					}
					else
					{
						obj_mediaSetting = true;
					}
				}
			}
		}
		if(obj_mediaSetting) {
			//alert("gotoMediaSetting");
			gotoMediaSetting(objLink);
		}
		else {
			//alert("gotoInfoSetting");
			document.location.href=objLink;
		}
  }

//=============================================================================
// function: checkState
// purpose: checkState of mediaContainer and if finish redirect to the right page
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
  function checkState(objLink)
  {
  	if(mediaContainer.getState()=='busy')
	{
		setTimeout('checkState(\''+objLink+'\')',1750);
	}
	else
	{
		document.location.href=objLink;
	}
  }
  

//=============================================================================
// function: checkChapterLink
// purpose: check all link in the page and change href to onclick if chapter 
//          of content are present in the array
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
  
  function checkChapterLink()
  {
  
  try
 {
  	var TotaalTagA = document.getElementsByTagName('a');
	

	for(i=0; i<= TotaalTagA.length-1; i++)
	{
		if (TotaalTagA[i]!= null)
		{	
			if (TotaalTagA[i].getAttribute('className') != null){
    				chkHref = TotaalTagA[i].getAttribute('className');
  				}
 	 		 else {
    				chkHref = TotaalTagA[i].getAttribute('class');
  			}  
			var obj_mediaSetting = false;
			var hrefContent = TotaalTagA[i].href.toLowerCase();
			if(hrefContent.indexOf('chapterid') != -1 && chkHref != "thickbox")
			{
				
				var chapterID = hrefContent.substring(hrefContent.indexOf('chapterid')+10,hrefContent.indexOf('chapterid')+14);
				if(chapterID.length>0)
				{
					if(checkChapterID(chapterID))
					{
						if(hrefContent.indexOf('contentid') != -1)
						{
							var contentID = hrefContent.substring(hrefContent.indexOf('contentid')+10,hrefContent.indexOf('contentid')+15);
							if(checkContentID(contentID))
							{
								obj_mediaSetting = true;
							}
						}
						else
						{
							obj_mediaSetting = true;
						}
					}
				}
				switchHrefToOnclik(TotaalTagA[i], obj_mediaSetting);
			}
		}
	 }
	 }
	 catch(err)
		{
			alert("Error" + err + "in CheckChapterLink");
		}
  }
  
//=============================================================================
// function: switchHrefToOnclik
// purpose: set href to # and create onclick evt with the href value in the js
//          function
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
  function switchHrefToOnclik(obj_Link, obj_mediaSetting)
  {
  		var hrefText = obj_Link.href;
		obj_Link.href="#";
		var evType = 'click';
		
		if (obj_mediaSetting)
		{
		   if(obj_Link.target == "_parent")
		   {
			var fn = function(){parent.gotoMediaSetting(hrefText)};
			}
			else
			{
			var fn = function(){gotoMediaSetting(hrefText)};
			}
		}
		else {
		    if(obj_Link.target == "_parent")
		    {
				var fn = function(){gotoInfoSetting(hrefText)};
			}
			else
			{
				var fn = function(){parent.gotoInfoSetting(hrefText)};
			}
		}
		var useCapture = '';
		
		  if (obj_Link.addEventListener){
		    	obj_Link.addEventListener(evType, fn, false);
		    	return true;
		  }
		  else if (obj_Link.attachEvent){
   			var r = obj_Link.attachEvent("onclick", function() { fn.apply(obj_Link); return false; });
   			return r;
 			  }
		  else 
		  {
		    return null;
		  }
  }
 
 
 //=============================================================================
// function: checkContentID
// purpose: check if contentID x  is present in the array
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================

 
  function checkContentID(int_contentID)
  {
   var return_value = false;
  	for(var w=0; w<=arrayOfContent.length; w++)
	{
  		if(arrayOfContent[w]== int_contentID){
			 return_value = true; 
			 break;
		}
	}
  	return return_value;
  }
  
  
//=============================================================================
// function: checkChapterID
// purpose: check if chapterID x  is present in the array
//-----------------------------------------------------------------------------
// Date changed:		By who:			Reason:
//=============================================================================
  
  function checkChapterID(int_chapterID)
  {
  	var return_value = false;
  	for(var i=0; i<=arrayOfChapter.length; i++)
	{
  		if(arrayOfChapter[i]== int_chapterID){
			 return_value= true; 
			 break;
		}
	}
		return return_value;
  }
