var iFails = 0;
var focus_name = '';

function $(v) {
	return document.getElementById(v);
}

function create_object(v) {
	if ( typeof(v) != 'object' ) {
		return $(v);
	}
	return v;
}

function ch(box) {
	box = create_object(box);
	
	return box.checked;
}
		
function in_array(string, array) {
	for (var index in array){
		if (array[index] == string){
			return true;
		}
	}
	return false;
}

/*
deprecated, used validate()
*/
function OnFormSubmit(formObj) {
	iFails = 0;

	for (var i in check_fields) {

		name = check_fields[i];

		if ( $(name).value == '' || in_array(name, email_fields) && isEmail($(name).value) == false || $(name).type == 'select-one' && $(name).value == '-') {	
			markError($(name));
		}
	}

	if ( typeof(extra_checks) != 'undefined') {

		for ( var j in extra_checks ) {
			func = extra_checks[j];
			if ( window[func]() == false ) {
				iFails++;
			}
		}
	}

	if (iFails != 0) {

		if ( focus_name != '' ) {
			$( focus_name ).focus();
			
			markError($(focus_name));
			
			focus_name = '';
		}
		
		focus_error();
		return false;
	}
	return true;
}

function validate() {
	//remove errors in case this is re-submit
	clear_errors();
	
	for (var i in check_fields) {

		name = check_fields[i];

		if ( $(name).value == '' || in_array(name, email_fields) && isEmail($(name).value) == false || $(name).type == 'select-one' && $(name).value == '-') {	
			markError($(name));
		}
	}

	if ( typeof(extra_checks) != 'undefined') {

		for ( var j in extra_checks ) {
			func = extra_checks[j];
			window[func]();
		}
	}

	aErrors = getElementsByClassName(document, "*", "error");
	errors = aErrors.length;
	
	if (errors != 0) {

		if ( focus_name != '' ) {
			$( focus_name ).focus();
			
			markError($(focus_name));
			
			focus_name = '';
		}

		ScrollToElement( aErrors[0] );
		return false;
	}
	return true;
}

function checkboxesChecked(arr){
	for (var iBox in arr) {
		try {
			if ($(arr[iBox]).checked == true) {
				return true;
			}
		}
		catch(err){}
	}
	return false;
}

/**
* Deprecated, use checkboxes() instead
*/
function validateCheckboxes(arr, marker){
	if (checkboxesChecked(arr) == false) {
		markError( $(marker) );
		return false;
	}
	return true;
}

function checkboxes() {
	arr = [];
	for ( i = 0; i < arguments.length; i++ ) {
		arr[arr.length] = arguments[i];
	}
	
	if (checkboxesChecked(arr) == false) {
		auto_markError($(arr[0]));
		return false;
	}
	return true;
}

function handleInput(selfObj) {
	if (selfObj.value != '') {
		removeClassName(selfObj.parentNode.parentNode, 'error');
	}
}

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
		return true;
	}
	else {
		return false;
	}
}

/*
Deprecated, use auto_markError instead.
*/
function markError(obj) {
	
	if (typeof(obj) != 'object'){

	}
	else {
		addClassName(obj.parentNode.parentNode, 'error', true);

		iFails++;

		if (focus_name == '') {
			focus_name = obj.id;
		}
		//ScrollToElement(obj);
	}
}

function auto_markError(ob) {
	if (ob != null){
		if ( ob.parentNode.tagName == 'LI' ) {
			addClassName(ob.parentNode, 'error', true);
			//ScrollToElement(ob.parentNode);
		}
		else {
			auto_markError(ob.parentNode);
		}
	}
}

function auto_removeError(ob) {
	cont = 0;
	if ( ob != null ) {
		//has parent li node?
		if ( ob.parentNode != null && ob.parentNode.tagName == 'LI' ) {
			obj = ob.parentNode;
			cont = 1;
		}
		else {
			obj = ob;
		}
		
		removeClassName( obj, 'error' );
		
		if ( cont == 1 ) {
			auto_removeError( obj );
		}
	}
}

