var reqCatPages;
var status_message = new Array(
"The query is not initialized",
"The query is created",
"The query is sent",
"The query is processed"
);
var clickNum = 0;

var curDay = new Date().getDay();
var numPagesPerView = 5;
if(curDay == 0 || curDay == 5) { numPagesPerView = 4; }
if(curDay == 6) { numPagesPerView = 3; }

function is_blank(str) {
	if(/\S+/.test(str))
	{
		return false;
	} else {
		return true;
	}
}

function check_form(the_form, form_fields, form_fields_names) {
	alert_string = "";
	focus_element = null;

	nom = 0;
	for (i = 0; i < form_fields.length; i++) {
		if (("checkbox" == the_form[form_fields[i]].type && false == the_form[form_fields[i]].checked) ||
		is_blank(the_form[form_fields[i]].value))
		{
			alert_string += ++nom + ". " + form_fields_names[i] + "\n";
			if (null == focus_element) focus_element = the_form[form_fields[i]];
		}
	}

	if ("" != alert_string) {
		alert(alert_string);
		focus_element.focus();
		return false;
	}

	return true;
}

function hide() {
	for(var i = 0; i < arguments.length; i++) {
		var element = document.getElementById(arguments[i]);
		if (element) {
			element.style.display = 'none';
		}
	}
}

function show() {
	for(var i = 0; i < arguments.length; i++) {
		var element = document.getElementById(arguments[i]);
		if (element) {
			element.style.display = 'block';
		}
	}
}

function addBookmark(title, url, false_location) {
	if (navigator.userAgent.indexOf("Opera") != -1) {
		return false;
	}
	else if (window.sidebar) {
		return false;
	}
	else if (typeof window.external == "object")
	{
		window.external.AddFavorite(url, title);
		return true;
	}

	location = false_location;
	return false;
}

function get_url(url) {
	if (session_append_sid) {
		if (-1 == url.indexOf('?')) {
			url += '?';
		} else {
			url += '&';
		}
		url += session_append_sid;
	}

	return url;
}

function popupWindow(url) {
	window.open(url,'show_image','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=300,height=300,screenX=50,screenY=50,top=50,left=50');
}

function randomNumber(m, n) {
	m = parseInt(m);
	n = parseInt(n);
	return Math.floor( Math.random() * (n - m + 1) ) + m;
}

function loadCatPage(catId, pageId) {
	clickNum = clickNum + 1;
	//if (clickNum % 5 == 0) {
	if (clickNum % numPagesPerView == 0) {
		return true;
	}

	url = document.location.protocol + '//' + document.location.hostname + '/ajax_catalogs.php?cat=' + parseInt(catId) + '&page=' + parseInt(pageId);
	if (window.XMLHttpRequest) {
		reqCatPages = new XMLHttpRequest();
		reqCatPages.onreadystatechange = processLoadCatPage;
		reqCatPages.open("GET", url, true);
		reqCatPages.send(null);

		show('loading_cat_page');
		return false;

	} else if (window.ActiveXObject) {
		reqCatPages = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqCatPages) {
			reqCatPages.onreadystatechange = processLoadCatPage;
			reqCatPages.open("GET", url, true);
			reqCatPages.send();

			show('loading_cat_page');
			return false;
		}
	}

	return true;
}

function processLoadCatPage() {
	if (reqCatPages.readyState == 4) {
		if (reqCatPages.status == 200) {
			var xmlDoc = reqCatPages.responseXML.documentElement;
			document.getElementById('cat_page_header').innerHTML = xmlDoc.getElementsByTagName("cat_page_header")[0].childNodes[0].nodeValue;
			document.getElementById('cat_page_image').innerHTML = xmlDoc.getElementsByTagName("cat_page_image")[0].childNodes[0].nodeValue;
			document.getElementById('cat_page_products').innerHTML = xmlDoc.getElementsByTagName("cat_page_products")[0].childNodes[0].nodeValue;
			if (xmlDoc.getElementsByTagName("cat_id")[0]) {
				if (xmlDoc.getElementsByTagName("cat_page_prev")[0]) {
					document.getElementById('page_prev').innerHTML = '<a href="'+xmlDoc.getElementsByTagName("cat_page_u_prev")[0].childNodes[0].nodeValue+'" onclick="return loadCatPage('+xmlDoc.getElementsByTagName("cat_id")[0].childNodes[0].nodeValue+', '+xmlDoc.getElementsByTagName("cat_page_prev")[0].childNodes[0].nodeValue+');"><img src="/images/prev.gif" width=32 height=32 border=0></a>';
				} else {
					document.getElementById('page_prev').innerHTML = '';
				}
				if (xmlDoc.getElementsByTagName("cat_page_next")[0]) {
					document.getElementById('page_next').innerHTML = '<a href="'+xmlDoc.getElementsByTagName("cat_page_u_next")[0].childNodes[0].nodeValue+'" onclick="return loadCatPage('+xmlDoc.getElementsByTagName("cat_id")[0].childNodes[0].nodeValue+', '+xmlDoc.getElementsByTagName("cat_page_next")[0].childNodes[0].nodeValue+');"><img src="/images/next.gif" width=32 height=32 border=0></a>';
				} else {
					document.getElementById('page_next').innerHTML = '';
				}
			} else {
				document.getElementById('page_prev').innerHTML = '';
				document.getElementById('page_next').innerHTML = '';
			}

		} else {
			var element_status = document.getElementById('loading_cat_page');
			if (element_status) { element_status.innerHTML = "Error " + reqCatPages.status + "<br />" + reqCatPages.statusText; }
		}

	} else {
		var element_status = document.getElementById('loading_cat_page');
		if (element_status) {
			image_loading = document.location.protocol + '//' + document.location.hostname + '/images/loading2.gif';
			element_status.innerHTML = '<img src="' + image_loading + '" width="150" height="15" border="0" alt="" title="' + status_message[reqCatPages.readyState] + '" />';
		}
	}
}

function copy(obj) {
	obj.focus();
	obj.select();
}

function gen_link_codes(link_url, link_name) {
	var element_block = document.getElementById('link_codes');
	if (element_block) {
		link_url = document.location.protocol + '//' + document.location.hostname + link_url;
		element_block.innerHTML = '<table border="0" cellpadding="0" cellspacing="2" class="simpleTable" style="margin-top:5px; color:gray;">'+
		'<tr><td><strong>HTML-код:</strong></td><td><input type="text" size="70" onFocus="copy(this);" value=\'<a href="'+link_url+'" target="_blank">'+link_name+'</a>\' /></td></tr>'+
		'<tr><td><strong>Для форумов:</strong></td><td><input type="text" size="70" onFocus="copy(this);" value="[URL='+link_url+']'+link_name+'[/URL]" /></td></tr>'+
		'<tr><td><strong>Прямая ссылка:</strong></td><td><input type="text" size="70" onFocus="copy(this);" value="'+link_url+'" /></td></tr>'+
		'</table>';
	}
}
