// JavaScript Document
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}


function handleServerResponse() 
{
  if (xmlHttp.readyState == 4) 
  {	   
 	if (xmlHttp.status == 200) 
    {
		var xmlResponse = xmlHttp.responseXML;
		  
		if(!xmlResponse || !xmlResponse.documentElement)
		{
			throw("Invalid XML structure:\n" + xmlHttp.responseText);  
		}
		  
		var rootNodeName=xmlResponse.documentElement.nodeName;
		  
		if(rootNodeName == "parserror")
		{
			throw("Invalid XML structure");
		}
	  	xmlRoot=xmlResponse.documentElement;
		
		var titleArray1 = xmlRoot.getElementsByTagName("obj1");
		var helloMessage1=titleArray1.item(0).firstChild.data;	
		document.getElementById("theDiv").innerHTML=helloMessage1;			
		
    } 
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function goToFilmAjax(targetpage, whatUID, maxListItem, whatObj)
{
	var domainName="http://www.marginal.dk/";
	var folderpath="fileadmin/template/main/php/";
	
	for(var i=0; i<maxListItem; i++)
	{
		document.getElementById("nList" + i).style.color="#ffffff";
	}	
	whatObj.style.color="#666666";
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{			
		var date = new Date();
		var timestamp = date.getTime();
		
		xmlHttp.open("GET", folderpath + "filmAjax_xml.php?id=" + whatUID + "&targetpage=" + targetpage + "&time=" + timestamp, true);
		//xmlHttp.open("GET", "http://marginal.dk/fileadmin/template/main/php/filmAjax_xml.php?id=155&targetpage=149&time=1254501069893", true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange =function()
		{
			handleServerResponse();
		}
		
		// make the server request
		xmlHttp.send(null);
	 }		
}

function overList(objS, bClass)
{
	if(objS.className=="nList")
	{
		objS.style.color = "#666666";
	}
	else if(objS.className==bClass)
	{
		objS.className=bClass;
	}
}

function outList(objS, bClass)
{
	if(objS.className=="nList")
	{
		objS.style.color = "#ffffff";
	}
	else if(objS.className==bClass)
	{
		objS.className=bClass;
	}
}


function activateCarousel(carousel)
{
	if(typeof(countSteps)=="undefined")
	{
		countSteps=0;
	}
	
	objCarOuter=document.getElementById(carousel);
	var objUl=objCarOuter.getElementsByTagName("ul")[0];
	
	var objLi=objUl.getElementsByTagName("li");
	var mMax=0;
	if(objLi.length>0)
	{
		oneStep=parseInt(objLi[0].offsetWidth);
		for(var i=0; i<objLi.length; i++)
		{
			mMax +=parseInt(objLi[i].offsetWidth);
		}		
	}
	
	if(typeof(startPos)=="undefined")
	{
	startPos=parseInt(objUl.offsetLeft);
	endPos=startPos-oneStep;
	}
	maxPos=mMax;	
	
	
	if(typeof(interval1)!="undefined")
	{
		window.clearInterval(interval1);
	}
	if(typeof(interval2)!="undefined")
	{
		window.clearInterval(interval2);
	}
	
	interval2=window.setTimeout(function(){startAnimCarousel(objUl, startPos, endPos, maxPos, oneStep);}, 5000);
	
	
}

function startAnimCarousel(objMove, startPos, endPos, maxPos, oneStep)
{
	if(countSteps<0)countSteps=0;
	interval1=window.setInterval(function(){goCarousel(objMove, startPos, endPos, maxPos, oneStep);}, 10);
}



function goCarousel(objMove, startPos, endPos, maxPos, oneStep)
{
	if(typeof(counter)=="undefined")
	{
		counter=objMove.offsetLeft;
	}
	if(endPos>-1*maxPos)
	{
		if(endPos<startPos)
		{
			step = endPos-objMove.offsetLeft;
			counter += step/5;
			
			objMove.style.left=counter + "px";
			if(objMove.offsetLeft<=(endPos+1))
			{
				objMove.style.left=endPos + "px";
				window.clearInterval(interval1);
				countSteps++;
				startPos=endPos;
				endPos=startPos-oneStep;				
				interval2=window.setTimeout(function(){startAnimCarousel(objMove, startPos, endPos, maxPos, oneStep);}, 5000);
			}
		}
		else if(endPos>startPos)
		{
			step = objMove.offsetLeft-endPos;
			counter -= step/5;
			objMove.style.left=counter + "px";
			if(objMove.offsetLeft>=(endPos-1))
			{
				objMove.style.left=endPos + "px";
				window.clearInterval(interval1);				
				//countSteps++;
				startPos=endPos;
				endPos=startPos-oneStep;
				
				
				interval2=window.setTimeout(function(){startAnimCarousel(objMove, startPos, endPos, maxPos, oneStep);}, 5000);
			}
			
		}
		
	
		
	}
	else
	{
		//window.clearInterval(interval1);
		startPos=-1*maxPos;
		endPos=0;
		step = endPos-objMove.offsetLeft;
		
		counter += step/5;
		objMove.style.left=counter + "px";
		
		if(objMove.offsetLeft>=0)
		{
			window.clearInterval(interval1);
			objMove.style.left=0 + "px";
			startPos=0;
			endPos=startPos-oneStep;			
			countSteps=0;		
			
			interval2=window.setTimeout(function(){startAnimCarousel(objMove, startPos, endPos, maxPos, oneStep);}, 5000);
		}
	}
}


function handleServerResponse2(whatTDiv, uidForm, uidCourse) 
{
  if (xmlHttp.readyState == 4) 
  {	   
 	if (xmlHttp.status == 200) 
    {
		var xmlResponse = xmlHttp.responseText;
			
		displayWSFormHere1(uidForm, 1000005, uidCourse);
		
    } 
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}



function handleServerResponse1(whatTDiv) 
{
  if (xmlHttp.readyState == 4) 
  {	   
 	if (xmlHttp.status == 200) 
    {
		var xmlResponse = xmlHttp.responseXML;
		  
		if(!xmlResponse || !xmlResponse.documentElement)
		{
			throw("Invalid XML structure:\n" + xmlHttp.responseText);
		}
		  
		var rootNodeName=xmlResponse.documentElement.nodeName;
		  
		if(rootNodeName == "parserror")
		{
			throw("Invalid XML structure");
		}
	  	xmlRoot=xmlResponse.documentElement;
		
		var titleArray1 = xmlRoot.getElementsByTagName("obj1");
		var helloMessage1=titleArray1.item(0).firstChild.data;
		
		if(whatTDiv)
		{		
			whatTDiv.innerHTML="";
			whatTDiv.innerHTML=helloMessage1;
			whatTDiv.style.display="block";
		}
		
    } 
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function displaySubFormHere(autoStart)
{
	var x=get_cookie("hTF");
	if(typeof(x)!="undefined" && x!=null && x=="1" && autoStart==1)
	{
		return false;
	}

	if(typeof(hideForever)!="undefined" && hideForever==1 && autoStart==1)
	{
		return false;
	}
	if(autoStart==1)
	{
		var params = "&id=" + 198 + "&type=" + 1000002;
	}
	else if(autoStart==0)
	{
		var params = "&id=" + 198 + "&type=" + 1000001;
	}
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
	
	if(!document.getElementById("whatTDiv"))
		{
			var whatTDiv=document.createElement("div");
			whatTDiv.id="whatTDiv";
			whatTDiv.className="whatTDivClass";
			document.getElementById("main").insertBefore(whatTDiv, document.getElementById("main").firstChild);
		}
	
	
	
		var date = new Date();
		var timestamp = date.getTime();		

		xmlHttp.open("POST", "index.php", true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		// define the method to handle server responses
		xmlHttp.onreadystatechange =function()
		{
			handleServerResponse1(whatTDiv);
		}
		
		// make the server request
		xmlHttp.send(params);
	 }		
}

function displayWSFormHere1(uidForm, typeP, uidCourse)
{
	if(typeof(whatTDiv1)=="undefined")
	{
		whatTDiv1=document.getElementById("whatTDiv1");
	}
	var params = "&id=" + uidForm + "&idC=" + uidCourse + "&type=" + typeP;
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
		xmlHttp.open("POST", "index.php", true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		// define the method to handle server responses
		xmlHttp.onreadystatechange =function()
		{
			
				handleServerResponse1(whatTDiv1);			
		}
		
		// make the server request
		xmlHttp.send(params); return false
	}
}


function displayWSFormHere(autoStart, uidCourse, uidForm, formData, typeP)
{
	if(document.getElementById("frmSubscribeCoursestitel"))
	{
			newSubjectData=document.getElementById("frmSubscribeCoursestitel").value;
	}
	if(typeof(formData)!="undefined")
	{
		var formObject = document.getElementById(formData);		
	    
		var formQuery="";
		for(var z=0; z<formObject.elements.length; z++)
		{
			var fObj=formObject.elements[z];
			var fVal=fObj.value;
			var fName=fObj.name;
			var fType=fObj.type;
			switch(fType)
			{
				
				case "text":
				case "textarea":
				case "password":
				case "file":
				case "hidden":	
				case "submit":					
				if(fName!="ajaxPage")
				{
					if(fName=="subject")
					{
						fVal=newSubjectData;
					}
					formQuery +="&" + fName + "=" + escape(fVal);					
				}
				break;
				case "radio":
				case "checkbox":
				var len=fObj.length;
						if (len)	{
							for (a=0;a<len;a++)	{
								if (fObj[a].checked)	{
									formQuery +="&" + fName + "=" + "Ja";
								}
								else
								{
									formQuery +="&" + fName + "=" + "Nej";
								}
							}
						} else {
							if (fObj.checked)	{
								formQuery +="&" + fName + "=" + "Ja";
							}
							else
							{
								formQuery +="&" + fName + "=" + "Nej";
							}
						}
				break;
			}
		}
	}
	
	
	if(autoStart==1)
	{
		typePage=1000003
		var params = "&id=" + uidForm +"&idC=" + uidCourse + "&type=" + typePage;
		if(typeof(uidCourse)=="undefined" || uidCourse.length<1)
		{
			typePage=1000004
		}
		if(typeof(typeP)!="undefined")
		{
			typePage = typeP;
		}
		
		
		
		if(typePage==1000004)
		{
			params = formQuery;
		}
		
	}
	else if(autoStart==0)
	{
		var params = "&id=" + uidForm +"&idC=" + uidCourse + "&type=" + 1000001;
	}
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{	
	
	if(!document.getElementById("whatTDiv1"))
		{
			var whatTDiv1=document.createElement("div");
			whatTDiv1.id="whatTDiv1";
			whatTDiv1.className="whatTDivClass1";
			document.getElementById("main").insertBefore(whatTDiv1, document.getElementById("main").firstChild);
		}
	if(typeof(whatTDiv1)=="undefined")
	{
		whatTDiv1=document.getElementById("whatTDiv1");
	}
	
	
		var date = new Date();
		var timestamp = date.getTime();		
		
		if(typePage==1000004)
		{
				
			
			//xmlHttp.open("POST", "fileadmin/template/main/php/clearPostedVars.php?id=" + uidForm +"&idC=" + uidCourse + "&type=" + typePage, true);
			xmlHttp.open("POST", "index.php?id=" + uidForm +"&idC=" + uidCourse + "&type=" + typePage, true);
		}
		else
		{
			
			xmlHttp.open("POST", "index.php", true);
		}
		
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		
		// define the method to handle server responses
		xmlHttp.onreadystatechange =function()
		{
			if(typePage==1000004)
			{
				handleServerResponse2(whatTDiv1, uidForm, uidCourse);
			}
			else
			{			
				handleServerResponse1(whatTDiv1);
			}
		}
		
		// make the server request
		xmlHttp.send(params); return false
	 }		
}




function hideTDiv(a, divId)
{
	if(document.getElementById(divId))
	{
		document.getElementById(divId).parentNode.removeChild(document.getElementById(divId));
		if(a==1)
		{
			hideForever=1;			
		}
		else if(a==0)
		{
			hideForever=0;
		}
		document.cookie="hTF=1";
	}
}

