var demGen = false;

function createCookie(name, value, days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name+"=";
	var ca = document.cookie.split(';');
	for (var i = 0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return true;
		}
	}
	return false;
}


function getQueryVariable(variable){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			if (pair[1] == "option1") {
				pair[1] = "General Demos";
				url = "general_demos.html";
			}else if (pair[1] == "option2") {
				pair[1] = "Anti-Money Laundering Demo- Introduction to Money Laundering";
				url = "Content/Courses/aml/jap_aml1/jap_aml1.html";
				width = 800;
				height = 600;
			}else if (pair[1] == "option3") {
				pair[1] = "Privacy Protection";
				url = "Content/Courses/privacy_protection/module1/mod_1.html";
				width = 800;
				height = 600;
			}else if (pair[1] == "option4") {
				pair[1] = "Code of Conduct";
				url = "Content/Courses/coc/CCC_Chapter_1/JAP_CCC_1.html";
				width = 800;
				height = 600;
			}else if (pair[1] == "option5") {
				pair[1] = "Information Security";
				url = "Content/Courses/info_sec/KP_infosec__courseoverview.html";
				width = 800;
				height = 600;
			}
			return pair[1];
		}
	}
}

function openWin(){
	createCookie('KnowledgePlatform', 'KnowledgePlatform', 365);
	if (getQueryVariable("option") == "General Demos") {
		demGen = true;
		
		document.location.href = url;
		}else {
		window.open(url, '', 'top=30,left=100,toolbar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
		//getQueryVariable("option")
	}
}
function checkCondition(){
	if (readCookie("KnowledgePlatform")) {
		document.location.href = "demos_list.html";
	}else {
			
	}
}
function checkCook(lnk){
	if (readCookie("KnowledgePlatform")) {
		switch (lnk){
		case "opt1":
		url = "general_demos.html";
		break;

		case "opt2":
		url = "Content/Courses/aml/jap_aml1/jap_aml1.html";
		width = 800;
		height = 600;
		break;
		
		case "opt3":
		url = "Content/Courses/privacy_protection/module1/mod_1.html"
		width = 800;
		height = 600;
		break;

		case "opt4":
		url = "Content/Courses/coc/CCC_Chapter_1/JAP_CCC_1.html";
		width = 800;
		height = 600;
		break;
		
		case "opt5":
		url = "Content/Courses/info_sec/KP_infosec__courseoverview.html";
		width = 800;
		height = 600;
		break;
		}
		if (lnk == "opt1") {
			location.replace(url);
			}else {
				window.open(url, '', 'top=30,left=100,toolbar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
			}
	
	}else {
		switch (lnk){
		case "opt1":
		url = "form.html?option=option1";
		break;

		case "opt2":
		url = "form.html?option=option2";
		break;
		
		case "opt3":
		url = "form.html?option=option3";
		break;

		case "opt4":
		url = "form.html?option=option4";
		break;
		
		case "opt5":
		url = "form.html?option=option5";
		break;
		}		
		window.location.href=url;
		//location.replace(url);
	}

}

function trim (str) {
	  while (str.charAt(0) == ' ')
		str = str.substring(1);
	  while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
	  return str;
}

function validate(){

	if(trim(document.presentationForm.first_name.value) == ""){
		document.presentationForm.first_name.value = ""
		alert("Please enter your First Name")
		document.presentationForm.first_name.focus();
		return false;
		}else if(trim(document.presentationForm.last_name.value) == ""){
			document.presentationForm.last_name.value = ""
			alert("Please enter your Last Name")
		document.presentationForm.last_name.focus()
		return false;
			}else if(document.presentationForm.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
			alert("Please enter your Valid Email address")
		document.presentationForm.email.focus()
		return false;
			}else if(trim(document.presentationForm.company.value) == ""){
				document.presentationForm.company.value = ""
			alert("Please enter your Company Name")
		document.presentationForm.company.focus()
		return false;
		}else if(document.presentationForm.lead_source.value == ""){
			alert("Please Provide the lead from where you have heard about Knowledge Platform")
		document.presentationForm.lead_source.focus()
		return false;
	}else{
		openWin();
		redirectPage();
		//return true;
	}
}

function getRedirectPage(){
	var ref=window.location.href+"";	
	var redirectPageName;
	ref=ref.toLowerCase();
	var offsetStr="?option=";
	var opt=ref.substring(ref.indexOf(offsetStr)+8);	
	switch (opt){
		case "option1":
			redirectPageName="general_demos.html";
			break;
		case "option2":
			redirectPageName="content_expertise_aml.html";
			break;
		case "option3":
			redirectPageName="content_expertise_privacy.html";
			break;
		case "option4":
			redirectPageName="content_expertise_coc.html";
			break;
		case "option5":
			redirectPageName="content_expertise_Is.html";
			break;
	}	
	return redirectPageName;
}