var SITE_ROOT = 'http://www.schoolnursenews.org/login/';

//clears and replaces text in form input fields and textareas
function clearText(thefield,defaultvalue) {
  if (defaultvalue==thefield.value) { thefield.value = ""; }
}
function replaceText(thefield,defaultvalue) {
  if (thefield.value=="") { thefield.value = defaultvalue; }
}


$(document).ready(function() { 
	//if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"]) {
	//	window.location="ie6.php"; }	
	//$(function(){
	//	$('a.newWin').click(function(){
	//		window.open(this.href);
	//		return false;
	//	});
	//});
	//jQuery('#side ul').accordion({event: 'mouseover', active: '.on',}); 
 });

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) {
			return pair[1];
		}
	}
}

function getQueryString(key)
{
    var re = new RegExp( "[?&]" + key + "=([^&$]*)", "i" );
    var offset = location.search.search( re );
    if ( offset == -1 ) return null;
    return RegExp.$1;
}

function showResult(data, redirectPageName, headerform)
{
	//alert(redirectPageName);
	if(headerform) {
		$('#errorMsgHeader').slideUp();
		$("#errorMsgHeader").html('');
	} else {
		$('#errorMsg').slideUp();
		$("#errorMsg").html('');
	}
	if(data[0].result=="success")
	{
        $("#processing").hide();
    	
    	if(redirectPageName=='') {
    		$("#btnSubmit").show();
    		clearErrors();
    		$.scrollTo($('#scrollPosition'),800);
    		$("#thankyouMsg").slideDown('slow',function(){
    			setTimeout(function(){ $("#thankyouMsg").slideUp("slow"); }, 3000);
    		});
    	} else {
    		resetForm();
    		window.location=SITE_ROOT+redirectPageName;
    	}
	}
	else
	{    
		$("#processing").hide();
		showErrors(data,headerform);
	}
}

function showErrors(data,headerform)
{
	clearErrors();
	
	//loop through the JSON array and display the errors
	$.each(data[0], function(key,value) {                    
		if(key != "result")
		{
			if(value==1)
			{
				$('label[for='+key+']').parent().addClass("error");
				$('#'+key).parent().addClass("error");
			}
			else
			{
				$('label[for='+key+']').parent().addClass("error");
				$('#'+key).parent().addClass("error");
				if(headerform) {
					$("#errorMsgHeader").html($("#errorMsgHeader").html()+value);
					$("#errorMsgHeader").slideDown();
				} else {
					$("#errorMsg").html($("#errorMsg").html()+value);
					$("#errorMsg").slideDown();
				}
			}						
		}
	});
	$.scrollTo($('#scrollPosition'),800);
	if(headerform) {
		$('#login_btnSubmit').show();
	} else {
		$('#btnSubmit').show();
	}
	
}

function resetForm()
{
	$('[type="text"]').val("");
	$('textarea').val("");
	$('.form2col, .form1col').children('div').each(function(index){
        $(this).removeClass("error");
    });
}

function submitenter(myfield,submitFunction,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		eval(submitFunction)();
		return false;
	}
	else return true;
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Cyanide_7 |  */
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele) found = true;
		else index++;
		return found;
	}
	
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	
	return true;
}

function confirmDelete(delUrl,itemid,title,deleteType) {
	if (confirm("Are you sure you want to delete the "+deleteType+" called "+title+"?")) {
		$.post(SITE_ROOT+delUrl,
			{ 
				operation: 'deleteItem', 
				id: itemid
			},
			function(data){
				gridReload();
			}, 
			"json"
		);
	}
}

function confirmDeleteMultiple(delUrl,items,deleteType) {
	if (confirm("Are you sure you want to delete the selected "+deleteType+"(s)?")) {
		$.post(SITE_ROOT+delUrl,
			{ 
				operation: 'deleteItem', 
				id: items
			},
			function(data){
				gridReload();
			}, 
			"json"
		);
	}
}

//Google Analytics Code


function clearErrors()
{
	$('.form2col, .form1col').children('div').each(function(index){
        $(this).removeClass("error");
    });
	$('.form2col, .form1col').children('div').children('div').each(function(index){
        $(this).removeClass("error");
    });
	$("#imgErrors").html('');
	$("#imgErrors").hide();
	$('#errorMsgHeader').slideUp();
	$("#errorMsgHeader").html('');
	$('#errorMsg').slideUp();
	$("#errorMsg").html('');
}

// function getCharsRemaining(fieldID,numChars)
//{
//	$('#'+fieldID).val($('#'+fieldID).val().substring(0, numChars));
//	$('#'+fieldID+'_chars').html((parseInt(numChars)-$('#'+fieldID).val().length));
//}


