function getAjaxObject(){
	try{
		var xmlhttp = new XMLHttpRequest();		
	}catch(err1){
		var ieXmlHttpVersions = new Array();
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

		var i;
		for (i=0; i < ieXmlHttpVersions.length; i++){
			try{
				var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
				break;
			}catch (err2){
				
			}
		}
	}
	return xmlhttp;
}


function AjaxLoad(link, elementid){
	var element = document.getElementById(elementid);
	if( !element )
		return;
	var AjaxObject = getAjaxObject();
	if(typeof AjaxObject == "undefined"){
		alert('In order to view this page your browser has to support AJAX')
		return;
	}
	AjaxObject.open("GET", link, true);
	AjaxObject.send(null);
	AjaxObject.onreadystatechange=function() {
		if (AjaxObject.readyState==4 && AjaxObject.status==200){
			element.innerHTML = AjaxObject.responseText;
		}
	}
	return;
}

function getVintageData(pVintageId){
	var lCurVintage = document.getElementById('active_vintage_id');
	if( lCurVintage.value == pVintageId )
		return;
	var lElement = document.getElementById('wine_vintage_details');
	if( !lElement )
		return;
	var lAjaxObject = getAjaxObject();
	var lLink = '/lib/getVintageDetails.php?vintage_id=' + pVintageId;
	if(typeof lAjaxObject == "undefined"){
		alert('In order to view this page your browser has to support AJAX')
		return;
	}
	lAjaxObject.open("GET", lLink, true);
	lAjaxObject.send(null);
	lAjaxObject.onreadystatechange=function() {
		if (lAjaxObject.readyState==4 && lAjaxObject.status==200){
			lElement.innerHTML = lAjaxObject.responseText;
			lCurVintage.value = pVintageId;
			changeVintageLinks(pVintageId);
		}
	}
	return;
}

function changeVintageLinks(pVintageId){
	var lElement = document.getElementById('wine_vintages');
	if( !lElement )
		return;
	var lLinks = lElement.getElementsByTagName('a');
	for( i = 0; i < lLinks.length; ++i){
		
		if( lLinks[i].id == 'vintage_' + pVintageId ){
			lLinks[i].className = 'active_vintage_link'
		}else{
			lLinks[i].className = '';
		}
	}
}

function reloadCaptcha() {
	var img = document.getElementById('cappic');
	img.src = '/lib/frmcaptcha.php?rld=' + Math.random();
	return false;
}

function rldContent(pId, pVal) {
	var lElement = document.getElementById(pId);	
	if(!lElement) {
		return;	
	}
	if (lElement.value == pVal) {
		lElement.value = '';
	}
}

function rldContent2(pId, pVal) {
	var lElement = document.getElementById(pId);
	if(!lElement){
		return;	
	}
	if (lElement.value == '') {
		lElement.value = pVal;
		lElement.className = '';
	}
}

function rldChangeClass (pId) {
	var lElement = document.getElementById(pId);
	if(!lElement) {
		return;
	} else {
		lElement.className = 'typeval';
	}
}

function CheckLoginForm(frm, uname, upass) {
	if (frm.uname.value == uname) {
		frm.uname.value = '';
	}
	
	if (frm.upass.value == upass) {
		frm.upass.value = '';
	}

	return true;

}

function pollsubmit(p,t,cid) {
	var http_request = getAjaxObject();
	if (!http_request) return true;
	
	disablepollbuttons(p);
	
	http_request.onreadystatechange = function() { poll_submit_callback(http_request,cid); };
	
	var qry = generatepollquery(p);
	
	var lmethod = 'GET';
	
	http_request.open(lmethod, '/lib/poll_submit.php?type=' + t + '&' + (lmethod == 'GET' ? qry : ''), true);
	if (lmethod == 'POST') http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(lmethod == 'GET' ? null : qry);
	
	return false;
}

function pollsubmitleft(p,t,cid) {
	var http_request = getAjaxObject();
	if (!http_request) return true;
	
	disablepollbuttons(p);
	
	http_request.onreadystatechange = function() { poll_submit_callback(http_request,cid); };
	
	var qry = generatepollquery(p);
	
	var lmethod = 'GET';
	
	http_request.open(lmethod, '/lib/poll_submit_left.php?type=' + t + '&' + (lmethod == 'GET' ? qry : ''), true);
	if (lmethod == 'POST') http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(lmethod == 'GET' ? null : qry);
	
	return false;
}

var lastsubmitbut = '';
var lastsubmitval = '';

function poll_btnclick(b) {
	lastsubmitbut = b.name;
	lastsubmitval = b.value;
	return true;
}

