// common functions
String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}



// check for form changes
var mac = navigator.appVersion.indexOf("Mac")!=-1;
var changeMade=false;
function formChange(){
	if(!changeMade)
		changeMade=true;
}
function checkSave(){
	if(changeMade){
		if(confirm('You have made changes to this page.\nWould you like to save your progress?')){
			document.mainForm.finalSub.value="N";
			document.mainForm.submit();
		}
	}
}
// URL redirect
function checkURLSave(URL){
	if(mac){
		if(changeMade){
			if(confirm('You have made changes to this page.\nWould you like to save your progress?')){
				document.mainForm.finalSub.value="N";
				document.mainForm.submit();
			}else{
				location.href=URL;
			}
		}else{
			location.href=URL;
		}
	}else{
		location.href=URL;
	}
}
// final submit
function submitFinalPaper(){
	document.mainForm.finalSub.value="Y";
	if(confirmMe()){
		document.mainForm.submit();
	}
}		
// page submit
function submitPage(){
	document.mainForm.finalSub.value="N";
	if(confirmMe()){
		document.mainForm.submit();
	}
}	
//also for the state save change prompt
if(!mac)
	window.onunload=checkSave;




function sure() {
	return confirm('Are you sure you want to delete this entry?');
}



// jquery functions 
$(document).ready(function() {
	if ($.browser.msie) {
		$('input:radio').click(function () {
			this.blur();
			this.focus();
		});
		$('input:checkbox').click(function () {
			this.blur();
			this.focus();
		});
	}
	
	if ($('.instructionsPopup').length > 0) {
		$('.instructionsPopup').cluetip();
	}
	if ($('.instructionsPopupWide').length > 0) {
		$('.instructionsPopupWide').cluetip({width: '700px'});
	}
	
	//maxlength
	$('textarea[maxlength]').keyup(function(){
		var limit = parseInt($(this).attr('maxlength'));
		var text = $(this).val();
		var chars = text.length;
		if(chars > limit){
			var new_text = text.substr(0, limit);
		$(this).val(new_text);
	}
	var newText = (limit - chars) + " characters left.";
		$(this).next('.maxChar').text(newText);
	});
	
	$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
	
});

