/***** cookie's functions from: http://www.toutjavascript.com/savoir/savoir02.php3 *****/
if(typeof(jQuery) != 'undefined')
{
$(document).ready(function(){
	var topBody = $(window).scrollTop();
	var topBodyPlus = topBody;//+($(window).width()*0.01);
	$("#Survey").css({'top':topBodyPlus+'px'});
	
	
	$(window).scroll(function(){
		var topScroll = $(window).scrollTop();
		var topScrollPlus = topScroll;//+($(window).width()*0.01);
		$("#Survey").css({'top':topScrollPlus+'px'});
	});
});
}



function setCookie(name, value)
{
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie =   name + "=" + escape(value) +
                        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                        ((path == null) ? "" : ("; path="+path)) +
                        ((domain == null) ? "" : ("; domain=" + domain)) +
                        ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function getCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}
function DeleteCookie() {
 if(uniqueId)
 {
	var name = uniqueId;
	var exp=new Date();
	exp.setTime (exp.getTime() - 100000);
	var cval=getCookie (name);
	if(cval == 'over')
	{
		document.cookie=name+"="+cval+"; expires="+exp.toGMTString();
	}
 }
} 
/***** differed callback *****/

function differCallback(timeout,country, callback, interval)
{
	var uniqueId ='';
	if(typeof(jQuery) != 'undefined')
	{
		if (!interval) {
			// Check elapsed time every: 
			interval = 1000;
		}

		// Unique identifier for the differCallback() call.
		uniqueId = 'dc_elapsed_' + timeout + '_' + callback.length + '_' + interval;
	    
		var elapsed = getCookie(uniqueId);
		if (elapsed == 'over') {
			// The timeout is over.
			return;
		} else if (elapsed == null) {
			// This is the first call of the function.
			elapsed = 0;
		}
	   
		elapsed = parseInt(elapsed);
		if (elapsed >= timeout) {
			if (callback()) {
				// Cookie'll never be null again.
				// The next call should set elapsed to interval.
				
				setCookie(uniqueId, 0,null,"/"+country+"/" );
			} else {
				
				setCookie(uniqueId, 'over',null,"/"+country+"/");
			}
			return;
		} else {
			// After setTimeout, interval'll be elapsed.
	        
			setCookie(uniqueId, elapsed + interval,null,"/"+country+"/");
		}
		setTimeout('differCallback(' + timeout+ ', "' + country + '", ' + callback + ', ' + interval + ');', interval);
    }
    return uniqueId;
}

function OpenSurveyLayer()
{
	$("a.opensurvey").click();
	return false;
}

function showQuestions()
{
	$("#SurveyIntro").hide();
	$("#SurveyQuestions").fadeIn("slow");
}
function sendAndClose()
{
	var params = new Array();
		params["participant"] = "false";
		params["Answer1"] = "";
		params["Answer2"] = "";
		params["Answer3"] = "";
		params["Answer4"] = "";
		params["Answer5"] = "";
		
		Ajax_Execute(location.pathname, "InsertSurvey", params, closeSurvey);
}
function closeSurvey()
{
	$("#popup_survey>div>.jqmClose").click();
}

function SendAnswer()
{
	if($("input[name*='rblAnswer']:checked").length == 4)
	{
		var params = new Array();
		params["participant"] = "true";
		params["Answer1"] = encodeURI($("input[name*='rblAnswer1']:checked").parent().next().children('label').text());
		params["Answer2"] = encodeURI($("input[name*='rblAnswer2']:checked").parent().next().children('label').text());
		params["Answer3"] = encodeURI($("input[name*='rblAnswer3']:checked").parent().next().children('label').text());
		params["Answer4"] = encodeURI($("input[name*='rblAnswer4']:checked").parent().next().children('label').text());
		params["Answer5"] = encodeURI($("textarea[id*='tbAnswer5']").val());
		
		Ajax_Execute(location.pathname, "InsertSurvey", params, SendAnswerCallBack);
	}
	else
	{
		alert($("#SurveyRequieredFieldValue").val());
	}
	
}

function SendAnswerCallBack(response)
{
	var statusCode = response["statuscode"];
	if(statusCode == "SUCCESSFUL")
	{
		$("#SurveyQuestions").hide();
		$("#SurveySended").fadeIn("slow");
		$(".SurClose").attr("href","javascript:closeSurvey()");
	}
}