function generatepollquery(f) {
	var retstr = "";
	for (var i = 0; i < f.elements.length; i++) {
		if (f.elements[i].type.toLowerCase() == 'text' || f.elements[i].type.toLowerCase() == 'textarea' 
			|| f.elements[i].type.toLowerCase() == 'hidden') {
			retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'submit') {
			if (f.elements[i].name == lastsubmitbut && f.elements[i].value == lastsubmitval) retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'select') {
			retstr += f.elements[i].name + "=" + escape(f.elements[i].options[f.elements[i].selectedIndex]) + "&";
		} else if (f.elements[i].type.toLowerCase() == 'radio' || f.elements[i].type.toLowerCase() == 'checkbox') {
			if (f.elements[i].checked) retstr += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
		}
	}
	return retstr;
}

function poll_submit_callback(p,cid) {
	if (p.readyState == 4 && p.status == 200) {
		var canketa = document.getElementById(cid);
		canketa.innerHTML = p.responseText;
		return;
	}
}

function disablepollbuttons(p) {
	for(i=0; i < p.elements.length; i++) {
		if (p.elements[i].type.toLowerCase() == 'submit') {
			p.elements[i].disabled = true;
		}
	}
}

var changed = 0;
function changeUnderline(name, op) {
	var el = document.getElementById(name);	
	// mouse over
	if (op == 1) {
		if (el.className == 'bottom-nobg') {
			el.className = 'bottom-bg'
			changed = 1;
		}
	// mouse out
	} else if (op == 2) {
		if (el.className == 'bottom-bg' && changed == 1) {
			el.className = 'bottom-nobg';
			changed = 0;
		}
	}	
}

function ChangeUndelineTopMenu(pDiv, pClassHide, pClassShow) {
	var divaboutus = document.getElementById('header-bl');
	var divportfolio = document.getElementById('header-bm');
	var divnews = document.getElementById('header-br');

	if(pDiv == 1) {
		divaboutus.className = pClassShow;
		divportfolio.className = pClassHide;
		divnews.className = pClassHide;
	} else if(pDiv == 2) {
		divaboutus.className = pClassHide;
		divportfolio.className = pClassShow;
		divnews.className = pClassHide;		
	} else if(pDiv == 3) {
		divaboutus.className = pClassHide;
		divportfolio.className = pClassHide;
		divnews.className = pClassShow;		
	}
}

function showHiddenDictData(dictid){
	var link = document.getElementById('filtermorelink_' + dictid);
	var hidelink = document.getElementById('filterhidelink_' + dictid);
	var hiddendata = document.getElementById('filtermore_' + dictid);

	
	if(hiddendata){
		hiddendata.style.display = 'block';
	}
	if(link){
		link.style.display = 'none';
	}
	if(hidelink){
		hidelink.style.display = 'block';
	}
}

function hideHiddenDictData(dictid){
	var link = document.getElementById('filtermorelink_' + dictid);
	var hidelink = document.getElementById('filterhidelink_' + dictid);
	var hiddendata = document.getElementById('filtermore_' + dictid);

	
	if(hiddendata){
		hiddendata.style.display = 'none';
	}
	if(link){
		link.style.display = 'block';
	}
	if(hidelink){
		hidelink.style.display = 'none';
	}
}

function showFilterCategories(pCat_id){
	var lDiv = document.getElementById('category_' + pCat_id);
	if( !lDiv )
		return
	var lArrow = document.getElementById('arrow_' + pCat_id);
	if( lDiv.style.display == 'none' ){
		lDiv.style.display = 'block';
		if(lArrow)
			lArrow.className = 'arrow_down';
	}else{
		lDiv.style.display = 'none';
		if(lArrow)
			lArrow.className = 'arrow';
	}
}

function popUp(img, name, width, height) {
	var opened = false;
	var win;
   	if (opened == false) {
		win = open(img,name,"status=0,scrollbars=1,menubar=0,toolbar=0,location=0,resizeable=0,width=" + width + ",height=" + height + ",left=" + ((screen.width-width)/2) + ",top=" + ((screen.height-height)/2));
	} else if (opened == true) {
		if (win.closed == false)
		win.close(); 
		win = window.open(img,name,"status=0,scrollbars=1,menubar=0,toolbar=0,location=0,resizeable=0,width=" + width + ",height=" + height + ",left=" + ((screen.width-width)/2) + ",top=" + ((screen.height-height)/2));
	}
	opened = true;
}

function initScrollConstants(){
	gHolderDiv = document.getElementById('scrollHolderDiv');
	gScrolDiv = document.getElementById('scrolDiv');
	gRightBtn = document.getElementById('scrollRightBtn');
	gLeftBtn = document.getElementById('scrollLeftBtn');
	gLabelDiv = document.getElementById('pathLabel');
	gHolderDiv.style.width = (622 - gLabelDiv.offsetWidth) + 'px';
	
	gHolderWidth = gHolderDiv.offsetWidth;	
	gScrolWidth = gScrolDiv.offsetWidth;
		
	gMinLeft = gHolderWidth - gScrolWidth;
	if(gMinLeft > 0)
		gMinLeft = 0;
	gCurrentLeftPos = gMinLeft;
	
	gInterval = null;
	
	changeDivPosition(gCurrentLeftPos);
}
function scrollDivRight(){	
	if( gMinLeft >= 0 ){
		stopScroll()
		return;
	}
	if( gCurrentLeftPos - 1 > gMinLeft )
		gCurrentLeftPos = gCurrentLeftPos - 1;
	else{
		stopScroll()
		gCurrentLeftPos = gMinLeft;
	}
	changeDivPosition(gCurrentLeftPos);
}

