var checks;
var bc = getCookie("browserContinue");
var detectionLocation = "http://www.i-studentgroup.com/includes/libs/php/i-studentgroup/";
if(pubType == null){
	var pubType = "undefined";
}

baseUrl = baseUrl == null ? "/" : baseUrl;

if(bc == null || bc == "true"){
	checks = ["browser","flash"];
} else {
	checks = ["flash"];
}

var checkedCount = 0;
var errorList = [];
var flashPluginVersion;	
var requiredFlashVersion;
var postData;
var xmlHttp;
var lightboxContent;

document.onload = pageLoadDetection();

function pageLoadDetection(){
	if(document.body != null) {
		checkForCookies();
	} else {
		setTimeout(function(){pageLoadDetection()},100);	
	}
}

function checkForCookies(){
	if(getCookie("cookiesChecked") == null){
		if(getCookie("cookieDetection") != null){
			setCookie("cookiesChecked","true");
		} else {
			errorList.push("cookie");
		}
	}
	requestDetection(checks[checkedCount]);	
}

function requestDetection(check){
	var sendData = check == "flash" ? "domain="+baseUrl+"&pubType="+pubType+"&target="+detectionLocation+"detection/"+check+"Detection.php&method=POST&data=&pageurl="+encodeURIComponent(document.documentElement.innerHTML)
								: "domain="+baseUrl+"&pubType="+pubType+"&target="+detectionLocation+"detection/"+check+"Detection.php&method=GET&data=";
								
	var onSuccess = check == "flash" ? flashResponse : browserResponse;
	
	jQueryDetection.ajax({
		type: "POST",
		url: baseUrl+"includes/php/xhrTransport.php",
		data: sendData,
		complete: onSuccess
	});	 
}

browserResponse = function(response) {	
	if(response.responseText == "browserNotAllowed") errorList.push("browser");
	checkProgress();
}

flashResponse = function(response) {
	if(response.responseText.indexOf("flashRequired") > -1){
		if (window.ActiveXObject) {
			var control = null;
			try {
				control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			} catch (e) {
				errorList.push("flash");
			}
			if (control) {
				var verPos = response.responseText.indexOf("Ver");
				if(verPos > -1) {	  
					flashPluginVersion = control.GetVariable('$version').substring(4);
					flashPluginVersion = flashPluginVersion.split(',');
					flashPluginVersion = parseInt(flashPluginVersion[0] + '.' + flashPluginVersion[1]);
					requiredFlashVersion = parseInt(response.responseText.substring(verPos+3));
					if(requiredFlashVersion > flashPluginVersion){
						errorList.push("flash");				
					}
				}
			}
		} else {
			var flashPlugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
			if(!flashPlugin){
				errorList.push("flash");	
			} else {
				var verPos = response.responseText.indexOf("Ver");
				if(verPos > -1) {
					var plugin = navigator.plugins["Shockwave Flash"].description.split(" ");
					for (var i = 0; i < plugin.length; ++i) {
						if (isNaN(parseInt(plugin[i]))) continue;
						flashPluginVersion = plugin[i]; 
					}
					requiredFlashVersion = parseInt(response.responseText.substring(verPos+3));
					if(requiredFlashVersion > flashPluginVersion){
						errorList.push("flash");				
					}
				}	
			}
		}
	}
	checkProgress();
}

function checkProgress() {	
	checkedCount++;	
	if(checkedCount < checks.length){	
		requestDetection(checks[checkedCount]);				
	} else if(checkedCount == checks.length) {
		requestWarnings();
	}	
}