function removeError(obj){
	removeClassName(obj.parentNode.parentNode, "error");
}

//http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html
function ScrollToElement(theElement){

	var selectedPosX = 0;
	var selectedPosY = 0;
      
	while(theElement != null){
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}

	window.scrollTo(selectedPosX,selectedPosY);
}


function wufocus(obj){
	clearSafariRadios();
	addClassName(obj.parentNode.parentNode, "focused", true);
}

function wublur(obj){
	removeClassName(obj.parentNode.parentNode, "focused");
}

function removeNonDigits(val){
	iReturn = val.replace(/[^0-9]/g, '');
	return iReturn;
}

function formatPhone(objtextbox){
	exp = /[^\d]/g;
	objtextbox.value = '+' + objtextbox.value.replace(exp,'');
}

function show() {
	
	for ( i = 0; i < arguments.length; i++ ) {
		
		obj = arguments[i];
		
		if ( typeof( obj ) == 'string' ){
			obj = $( obj );
		}
		
		obj.style.display = 'block';
	}
}

function hide() {
	
	for ( i = 0; i < arguments.length; i++ ) {
		
		obj = arguments[i];
		
		if ( typeof( obj ) == 'string' ){
			obj = $( obj );
		}
		
		obj.style.display = 'none';
		
		//solves wufoo .error bug
		auto_removeError( obj );
	}
}

function IsNumeric(sText) {
	ValidChars = "0123456789.";
	
	if (sText.length == 0){
		IsNumber = false;
	}
	else {
		IsNumber = true;
	}

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		
		Char = sText.charAt(i); 
		
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function forms_reset() {
	document.getElementsByTagName('input')[0].focus();
	document.getElementsByTagName('form')[0].reset();
	
	clear_errors();
}

function clear_errors() {
	fields = getElementsByClassName(document, "*", "error");
	for(i = 0; i < fields.length; i++) {
		removeClassName(fields[i], 'error');
	}
}

function boxes() {
	
	for ( i = 0; i < arguments.length; i++ ) {
		checkbox_validation( arguments[i] );
	}
}

function checkbox_validation(fieldName) {
	
	if (arguments.length > 1) {
		num = arguments[1];
	}
	else {
		num = 0;
	}
	
	field = fieldName + '_' + num;
	
	next = num + 1;

	if ( $(field) != null) {
		
		// radio not checked, is there a next radio?
		nextnum = num + 1;
	
		if ( $(fieldName + '_' + nextnum) != null ) {
			last_field = 0;
		}
		else last_field = 1;
		
		
		if ( last_field == 1 && $(fieldName + '_other') != null ) {

			//last radio for txt selected, but there's no txt
			if ( $(fieldName + '_other').value.length == 0 ) {
				auto_markError( $(fieldName + '_0') );
				return false;
			}
			else {
				$(field).checked = true;
			}
		}
		else if ( $(field).checked == true ) {
			return true;
		}
		else if ( last_field == 0 ) {
			
			//great, there's a next radio, continue recursion
			checkbox_validation(fieldName, nextnum);
		}
		else {
			
			//no field found, radio group is invalid
			auto_markError( $(fieldName + '_0') );
			return false;
		}
	}
}

function fixName(name) {
	return ucwords(name.toLowerCase());
}

function ucwords(str) {
    // Uppercase the first character of every word in a string  
    // 
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/ucwords    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Waldo Malqui Silva
    // +   bugfixed by: Onno Marsman
    // +   improved by: Robin
    // *     example 1: ucwords('kevin van zonneveld');    // *     returns 1: 'Kevin Van Zonneveld'
    // *     example 2: ucwords('HELLO WORLD');
    // *     returns 2: 'HELLO WORLD'
    return (str + '').replace(/^(.)|\s(.)/g, function ($1) {
        return $1.toUpperCase();    });
}

function autoname(field) {
	$(field).value = fixName( $(field).value );
}

function focus_error() {
	errors = getElementsByClassName( document, "*", "error" );
	ScrollToElement( errors[0] );
}
