/*
Copyright (c) willFarrell 2011
@import 'obj.js';
@import 'localstorage.js';
*/
/*global alert*/
/*global $ getType getName getChecked getValue setInnerHTML getLength*/ // obj.js
/*global xmlreqPOST*/ // ajax.js

// Form Validation Functions //

/*function commentError (obj, state) {
	var _comment = '_comment';
	if(state) $(obj.name+_comment).className='label'; //label or error
	else $(obj.name+_comment).className='error';	
}*/

/*function checkLength(obj, minlength){
	if (obj.value.length < minlength) {commentError(obj, false); return false;}
	else {commentError(obj, true); return true;}
}*/

/*function checkEmail(obj) {
	var str = obj.value,
	filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ///^.+@.+\..{2,3}$/
	
	if(filter.test(str)) {
		commentError(obj, true); 
		return true;
	} else {
		commentError(obj, false);
		return false;
	}		
}

function checkPassword(obj, passName) {
	var pass = $(passName).value;
	if(obj.value == pass) {commentError(obj, true); return true;}
	else {commentError(obj, false); return false;}
}*/

function saveFields(obj, url) {
	
	//obj = obj.parentNode;
	//alert(obj.innerHTML);
	var data = "",
			inputs = $('input', obj),
			selects = $('select', obj),
			fields = $('textarea', obj),
			_amp = '&',
			_equals = '=',
			i, input, field, option
			;
	//		inputs = $('input');
	for (i = inputs.length; i--;) {
		input = inputs[i];
		if (getType(input) !== 'submit') {
			
			//data += '&'+inputs[i].name+'=';
			if (getType(input) === 'checkbox') {
				data += _amp+getName(input)+_equals+getChecked(input);
			} else if (getType(inputs[i]) === 'radio' && getChecked(input)) {
				data += _amp+getName(input)+_equals+getValue(input);
			} else if(getType(input) != 'checkbox' && getType(inputs[i]) != 'radio') { //type == 'text'
				data += _amp+getName(input)+_equals+encodeURIComponent(input.value.replace(
						new RegExp( "\\+", "g" ),
						"%2B"
					));
			}
		} 
	}
	
	// select
	for (i=selects.length; i--;) {
		option = selects[i];
		data += _amp+getName(option)+_equals+getValue(option);
	}
	// textarea
	for (i=fields.length; i--;) {
		field = fields[i];
		data += _amp+getName(field)+_equals+encodeURIComponent(field.value.replace(
					new RegExp( "\\+", "g" ),
					"%2B"
				));
	}
	//data = encodeURI(data);
	//data = data.substr(1, data.length);
	//alert(url+"?"+data);
	//url && xmlreqPOST(url, data);
	if (url) {
		//if(!online) {
		//	var json = ls.getItem('que')?JSON.parse(ls.getItem('que')):[];
		//	json[url] = data;
		//	ls.setItem('que', JSON.stringify(json));
		//} else {
			xmlreqPOST(url, data);
		//}
	}
}

function wordcount(id, count) {
	var words = count.split(/\s/);
	setInnerHTML($(id), getLength(words)); // ID of the count holder
}

function charcount(id, count) {
	setInnerHTML($(id), getLength(count)); // ID of the count holder
}

function commacount(id, count) {
	var words = count.split(/,/);
	setInnerHTML($(id), getLength(words)); // ID of the count holder
}

function uploadImgCheck(obj, form) {
  var ext = getValue(obj);
  ext = ext.substring(getLength(ext)-3,getLength(ext));
  ext = ext.toLowerCase();
  if(ext !== 'png' && ext !== 'jpg' && ext !== 'gif' && ext !== 'bmp') {
    alert('You selected a .'+ext+' file;\n'+
          'please select a png, jpg, gif, or bmp file instead!');
		form.reset();
  } else {
		alert('submitted');
		form.submit();
	}
}



// file upload - update iframe

/*function $(element) {
	if (arguments.length > 1) {
		for (var i = 0, elements = []; i<arguments.length; i++)
			elements.push($(arguments[i]));
		return elements;
	} else {
		element = document.getElementById(element);
		return element;
	}
}*/
function uploadImgCheck($obj, $form) {
	var ext = $obj.value, i = ext.lastIndexOf(".");
	ext = ext.substring(i+1,ext.length);
	ext = ext.toLowerCase();
	if(ext != 'png' && ext != 'jpg' && ext != 'gif' && ext != 'bmp') {
		alert('You selected a .'+ext+' file;\n'+
					'please select a png, jpg, gif, or bmp file instead!');
		$form.reset();
	} else {
		$form.submit();
	}
}

function uploadDocCheck($obj, $form) {
	var ext = $obj.value, i = ext.lastIndexOf(".");
	ext = ext.substring(i+1,ext.length);
	ext = ext.toLowerCase();
	if(ext != 'docx') {
		alert('You selected a .'+ext+' file;\n'+
					'please select a docx file instead!');
		$form.reset();
	} else {
		$form.submit();
	}
}
