

//jQuery.fn.fixAjaxSubmitButton = function() {
//  $(this).append('<input class="emulate_submit" type="hidden" name="" value="" />');
//  $(':submit', $(this)).each(function(){
//      $(this).click(function(){
//          $('.emulate_submit').attr('name', this.name).attr('value', this.value)
//      })
//  })
//  return this;
//};

jQuery.fn.alternateRowColors = function(){
	$("tbody tr[class!='header']:visible:odd").removeClass("two").addClass("one");
	$("tbody tr[class!='header']:visible:even").removeClass("one").addClass("two");
	return this;
}

function getClientHeight(){
  return (document.compatMode=='CSS1Compat' && !window.opera)?document.documentElement.clientHeight:document.body.clientHeight;
}

function checkForm(obj) {
	var element, pattern, value;
	for (var i = 0; i < obj.elements.length; i++) { // пробегаемся по всем элементам формы
		element = obj.elements[i];
		value = jQuery.trim(element.value);
		// И только если есть чего говорить юзеру в случае ошибки
		if (!element.getAttribute("alt")) continue;
		if (element.getAttribute("type") == 'button') continue;
		
		if (value == 'Поиск...'){
			alert(element.getAttribute("alt"));
			element.focus();
			return false;
		}		
		
		if (pattern = element.getAttribute("src")) { // если задан рег
			pattern = new RegExp(pattern, "gm");
			if (!pattern.test(value)) {
				alert(element.getAttribute("alt"));
				element.focus();
				return false;
			}
		} 
		else {
				if (value == "") { // иначе просто проверка что поле не пустое
				alert(element.getAttribute("alt"));
				element.focus();
				return false;
			}
		} 
	}
	return true;
}


// Проверка формы регистрации
function checkFormReg(obj){
	var password_one = document.getElementById('password_one').value;
	var password_two = document.getElementById('password_two').value;	
	if (password_one != password_two) {
		alert('Пароль и подтверждение пароля должны совпадать!');
		return false;
	}
	return checkForm(obj);
}

// установка куки
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "; path=/") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}
// получение куки
function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
// удаление куки
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// общая функция подготовки формы к отправке
function showRequestForm(formData, jqForm, options) {
	if (checkForm(jqForm[0])){
		$("body").animate({opacity: 0}, 100);
		return true;
	}
	else
		return false;
} 

// общая функция обработки ответа после отправки формы
function showResponseForm(responseText, statusText)  { 
	if ((responseText != '') && (responseText != '<pre></pre>') && (responseText != '<PRE></PRE>') && (responseText != 'true')){
		pattern = new RegExp("^ERROR$.*", "gm");
		if (/ОШИБКА/.test(responseText)){
			backgroundColor = '#000';
			color = '#f00';
		}
		else{
			backgroundColor = '#C1B3E7';
			color = '#306376';
		}
		
		$.blockUI({ 
			message: responseText,
			css: { 
				border: 'none', 
				padding: '15px', 
				fontSize: '20pt',
				backgroundColor: backgroundColor, 
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: '.8', 
				color: color 
			},
			timeout: 2500,
			onUnblock: function() { 
				$("body").animate({opacity: 1}, 100);
			} 
		})				
	}
}