function scrollDivLeft(){
	if( gMinLeft >= 0 ){
		stopScroll()
		return;
	}
	if( gCurrentLeftPos + 1 < 0 )
		gCurrentLeftPos = gCurrentLeftPos + 1;
	else{
		gCurrentLeftPos = 0;
		stopScroll()
	}
	changeDivPosition(gCurrentLeftPos);
}

function checkBtnClasses(){
	if( gLeftBtn ){
		if( gCurrentLeftPos == 0 || gMinLeft >= 0 ){
			gLeftBtn.className = 'LeftScrollBtnLinkInactive';
		}else{
			gLeftBtn.className = 'LeftScrollBtnLinkActive';
		}
	}
	if( gRightBtn ){
		if( gCurrentLeftPos == gMinLeft || gMinLeft >= 0 ){
			gRightBtn.className = 'RightScrollBtnLinkInactive';
		}else{
			gRightBtn.className = 'RightScrollBtnLinkActive';
		}
	}
}

function changeDivPosition(pLeft){
	gScrolDiv.style.left  = pLeft + 'px';	
	checkBtnClasses()
}

function stopScroll(){	
	if(gInterval)
		clearInterval(gInterval)
}

function startScroll(pDir){
	if( gMinLeft >= 0 ){		
		return;
	}
	if( pDir == 0){//Right		
		gInterval = setInterval("scrollDivRight('right')", 20);
	}else{	
		gInterval = setInterval("scrollDivLeft('left')", 20);
	}	
}

// correctly handle PNG transparency in Win IE 5.5 & 6.
function correctPNG() {
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {
		for(var i=0; i<document.images.length; i++) {
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				var imgStyle = "display:inline-block;" + img.style.cssText;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
					+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
				img.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
}

function customCbox(pCboxId, pImgId, pCheckedImg, pUncheckedImg) {
	var lCbox = document.getElementById(pCboxId);
	var lImg = document.getElementById(pImgId);
	
	if (lCbox.checked) {
		lCbox.checked = false;
		lImg.src = pUncheckedImg;
	} else {
		lCbox.checked = true;
		lImg.src = pCheckedImg;
	}
}

function SubmitAgeControl(pType) {
	var confirm = document.getElementById('confirm')
	var form = document.ageControl;
	if(pType) {
		confirm.value = pType;
		form.submit();
	}
}

function ChangeImg(pLink, pType) {
	var imgc = document.getElementById('confirmimg');
	var imgd = document.getElementById('denyimg');
	if(pLink == 1) {
		if(pType == 1) {
			imgc.src= '/i/confirm_hover.jpg';
		} else {
			imgc.src= '/i/confirm.jpg';
		}
	} else {
		if(pType == 1) {
			imgd.src= '/i/deny_hover.jpg';
		} else {
			imgd.src= '/i/deny.jpg';
		}
	}
}

var gWindowHeight = 0;
var gTopDivHeight = 346;
var gBottomDivHeight = 325;
var gCommonHeight = gTopDivHeight + gBottomDivHeight;
var gBottomDiv = false;
var gTopDiv = false;

function initBottomAgeDiv(){
	gWindowHeight = document.documentElement.clientHeight;
	gTopDiv = document.getElementById('ageTopDiv');
	gBottomDiv = document.getElementById('ageBottomDiv');
	if( !gTopDiv || !gBottomDiv )
		return;
	if( gWindowHeight < gCommonHeight)
		changeBottomAgeDivStyle(0);
	else
		changeBottomAgeDivStyle(1);
}

function correctBottomAgeDiv(){
	lNewHeight = document.documentElement.clientHeight;	
	if( gWindowHeight < gCommonHeight && lNewHeight > gCommonHeight )
		changeBottomAgeDivStyle(1);
	if( gWindowHeight > gCommonHeight && lNewHeight < gCommonHeight )
		changeBottomAgeDivStyle(0);
	gWindowHeight = lNewHeight;
		
}

function changeBottomAgeDivStyle(pOper){
	if( !gTopDiv || !gBottomDiv)
		return;
	if( !pOper ){
		//Smalqvame
		gBottomDiv.style.top = gTopDivHeight + 'px';
		gBottomDiv.style.bottom = '';
	}else{
		//Ugolemqvame
		gBottomDiv.style.top = '';
		gBottomDiv.style.bottom = '0px';
	}
}
