/*
 Exam._loadCustomVars = function() { return { action: "exam", namespace: $("namespace").value	}; };
 */
function Exam() { }

Exam.load = function()
{
	if (!Exam.url) { 		return; 	}	if (Exam._preHook) {        Exam._preHook();    }
	ajaxCaller.get(Exam.url, Exam._loadVars(), Exam._onResponse, false, "a get request");
}

Exam.setLoadVars = function(handler)
{
    Exam._loadVars = handler;
};

Exam._onResponse = function (text, headers, callingContext) {
  if (Exam._postHook) {      Exam._postHook();  }
  
  var resultBlock = $("ExamContainer");
  resultBlock.innerHTML = text;			 //+ "<textarea style=\"width:250px;height:300px;\">"+text+"///Cookie ? "+ Exam.getCookieKey() +"</textarea>";
  
  if (headers["AJAXOnLoad"])	//Header in response for faking onload event
	eval(headers["AJAXOnLoad"]);
 }
 
/**
 * Setup a GMail style loading message.
 * @see http://www.getahead.ltd.uk/dwr/util-general.html
 */
Exam.useLoadingMessage = function()
{
    var disabledZone = document.createElement('div');
    disabledZone.setAttribute('id', 'disabledZone');
    disabledZone.style.position = "absolute";
    disabledZone.style.zIndex = "1000";
    disabledZone.style.left = "0px";
    disabledZone.style.top = "0px";
    disabledZone.style.width = "100%";
    disabledZone.style.height = "100%";
    document.body.appendChild(disabledZone);

    var messageZone = document.createElement('div');
    messageZone.setAttribute('id', 'messageZone');
    messageZone.style.position = "absolute";
    messageZone.style.top = "0px";
    messageZone.style.right = "0px";
    messageZone.style.background = "red";
    messageZone.style.color = "white";
    messageZone.style.fontFamily = "Arial,Helvetica,sans-serif";
    messageZone.style.padding = "4px";
    disabledZone.appendChild(messageZone);

    var text = document.createTextNode('Loading');
    messageZone.appendChild(text);

    $('disabledZone').style.visibility = 'hidden';

    Exam.setPreHook(function() { $('disabledZone').style.visibility = 'visible'; });
    Exam.setPostHook(function() { $('disabledZone').style.visibility = 'hidden'; });
};

Exam.url = null;

Exam.setPreHook = function(handler)
{
    Exam._preHook = handler;
};

Exam.setPostHook = function(handler)
{
    Exam._postHook = handler;
};

Exam._postHook = null;
Exam._preHook = null;

Exam.getRadioValue = function(field)
{
	if (field)
	{
		for (i=0;i<field.length;i++){
			if (field[i].checked==true){
				return field[i].value
				break //exist for loop, as target acquired.
			}
		}
	}
	else
	{
		return '';
	}
}

Exam.getCookieKey = function() {
	var key = Exam._getCookie("ExamKey");
	return (key)?key:"";
}

Exam.getCookiePassed = function() {
	var passed = Exam._getCookie("ExamPassed");
	return (passed)?passed:0;
}

Exam.setCookieKey = function(ExamKey) {
	var expr = new Date();
	expr = new Date(expr.getTime() + 365*24*60*60*1000);
	Exam._setCookie("ExamKey", ExamKey, expr, "/", null, 0);
}

Exam.setCookiePassed = function() {
	var expr = new Date();
	expr = new Date(expr.getTime() + 365*24*60*60*1000);
	Exam._setCookie("ExamPassed", 1, expr, "/", null, 0);
}

Exam.resetCookies = function() {
	Exam._deleteCookie("ExamKey");
	Exam._deleteCookie("ExamPassed");
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
Exam._setCookie = function(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
Exam._getCookie = function(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
Exam._deleteCookie = function(name, path, domain) {
    if (Exam._getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


/* initialize Exam */
//Exam.useLoadingMessage();
Exam.url = "/exam/ajax.aspx";
Exam.setLoadVars(function() { return {namespace: $("namespace").value } } );


/*
 * Toggle the visual display style 
 *
 * name  Id of the element.
 */
function Togglehide(szElementId)
{
	if (document.getElementById(szElementId))
	{
		if (document.getElementById(szElementId).style.display == "none") {
			document.getElementById(szElementId).style.display = "inline";
		}else{
			document.getElementById(szElementId).style.display = "none";
		}
	}
}

function validated()
{	
	eval("document.getElementById('KlarTilTilmelding').style.display = \"inline\";document.getElementById('ManglerTest').style.display = \"none\";");
}
