/* 
 *
 * 1001 Genomes Data Policy Check Audit 
 *
 * written 2010 by Joffrey Fitz
 * 
 */

var container_div;
var stored_content = "";


function init() {
	container_div = document.getElementById("content_container");
	stored_content = container_div.innerHTML;

	if( test_location() ) {
		if(! test_cookie() ) {
			show_policy();
		}
	}
}

function test_location() {
//	if(window.location.href.match(/\/MPICao2010\/releases\//) or window.location.href.match(/\/MPISchneeberger2011\/releases\//)) { 
//	alert(window.location.href);
	if(
// 		   window.location.href.match(/\/MPICao2010\/releases\//) 
		window.location.href.match(/\/MPIcollab2011\/releases\//) 
		|| window.location.href.match(/\/Salk\/releases\//)
		|| window.location.href.match(/\/GMINordborg2010\/releases\//)
		|| window.location.href.match(/\/JGIHeazlewood2011\/releases\//)
		|| window.location.href.match(/\/JGIHeazlewood2008\/releases\//)
	) {
		return 1;
	}
	else {
		return 0;
	}
}

function test_cookie() {
	if (document.cookie == "1001Genomes=DataCenterAccess") {
		//alert("cookie" + document.cookie);
// 		alert("got cookie");
		return 1;
	} else {
  		return 0;
	}
}

function set_cookie() {
// 	alert("set_cookie()");
	var expire_date = new Date();
//	var period = expire_date.getTime() + (24 * 60 * 60 * 1000); // 24 hours
// 	var period = expire_date.getTime() + ( 1 * 60 * 1000); // 1 min
	var period = expire_date.getTime() + (1 * 60 * 60 * 1000); // 1h
	expire_date.setTime(period);
	document.cookie = "1001Genomes=DataCenterAccess; expires=" + expire_date.toGMTString();
	if( document.cookie == "" ) {
		alert("Please enable cookies to remember your Data Usage Policy agreement.");
	}
}

function show_policy() {
	var policyfile = "policy.html"; // default MPI policy
	if( window.location.href.match(/\/Salk\/releases\//) ) {
			policyfile = "policy_salk.html";
	}
	if( window.location.href.match(/\/GMINordborg2010\/releases\//) ) { 
            policyfile = "policy_gmi.html";
    }
	if( window.location.href.match(/\/MPIcollab2011\/releases\//) ) { 
            policyfile = "policy_MPI_MPIZ.html";
    }
	if( window.location.href.match(/\/JGIHeazlewood2011\/releases\//) ) {
			policyfile = "policy_JGI.html";
	}
	if( window.location.href.match(/\/JGIHeazlewood2008\/releases\//) ) {
			policyfile = "policy_JGI.html";
	}
	new Ajax.Updater('content_container', '/layout_files/data_policy/' + policyfile, { method: 'get' });
}

function enableButton() {
	cb = document.getElementById('yesido_cb');
	b  = document.getElementById('agree_b');
	if(cb.checked)
		{b.disabled=false}
	else {b.disabled=true}
}

function showData() {
	set_cookie();
	container_div.innerHTML = stored_content;
}


