var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function NewWindow(mypage,myname,w,h,scroll){
	var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	var win = window.open(mypage,myname,settings);
	win.focus();
}
function calcTime(){
	var mypage = 'local_time.php';
	var myname = 'time';
	var w = 514;
	var h = 372;
	var scroll = 'no';
	var LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	var settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	var win = window.open(mypage,myname,settings);
	win.focus();
}

function jump(x){
	x=x.value;
	$(x).scrollTo();
}

function clearForm(thisform){
	thisform.reset();
}

function submitContactForm() {
	var form = document.getElementById('commentform');
	fVals = "";
	if(validate_contactform(form)){
		//alert("form is good to submit");
		form.submit();
	}
}

function createCookie(name, email, country) {
	if ( name != '' && email != '' && country != '') {
		var date = new Date();
		date.setTime(date.getTime()+(24*60*60*300000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = "pangea=" + name + "|" + email + "|" + country + "|" + expires + "; path=/";
	}
}


function getCookieInfo(form) {	
	var position=document.cookie.indexOf("pangea=") + 7;
	var val = document.cookie.substring(position);
	var valArray = val.split('|');

	if ( document.cookie.indexOf('pangea=') > -1 ) { 

		form.elements['author'].value = unescape(valArray[0]);
		form.elements['email'].value = unescape(valArray[1]);	
		form.elements['country'].value = unescape(valArray[2]);
		document.getElementById('welcome').style.display = 'block';
		document.getElementById('welcome_user_name').innerHTML = valArray[0];
		document.getElementById('loginSection').style.display = 'none';			
		document.getElementById('verificationSection').style.display = 'none';	
		document.getElementById('commentArea').style.display = 'block';
		document.getElementById('commentpostresult').style.display = 'none';
	}
}

function submitCommentForm(x) {
	var form = $(x);
	fVals = "&country=" + form.country.value;
	var allNodes = form.serialize(); //alert(allNodes); return false;

	if(validate_contactform(form)){
		createCookie(form.author.value, form.email.value, form.country.value);
		//getContent('post_comment_success.php', allNodes, 'commentpostresult');
		

new Ajax.Request("post_comment_success.php",
{
	method:'post',
	parameters: allNodes,
	onSuccess: function(request){				
		if (request.responseText == 'success') {
			window.parent.location.reload(false);
		} else {
			form.comment.value = '';
			form.parent_id.value = '';
			document.getElementById('parentCommentContent').innerHTML = '';
			document.getElementById('commentpostresult').style.display = 'block';	
			document.getElementById('commentpostresult').innerHTML = request.responseText;	
			document.getElementById('commentform').style.display = 'none';
		}
	},
	onFailure: function(){ 
		console.log("TOTAL failure");
	}
});
	} else {
		//alert('please check your submission and try again.');
		return false;
	}								

}

function submitmailListForm() {
	document.getElementById('mailListFormError').style.display = "none";
	var form = document.getElementById('mailListForm');
	fVals = "";
	var numErrors = 0;
	with (form) {		
		if (validate_required(email)==false) {numErrors++;}
		else if (!(email.value.match(/^.+@.+\..+$/))){numErrors++;}
	}
	if(numErrors > 0){
		document.getElementById('mailListFormError').style.display="block";
		form.email.focus();
	}
	else {form.submit();}
				
}

//Returns false if a field is null or empty, else true
function validate_required(field) {
	with (field)
	{
		if (value==null||value=="")
		{return false}
		else {return true}
	}
}

//Checks that all required fields on the page have been filled in and that all formatted fields are in their proper forms
function validate_contactform(thisform) {
	errors = "";
	var form = thisform;
	clearContentErrorDivs();
	var errArray = new Array();
	var numErrors = 0;
	with (thisform) {

		if (validate_required(author)==false) {			
			errArray[numErrors] = "authorLabel";
			numErrors++;
		} else { fVals += '&author=' + (form.author.value); }
		
		if (validate_required(country)==false) {			
			errArray[numErrors] = "countryLabel";
			numErrors++;
		} else { fVals += '&country=' + (form.country.value); }
		
		if (validate_required(email)==false) {			
			errArray[numErrors] = "emailLabel";
			numErrors++;
		}
		else if (!(email.value.match(/^.+@.+\..+$/)))
		{			
			errArray[numErrors] = "emailLabel";
			numErrors++;
		} else { fVals += '&email=' + (form.email.value); }		
		
		if (validate_required(comment)==false) {			
			errArray[numErrors] = "commentLabel";
			numErrors++;
		} else { fVals += '&comment=' + (form.comment.value); }

		if(numErrors > 0){
			document.getElementById('errorMsg').style.display = 'block';			
			for(var i=0;i<errArray.length;i++){
				var tmp = errArray[i];
				document.getElementById(tmp).className += " form_error";
			}

			return false;	
		} else {
		return true;
		}
	}
}

function clearContentErrorDivs(){
	var errorDivs = new Array(
	"authorLabel",
	"countryLabel",
	"emailLabel",
	"verificationLabel",
	"commentLabel");
	for(var i=0;i<errorDivs.length;i++){
		tmp = errorDivs[i];
		document.getElementById(tmp).className = document.getElementById(tmp).className.replace("form_error", '');
	}					
	document.getElementById('errorMsg').style.display = 'none';
}

function getCurrentTime() {
	return (new Date()).getTime();
}


function launchStream(){
	NewWindow('http://www.pangeaday.org/pangeaday/index.html','live',460,430,'no');
}

BrowserDetect.init();