function requestWarnings(){
	var errorOutput = "";
	for(var k=0; k < errorList.length; k++){
		if(errorOutput != "") errorOutput += "<hr />";
		var sendData = "domain="+baseUrl+"&pubType="+pubType+"&target="+detectionLocation+"warnings/"+errorList[k]+"Warning.php&method=GET&data="+encodeURIComponent("pubLanguage="+pubLanguage);
		if (errorList[k] == "flash") sendData += encodeURIComponent("&ver="+requiredFlashVersion);
		errorOutput += jQueryDetection.ajax({
							type: "POST",
							url: baseUrl+"includes/php/xhrTransport.php",
							data: sendData,
							async: false
						}).responseText;
		if(errorList[k] == "browser" && in_array(errorList, "cookie") == -1){
			errorOutput += '<p><input type="checkbox" name="hideMessage" id="browserHideMessage" onclick="browserContinue(\'input\')" /><a onclick="browserContinue(\'link\')">'+helpFormHide+'</a></p>';
		}
	}
	
	if(errorOutput != ""){
		errorOutput += '<hr /><p><a onclick="showWarningForm()">'+helpFormClick+'</a></p>';			
		displayLightbox(errorOutput);
	}
}

function browserContinue(obj){
	var cb = jQueryDetection("#browserHideMessage");
	
	if(obj == "link")
	{
		if(jQueryDetection("#browserHideMessage").is(':checked'))
		{
			jQueryDetection("#browserHideMessage").removeAttr('checked');
		} 
		else
		{
			jQueryDetection("#browserHideMessage").attr('checked','checked');
		}
	}
	
	if(jQueryDetection("#browserHideMessage").is(':checked')){
		setCookie("browserContinue","false",-1);
	} else {
		setCookie("browserContinue","true",0);	
	}
}

function showWarningForm(){
	var errors = "";
	for(i=0; i<errorList.length;i++){
		errors += errorList[i]+",";
	}
	errors = errors.substring(0, errors.length-1);
	lightboxContent = jQueryDetection("#boxContent").html();	
	
	var sendData = "domain="+baseUrl+"&pubType="+pubType+"&target="+detectionLocation+"warnings/warningForm.php&method=GET&data="+encodeURIComponent("pubLanguage="+pubLanguage+"&er="+errors);
	
	swapLightboxContent(jQueryDetection.ajax({
							type: "POST",
							url: baseUrl+"includes/php/xhrTransport.php",
							data: sendData,
							async: false
						}).responseText,279);
	
	jQueryDetection("#warningFormflashVersion").val(flashPluginVersion == null ? "not installed": flashPluginVersion);	
	addKeyPressEvents();
}

function hideWarningForm(){	
	swapLightboxContent(lightboxContent);	
}

function warningFormSend(){
	if(checkForm()){
		var formData = "";
		var elems = document.warningForm.getElementsByTagName("input");
		for(i=0; i<elems.length;i++){
			if(elems[i].type != "button"){
				formData += elems[i].name+"="+elems[i].value+"&";
			}
		}
		
		var sendData = "domain="+baseUrl+"&pubType="+pubType+"&target="+detectionLocation+"warnings/warningFormProcess.php&method=POST&data="+encodeURIComponent("pubLanguage="+pubLanguage+"&websiteAddress="+document.location.href+"&"+formData);
		jQueryDetection.ajax({
			type: "POST",
			url: baseUrl+"includes/php/xhrTransport.php",
			data: sendData,
			complete: warningFormResponse
		});	
	}
}

var warningFormResponse = function(response){
	if(response.responseText.indexOf("@@SUCCESS") > -1){
		var output = response.responseText.replace("@@SUCCESS", "");
		jQueryDetection("#boxContent").html(output);
	} else {
		var output = response.responseText.replace("@@ERROR", "");
		jQueryDetection("#formError").css('display','block');
		jQueryDetection("#formError").html(output);
	}		
	fitLightboxToContent();
}

function addKeyPressEvents(){
	var inps = document.warningForm.getElementsByTagName("input");
	for(i=0;i<inps.length;i++){
		if(inps[i].type == "text"){
			inps[i].setAttribute("onkeyup","warningKeyPress(this, event)");	
			inps[i].setAttribute("onblur","warningKeyPress(this, event)");
		}
	}
}

function warningKeyPress(obj, e){
	addValidityIndicator(obj);	
	
	if (e.keyCode == 13) {
		warningFormSend();
		return false;
	}
	return true;
}

function in_array(array, val){
	for(i=0; i<array.length; i++){
		if(array[i] == val){
			return i;	
		}
	}
	return -1;
}
