function full(str, elem, name, nullvalue) {
	if(!name) name = '';
	if(!nullvalue) nullvalue = 0
	if (elem.value == nullvalue) {
		alert(str + (name != '' ? " " + name + "." : ''))
	    elem.focus();
	    return false;
	}
	return true;
}

function checkEmail(str, elem) {
	if (elem.value == 0) {
		alert(str);
	    elem.focus();
	    return false;
	} else {
		var re=/^[\w\.\_\+-]*@[\w\_\+-]+(\.[\w\_\+-]+)*\.[\w\+-]+$/;
		if (elem.value.search(re)==-1) {
			alert(str);
			elem.focus();
			elem.select();
			return false;
		}
		return true;
	}
}

function getNewXMLHttp() {
	xmlhttp = null;

    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = null;
        }
   
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = null;
        	}
		}
    }
    
    return xmlhttp;
}

function getPage(lang, pgName, pgHandle, sessionid) {

	var slideBgInMorph = new Fx.Morph ( 'background', {
			duration: 500,
			transition: Fx.Transitions.Sine.easeOut
		}
	);

	var slideBgOutMorph = new Fx.Morph ( 'background', {
		onComplete: function() {
			$('background').style.backgroundImage = "url(/Assets/Images/PageImages/" + pgHandle + ".jpg)";
			setTimeout(function () { slideBgInMorph.start({ marginTop: ['-541px', '-270px'], marginLeft: ['-420px', '-420px'] }); }, 100);
			setTimeout(function () { slideContentInMorph.start({ marginTop: ['-650px', '-235px'], marginLeft: ['-50px', '-50px'] }); }, 250);
		},
		duration: 400,
		transition: Fx.Transitions.Sine.easeIn
	});

	var slideContentOutMorph = new Fx.Morph ( 'page-content', {
		onComplete: function() {
			loadContent(pgHandle, lang, sessionid);
		},
		duration: 300,
		transition: Fx.Transitions.Sine.easeIn
	});

	var slideContentInMorph = new Fx.Morph ( 'page-content', {
			duration: 400,
			transition: Fx.Transitions.Sine.easeOut
		}
	);

	// set the old link to inactive
	var currentBtn = $('mmbtn-' + currentPageHandle);
	if(currentBtn) {
		currentBtn.className = "lfloat mmbutton";
		currentBtn.onmouseover = function () { this.className = 'lfloat mmbutton active'; }
		currentBtn.onmouseout = function () { this.className = 'lfloat mmbutton'; }
		var cp = new String(currentPage);
		var ch = new String(currentPageHandle);
		currentBtn.onclick = function() { getPage(lang, cp, ch, sessionid) };
		var currentLink = $('link-' + currentPageHandle);
		currentLink.innerHTML = "<" + "a href=\"/" + ch + "/\" onclick=\"return getPage('" + lang + "', '" + cp + "', '" + ch + "', '" + sessionid + "')\">" + cp + "<" + "/a" + ">";
		currentBtn.style.cursor = 'pointer';
	}
	// set the new link to active
	var newBtn = $('mmbtn-' + pgHandle );
	if(newBtn) {
		newBtn.className = "lfloat mmbutton active";
		newBtn.onmouseover = function () { this.className = 'lfloat mmbutton active'; }
		newBtn.onmouseout = function () { this.className = 'lfloat mmbutton active'; }
		newBtn.onclick = function() { };
		var newLink = $('link-' + pgHandle);
		newLink.innerHTML = "<" + "span" + ">" + pgName + "<" + "/span" + ">";
		newBtn.style.cursor = 'default';
	}
	
	// finally set the new page as the current page
	currentPage = pgName;
	currentPageHandle = pgHandle;
	
	// change the title of the page
	document.title = (pgHandle != 'home' ? pgName + ' » ' : '') + 'CoPrint';

	// change the location of the document 
	// (when the user refreshes the page we must display the corect page not the original one)
	url = "/set-location.php";
	str = "sess=" + sessionid + "&location=" + escape(pgHandle) + "&lang=" + lang;
	var ajaxLocation = getNewXMLHttp();
	ajaxLocation.open("POST", url, true);
	ajaxLocation.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxLocation.send(str);

	// start the animation
	slideContentOutMorph.start({ marginTop: ['-235px', '-235px'], marginLeft: ['-50px', '421px'] });
	setTimeout( function() { slideBgOutMorph.start({ marginTop: ['-270px', '-270px'], marginLeft: ['-420px', '421px'] }); }, 150);
	
	return false;
}

function startGallery() {
	var myGallery = new gallery( $('gallery'), {
		timed: true,
		delay: 3500,
		fadeDuration: 750,
		showArrows: false,
		showCarousel: false,
		embedLinks: false,
		useHistoryManager: false,
		useThumbGenerator: false,
		showInfopane: false,
		slideInfoZoneSlide: false,
		titleSelector: "span",
		slideInfoZoneOpacity: 0,
		defaultTransition: "crossfade"
	} );
}