$('html').addClass('js');

$(document).ready(function () {
	$.cssJS();
	$.tagClick();
	$.fieldJump();
	$.fieldSearch();     });
	
$(window).load(function () {
	$.actionValid();     });

//GENERIC

jQuery.fn.cssInt = function($attribute) { var $value = parseInt($(this).css($attribute)); if (isNaN($value)) { $value = 0; } return $value; }

jQuery.fn.boxHeight = function () { 
	var $height = $(this).height();
	$height += ($(this).cssInt('border-bottom-width') + $(this).cssInt('border-top-width'));
	$height += ($(this).cssInt('padding-bottom') + $(this).cssInt('padding-top'));
	return $height;     } 

//DIV
		
jQuery.cssJS = function() { 
	var $maxheight = $('div.pagebody:first').boxHeight();
	var $img = $('div.rowimg:first');
	if ($img.length > 0) {
		var $label = $('div.rowlabel:first');
		var $imgheight = $img.boxHeight();
		var $labelheight = $label.boxHeight();
		if ($labelheight < $imgheight) { 
			var $padding = ($imgheight - $labelheight) / 2;
			var $paddingtop = Math.floor($padding) + 'px';
			var $paddingbottom = Math.ceil($padding) + 10 + 'px';
			$label.css({'padding-top': $paddingtop, 'padding-bottom': $paddingbottom});
			$labelheight = $label.boxHeight();     }
		if ($labelheight < $maxheight) {	
			var $margintop = Math.floor(($maxheight - $labelheight) / 2) - 5 + 'px';
			$label.css({'margin-top': $margintop}); }     }     }
		
jQuery.tagClick = function() { 
	$('.click').click(function() { 
		var $a = $(this).find('a:first');
		var $href = $a.attr('href');
		if ($a.attr('target') == '_blank') { window.open($href); }
		else { window.location = $href; }     });     }		
		
//FORM

jQuery.fieldJump = function () { $('select.fieldjump').change(function() { $(this).parents('form').submit(); }); }
	
jQuery.fieldSearch = function () {
	$('select.fieldsearch').each(function() {
		if ($(this).val()) { $selected = $(this).children('option:selected'); } else { $selected = $(this).children('option:first'); }
		$(this).children('option:first').remove();
		$(this).wrap('<div class="cell cellsearch"></div>');
		$(this).after('<span>' + $selected.text() + '</span>');
		$(this).css({'opacity':0});     });     }

jQuery.actionValid = function ($location, $scroll, $open) { 
	$('form').submit(function() { 
		$(this).find('span.messageerror').remove();
		$input = $(this).find('input, select, textarea');
		var $valid = true;
		$input.each(function() { 
			var $error = false;
			var $value = $(this).val();
			if ($(this).is('.fieldreq') && !$(this).fieldValid('REQUIRED')) { $error = 'Required'; }
			else if ($(this).is('.fieldemail') && !$(this).fieldValid('EMAIL')) { $error = 'Invalid email'; }
			else if ($(this).is('.fieldpassword') && !$(this).fieldValid('PASSWORD')) { $error = 'Invalid password'; }
			else if ($(this).is('.fieldpasswordmatch') && !$(this).fieldValid('PASSWORDMATCH')) { $error = 'Typing error'; }
			else if ($(this).is('.fieldurl') && !$(this).fieldValid('URL')) { $error = 'Invalid URL'; }
			if ($error) { $(this).parents('div.row:first').append('<span class="message messageerror">' + $error + '</span>'); $valid = false; }     }); //stop submit
		if ($valid && $(this).parents('div.objecttxtbox').length == 1) { $(this).winClose('this_open'); }
		return $valid;     });     }
		
jQuery.fn.fieldValid = function ($type) {
	var $value = $(this).val();
	if ($type == 'REQUIRED' && !$value) { return false; }
	else if ($type == 'EMAIL') { var $pattern = /^[\w.+-]+@[\w.-]+\.[a-z]{2,6}$/; return $pattern.test($value); }
	else if ($type == 'PASSWORD') { var $pattern = /^[\w-]{4,20}$/; return $pattern.test($value); }
	else if ($type == 'PASSWORDMATCH') { $match = $(this).prevAll('input.fieldpassword:first').val(); if ($value == $match) { return true; } else { return false; } }
	else if ($type == 'URL' && $value) { if ($value.substr(0,4) == 'www.') { $value = 'http://' + $value; } var $pattern = /^(http|https):\/\/[\w-.]+[\w.]{2,6}/; return $pattern.test($value); } 
	else { return true; }    }
