//Javascript Document
$(window).load(function(){
	$("input[type=text]").focus(function(){
		clearTextInput(this);
	}).blur(function(){
		checkInputField(this);
	})
	
	$("textarea").focus(function(){
		clearTextInput(this);
	}).blur(function(){
		checkInputField(this);
	})
});
//usage: form input text element onfocus="clearTextInput(this);" | Clears the form element of any default text and stores that text in a txt property
function clearTextInput(Input){
	if(!Input.txt)
		Input.txt = Input.value;
	if(Input.txt == Input.value)
		Input.value = "";
}
//usage: form input text element onblur="checkInputField(this)"; | Checks the form element and replaces it with default text if the field was left empty by the user
function checkInputField(Input){
	if(Input.value == "" || Input.value == Input.txt+" is Required")
		Input.value = Input.txt;
}
//setup variables and randomly pick 2 numbers between 1 and 10 to use a captcha device
var aa = Math.ceil(Math.random() * 10);
var bb = Math.ceil(Math.random() * 10);
var cc = aa + bb;
var isCaptcha = false;
//outputs the html for the simple match captcha
function DrawBotBoot(){
	isCaptcha = true;
	///document.write("<label for='BotBootInput'>What is "+ aa + " + " + bb +"? </label><span class='captcha'></span>");
	document.write("What is "+ aa + " + " + bb +"?&nbsp;&nbsp;&nbsp;&nbsp;<input id='BotBootInput' onkeypress='return onlyNumbers(event)' class='fancy' style='width:24px' maxlength='2' type='text' size='2' value=' '/>");
}
//a function that validates the captcha element. Run this on submit or check it in any submit function.
function ValidBotBoot(){
	var dd = document.getElementById('BotBootInput').value;
	if (dd == cc) return true;
	document.getElementById('BotBootInput').value = "What is "+ aa + " + " + bb +"?";
	return false;
}

//email/string validation functions compressed to save space. I don't really need to see them all out.
function Validate_String(string){valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';invalid_chars = '';if(string == null || string == '')return(true);for(index = 0; index < string.length; index++){char = string.substr(index, 1);if(valid_chars.indexOf(char) == -1){if(invalid_chars.indexOf(char) == -1){if(invalid_chars == '')invalid_chars += char;else invalid_chars += ', ' + char;}}}   }
function Validate_Email_Address(email_address){at = email_address.indexOf('@');dot = email_address.lastIndexOf('.');if(at == -1 || dot == -1 || dot <= at + 1 || dot == 0 || dot == email_address.length - 1)return false;user_name = email_address.substr(0, at);
domain_name = email_address.substr(at + 1, email_address.length);if(Validate_String(user_name) === false || Validate_String(domain_name) === false)return false;   return true;}

function validate(Form, extra){//requires jquery
	var num = Form.length;
	var err = false;
	if(extra)
		err = complyForm(Form);
	
	
	for(i=0;i<num;i++){
		if($(Form[i]).hasClass("required")){
			if( !Form[i].txt || Form[i].value == Form[i].txt || Form[i].value == Form[i].txt+" is Required" || Form[i].value == "Invalid Email" ){
				if($(Form[i]).hasClass("select")){
					if(!Form[i].value){
						err = true;
						$(Form[i]).before("<div class='selectError error"+i+"'>Please select a valid "+$(Form[i]).attr("name")+"</div>");
						$(".selectError").hover(function(){
							$(this).animate({opacity:0},600);
						})
					}
					continue;
				}
				
				if(!Form[i].txt)
					Form[i].txt = Form[i].value;
				
				Form[i].value = Form[i].txt+" is Required"; 
				
				$(Form[i]).addClass("error");
				$(Form[i]).bind("focus", function(e){
					$(this).removeClass("error");
					$(this).attr({"value":""});
				});
				err = true;
			}
		}
		if($(Form[i]).hasClass("email")){
			if(!Validate_Email_Address(Form[i].value)){
				Form[i].value = "Invalid Email";
				err = true;
			}
		}
	}
	if(isCaptcha)
		if( ValidBotBoot() == false ){ 
			alert("Please tell us what "+ aa + " + " + bb +" is. This is a device to help keep spam down.");
			err = true;
		}
	if(err)
	return false;
	else
	return true;
}

function stripslashes (str) {
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // +   input by: Rick Waldron
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
    return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':
                return '\0';

            case '':
                return '';
            default:
                return n1;
        }
    });
}

function ConfirmDelete(){
	if( confirm("Are you sure you want to delete this?\n\nOK=Yes - Cancel=No") ){
		return true;
	}else{
		return false;
	}
}

function onlyNumbers(e){
	var keynum
	var keychar
	var numcheck

	if(window.event){// IE
		keynum = e.keyCode;
	}
	else if(e.which){// Netscape/Firefox/Opera
		keynum = e.which;
	}
	var character = String.fromCharCode(keynum);
	keychar = String.fromCharCode(keynum)
	numcheck = /[^0-9.,]/;//restrict to only numbers
	if (!e.ctrlKey && keynum!=9 && keynum!=8 && (keynum!=39 || (keynum==39 && character=="'")) && keynum){
		return !numcheck.test(keychar);
	}
	return true;
}