//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)

	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	//alert(scrolll);

	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}



	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) ;

	return arrayPageSize;
}

function hideOverlay()
{
	var objOverlay = document.getElementById('overlay2');
	objOverlay.style.display = 'none';
}

function changeOverlaySize() {
	var objOverlay = document.getElementById('overlay2');
	var arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');
}

function showDivWindow(id) {
	var objOverlay = document.getElementById('overlay2');
	var arrayPageSize = getPageSize();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	$(id).style.left = (Math.round(arrayPageSize[0] / 2) - 150) + 'px';

	$(id).style.top = (Math.round(self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)
	)+150) + 'px';



	$(id).appear({ duration: 0.3 });

	var arrayPageSize = getPageSize();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}

function hideDivWindow(id) {
	$(id).fade({ duration: 0.3 });
	setTimeout('hideOverlay()', 450);
}

function submitForm(formId, successText, div) {

	if (!parseInt(document.getElementById('add_quantity').value)) {
		alert('куда?');
		document.getElementById('add_quantity').value='1';
	}
	if (!parseInt(document.getElementById('quantity').innerHTML)) {
		document.getElementById('quantity').innerHTML="0"
	}

	if (!parseInt(document.getElementById('summ').innerHTML)) {
		document.getElementById('summ').innerHTML="0"
	}


	document.getElementById('quantity').innerHTML=parseInt(document.getElementById('quantity').innerHTML)+parseInt(document.getElementById('add_quantity').value);
	document.getElementById('summ').innerHTML=parseInt(document.getElementById('summ').innerHTML)+(parseInt(document.getElementById('add_quantity').value)*parseInt(document.getElementById('h_price').value));
	$(cur).innerHTML = 'руб.'

	var count = parseInt($(quantity).innerHTML);
	if (count==1)
	$(tov).innerHTML='товар,';
	else if (count<=4) {
		$(tov).innerHTML='товара,';
	}
	else {
		$(tov).innerHTML='товаров,';
	}


	$(formId).request({
		onComplete: function(req)  {

			var response = req.responseText;
			var resJSON = response.evalJSON();

			if(resJSON.statusOK) {
				//updateContent('');
				//alert(successText);
				//document.location.href= '/card/';

				hideDivWindow(div);
				$(formId).reset();
			} else {
				alert( "Произошла ошибка:\n" );
			}
		}
		, onFailure: function(req) {
			alert('Some error occured');
		}
	})
}

function showAddCardDiv(id, divId, url) {

	showDivWindow(divId);

	new Ajax.Updater(divId, url, {
		method: 'post'
		, parameters: {
		'id': id
		}
		, onComplete: function (req) {
			changeOverlaySize();
		}

	});


}

function showFFormDiv(id, type, page, divId, url, sec_id) {
	showDivWindow(divId);
	new Ajax.Updater(divId, url, {
		method: 'post'
		, parameters: {
		'id': id,
		'type': type,
		'page': page,
		'sec_id': sec_id

		}
		, onComplete: function (req) {
			changeOverlaySize();
		}

	});
}


function showAuthDiv(divId, url) {
	showDivWindow(divId);
	new Ajax.Updater(divId, url, {
		method: 'post'
		, onComplete: function (req) {
			changeOverlaySize();
		}

	});
}


function updateProductDiv(id, divId, url) {
	showDivWindow(divId);
	new Ajax.Updater(divId, url, {
		method: 'get'
		, parameters: {
		'id': id
		}
		, onComplete: function (req) {
			changeOverlaySize();
		}

	});
}



function submitProductForm(formId, successText, div, fdiv) {
	$(formId).request({
		onComplete: function(req)  {

			var response = req.responseText;
			var resJSON = response.evalJSON();

			if(resJSON.count!=0) {
				//alert(resJSON.count);
				//updateContent('');
				//alert(successText);
				//document.location.href= document.location.href;
				//hideDivWindow(div);
				//$(formId).reset();
				$form_elements=$(formId).serialize();
				$('error').innerHTML='';
				new Ajax.Updater(div, '/search/filter_product.php', {
					method: 'post'
					, parameters: {
					'data': $form_elements
					}
					, onComplete: function (req) {
						//changeOverlaySize();
						hideDivWindow(fdiv);
						//$(formId).reset();
					}

				});



			} else {
				$('error').innerHTML='По вашему запросу ничего не найдено, пожалуйста, попробуйте снова';
			}
		}
		, onFailure: function(req) {
			alert('Some error occured');
		}
	})
}



function submitAuthForm(formId, div) {

	$(formId).request({
		onComplete: function(req)  {

			var response = req.responseText;
			var resJSON = response.evalJSON();

			if(resJSON.statusOK) {
				//updateContent('');
				//alert(successText);
				document.location.href= '/card/home/';
				hideDivWindow(div);
				$(formId).reset();
			} else {
				$('error').innerHTML='Неверный логин или пароль';
			}
		}
		, onFailure: function(req) {
			alert('Some error occured');
		}
	})
}



function getSectionItems(divId, sectorId, url, sectionId, subSectionId) {

	new Ajax.Updater(divId, url, {
		method: 'post'
		, parameters: {
		'sector': sectorId,
		'hsection': sectionId
		}
		, onComplete: function (req) {
			getSubSectionItems('subSectionItems', $('sectionId').options[$('sectionId').selectedIndex].value, '/mod/catalog/select_items.php',  subSectionId);
		}

	});
};

function getSubSectionItems(divId, sectorId, url,  subSectionId) {

	new Ajax.Updater(divId, url, {
		method: 'post'
		, parameters: {
		'section': sectorId,
		'hsubSection':  subSectionId

		}
		, onComplete: function (req) {
			getCatalogItems('catalogItems', $('subSectionId').options[$('subSectionId').selectedIndex].value, '/mod/catalog/catalog_items.php');

		}

	});
};

function getCatalogItems(divId, subSectionId, url) {

	new Ajax.Updater(divId, url, {
		method: 'post'
		, parameters: {
		'subSectionId': subSectionId
		}
		, onComplete: function (req) {

			$('sectorIdh').value=$('sectorId').options[$('sectorId').selectedIndex].value;
			$('sectionIdh').value=$('sectionId').options[$('sectionId').selectedIndex].value;
			$('subSectionIdh').value=$('subSectionId').options[$('subSectionId').selectedIndex].value;

		}

	});
};