var hasAlreadyPosted = 0;

function checkDot(x,y) {
	if(hasAlreadyPosted==0){
		var pars = "eID=xmas2008&do=check&x=" + x + "&y=" + y
		var myAjax = new Ajax.Request("index.php", {
			method :'get',
			parameters :pars,
			onComplete :getCheck
		});
	}
}

function getCheck(requestObject) {
	
	var xmldoc = requestObject.responseXML;
	xmldoc.getElementsByTagName("success")[0].normalize();
	xmldoc.getElementsByTagName("uid")[0].normalize();
	var success = xmldoc.getElementsByTagName("success")[0].firstChild.data;
	
	var uid = xmldoc.getElementsByTagName("uid")[0].firstChild.data;
	if (success == 1) {
		///alert(uid);
		window.document.xmas2008.resultCheckDot(success);
		showForm(uid);
	}else{
		window.document.xmas2008.resultCheckDot(success);
	}
	
	
}

/*
 * function checkDot(uid) { var pars = "eID=xmas2008&do=check&uid=" + uid var
 * myAjax = new Ajax.Request("index.php", { method :'get', parameters :pars,
 * onComplete :processAnswer }); }
 */


function checkForm(){
	
	
	/*if ((document.message.lastname.value.length=="0") || (document.message.lastname.value==missing)) {
		document.message.lastname.style.backgroundColor = fieldColor;
		document.message.lastname.style.border = border;
		document.message.lastname.value = missing;
		status = 1;
	}
	if ((document.message.firstname.value.length=="0") || (document.message.firstname.value==missing)) {
		document.message.firstname.style.backgroundColor = fieldColor;
		document.message.firstname.style.border = border;
		document.message.firstname.value = missing;
		status = 1;
	}
	if ((document.message.email.value.length=="0") || (document.message.email.value==missing)) {
		document.message.email.style.backgroundColor = fieldColor;
		document.message.email.style.border = border;
		document.message.email.value = missing;
		status = 1;
	}
	if ((document.message.msgtxt.value.length=="0") || (document.message.msgtxt.value==missing)) {
		document.message.msgtxt.style.backgroundColor = fieldColor;
		document.message.msgtxt.style.border = border;
		document.message.msgtxt.value = missing;
		status = 1;
	}
	if ((document.message.upload.value.length=="0") || (document.message.fakefile.value==missing)) {
		document.message.fakefile.style.backgroundColor = fieldColor;
		document.message.fakefile.style.border = border;
		document.message.fakefile.value = missing;
		status = 1;
	}
	if ((document.message.country.value=="0")) {
		document.message.country.style.backgroundColor = fieldColor;
		document.message.country.style.border = border;
		status = 1;
	}*/
	
	if(document.message.terms.checked){
		var terms = 1;
	}else {
		var terms = 0;
	}
	
	
	var pars = "eID=xmas2008&do=submit&uid=" + document.message.dotid.value+ "&firstname="+document.message.firstname.value+"&lastname="+document.message.lastname.value+"&email="+document.message.email.value+"&msgtxt="+document.message.msgtxt.value+"&country="+document.message.country.value+"&securitycode="+document.message.securitycode.value+"&upload="+document.message.fakefile.value+"&gallery="+document.message.galleryImage.value+"&terms="+terms;
	
	$('successMessage').show();
	$('innerForm').hide();
	var myAjax = new Ajax.Request("index.php", {
		method :'post',
		parameters :pars,
		onComplete :updateForm
	});
}

function updateForm(requestObject){
	var missing = 'missing!';
	var fieldColor = '#999999';
	var border = '1px solid #ff6600';
	
	var xmldoc = requestObject.responseXML;
	xmldoc.getElementsByTagName("answer")[0].normalize();
	xmldoc.getElementsByTagName("errorfname")[0].normalize();
	xmldoc.getElementsByTagName("errorlname")[0].normalize();
	xmldoc.getElementsByTagName("errormail")[0].normalize();
	xmldoc.getElementsByTagName("errormsg")[0].normalize();
	xmldoc.getElementsByTagName("errorcountry")[0].normalize();
	xmldoc.getElementsByTagName("errorcaptcha")[0].normalize();
	xmldoc.getElementsByTagName("errorupload")[0].normalize();
	xmldoc.getElementsByTagName("errorterms")[0].normalize();
	xmldoc.getElementsByTagName("uid")[0].normalize();
	var answer = xmldoc.getElementsByTagName("answer")[0].firstChild.data;
	var errorfname = xmldoc.getElementsByTagName("errorfname")[0].firstChild.data;
	var errorlname = xmldoc.getElementsByTagName("errorlname")[0].firstChild.data;
	var errormail = xmldoc.getElementsByTagName("errormail")[0].firstChild.data;
	var errormsg = xmldoc.getElementsByTagName("errormsg")[0].firstChild.data;
	var errorcountry = xmldoc.getElementsByTagName("errorcountry")[0].firstChild.data;
	var errorcaptcha = xmldoc.getElementsByTagName("errorcaptcha")[0].firstChild.data;
	var errorupload = xmldoc.getElementsByTagName("errorupload")[0].firstChild.data;
	var errorterms = xmldoc.getElementsByTagName("errorterms")[0].firstChild.data;
	var uid = xmldoc.getElementsByTagName("uid")[0].firstChild.data;
	//alert(errorfname+" "+errorlname+" "+errormail+" "+errormsg+" "+errorcountry+" "+errorcaptcha);
	if (errorfname==0&&errorlname==0&&errormail==0&&errormsg&&errorcountry==0&&errorcaptcha==0&&errorupload==0&&errorterms==0) {
		$('innerForm').hide();
		$('successMessage').innerHTML = answer;
		$('closelink').href = "javascript:hideForm(2);";
		$('formLayerRight').hide();
		window.document.xmas2008.restartApp(uid);
		hasAlreadyPosted = 1;
	} else {
		$('successMessage').hide();
		$('innerForm').show();
		if (errorfname!=0) {
			document.message.firstname.style.backgroundColor = fieldColor;
			document.message.firstname.style.border = border;
			document.message.firstname.value = missing;
		}
		if (errorlname!=0) {
			document.message.lastname.style.backgroundColor = fieldColor;
			document.message.lastname.style.border = border;
			document.message.lastname.value = missing;
		}
		if (errormail!=0) {
			document.message.email.style.backgroundColor = fieldColor;
			document.message.email.style.border = border;
			document.message.email.value = missing;
		}
		if (errormsg!=0) {
			document.message.msgtxt.style.backgroundColor = fieldColor;
			document.message.msgtxt.style.border = border;
			document.message.msgtxt.value = missing;
		}
		if (errorcountry!=0) {
			document.message.country.style.backgroundColor = fieldColor;
			document.message.country.style.border = border;
		}
		if (errorcaptcha!=0) {
			document.message.securitycode.style.backgroundColor = fieldColor;
			document.message.securitycode.style.border = border;
			document.message.securitycode.value = missing;
		}
		if (errorterms!=0) {
			alert(termsText);
		}

		if (errorupload!=0) {
			$('errorupload').show();
			$('fakefiletop').style.top = "15px";
		}else {
			$('errorupload').hide();
			$('fakefiletop').style.top = "0px";
		}
		//document.message.upload.style.visibility = 'visible';
		
	}
}

function setFirst(strField) {
	document.message[strField].select();
	document.message[strField].style.backgroundColor = "#FFFFFF";
	document.message[strField].style.border = "1px solid #999999";
}

function setFirstTell(strField) {
	document.tell[strField].select();
	document.tell[strField].style.backgroundColor = "#FFFFFF";
	document.tell[strField].style.border = "1px solid #999999";
}

function setMenue(strField) {
	document.message[strField].style.backgroundColor = "#FFFFFF";
	document.message[strField].style.border = "1px solid #999999";
}

function newCaptcha() {
	var time = new Date();
	document.getElementById("captcha-img").src = "fileadmin/xmas2008/inc/class.captcha.php?" + time.getMilliseconds();
}
function newCaptchaTell() {
	var time = new Date();
	document.getElementById("captcha-img-tell").src = "fileadmin/xmas2008/inc/class.captcha.php?" + time.getMilliseconds();
}

function showForm(uid) {
	$('entryForm').show();
	newCaptcha();
	$('formLayerMiddle').show();
	$('formLayerRight').show();
	
	//Effect.Appear('entryForm');
	//setTimeout("document.getElementById('entryForm').style.display = 'block'",1000);
	
	/*if (navigator.appVersion.toLowerCase().indexOf("linux") != -1 || navigator.appVersion.toLowerCase().indexOf("x11") != -1) {
		$('xmasFlash').hide();
		$('maclinFlash').show();
	}
	if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Firefox")!=-1) {
		$('xmasFlash').hide();
		$('maclinFlash').show();
	}*/
	
	$('dotid').value = uid;
}


function resetForm(){
	$('dotid').value = '';
	document.message.fakefile.value = '';
	document.message.firstname.value = '';
	document.message.lastname.value = '';
	document.message.email.value = '';
	document.message.upload.value = '';
	document.message.upfield.value = '';
	document.message.country.value = "0";
	document.message.msgtxt.value = '';
	document.message.securitycode.value = '';
	$('startmsg').show();
	$('imageCatalogue').hide();
	$('fakefileform').show();
	$('fileform').show();
	$('galleryButton').show();
	$('msg').innerHTML = '';
	$('successMessage').hide();
	$('innerForm').show();
}

function hideForm(deleteForm){
	
	//Effect.Fade('entryForm');
	
	/*if (navigator.appVersion.toLowerCase().indexOf("linux") != -1 || navigator.appVersion.toLowerCase().indexOf("x11") != -1) {
		$('xmasFlash').show();
		$('maclinFlash').hide();
	}
	if (navigator.userAgent.indexOf("Mac")!=-1 && navigator.userAgent.indexOf("Firefox")!=-1) {
		$('xmasFlash').show();
		$('maclinFlash').hide();
	}*/
	$('entryForm').hide();
	
	window.document.xmas2008.resultCheckDot(false);
	if (deleteForm==1) {
		var uid = $('dotid').value;
		
		var pars = "eID=xmas2008&do=unlock&uid=" + uid;
		var myAjax = new Ajax.Request("index.php", {
			method :'get',
			parameters :pars
		});
	}
	resetForm();
	
}

function deleteImage() {
	var uid = $('dotid').value;
	$('startmsg').show();
	$('msg').innerHTML = '';
	document.message.upload.value = '';
	document.message.upfield.value = '';
	document.message.fakefile.value = '';
	$('galleryButton').show();
	
}

function uploadFile(uid) {
	$('upfield').value = "1";
	$('startmsg').hide();
	$('msg').innerHTML = '<img src="fileadmin/xmas2008/images/ajax-loader.gif" style="margin-left:46px;" />'
	document.message.target = "upload_target";
	document.message.submit();
}

function stopUpload(text,success) {
	$('upfield').value = "0";
	$('galleryButton').hide();
	$('msg').innerHTML = text;
	document.message.fakefile.value = document.message.upload.value
	if (!success){
		document.message.upload.value = '';
		document.message.upfield.value = '';
		document.message.fakefile.value = '';
		$('galleryButton').show();
	}
	
	return true;
}

/*
 * Form field Limiter script- By Dynamic Drive For full source code and more
 * DHTML scripts, visit http://www.dynamicdrive.com This credit MUST stay intact
 * for use
 */

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength)
		return false
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
	var pressedkey=/[a-zA-Z0-9\.\,\/]/ // detect alphanumeric keys
	if (pressedkey.test(String.fromCharCode(e.which)))
		e.stopPropagation()
	}
}

function countlimit(maxlength,e,placeholder){
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder)){
	if (lengthleft<0)
		theform.value=theform.value.substring(0,maxlength);
		if(lengthleft<0){
			$('remaining').innerHTML=0;
		}else {
			$('remaining').innerHTML=lengthleft;
		}
	}
}


function displaylimit(thename, theid, thelimit){
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text=thelimit;
	if (document.all||ns6)
		$('remaining').innerHTML = limit_text;
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}

function selectImage() {
	$('startmsg').hide();
	$('fakefileform').hide();
	$('fileform').hide();
	$('imageCatalogue').show();
}

function switchImg(image) {
	$('currentImg').src = "fileadmin/xmas2008/images/catalogue/"+(pictures[image]);
	if(image!=0){
		$('lastImg').show();
		$('lastImg').href="javascript:switchImg("+(image-1)+");";
	}else {
		$('lastImg').hide();
	}
	
	if(image!=pictures.length-1){
		$('nextImg').show();
		$('nextImg').href="javascript:switchImg("+(image+1)+");";
	}else {
		$('nextImg').hide();
	}
	
	
}

function useImage() {
	var file = $('currentImg').src;
	file = file.split("/");
	
	$('galleryImage').value = file[7];
	$('galleryTextUsed').show();
	$('galleryText').hide();
}

function deleteImageGallery(){
	$('imageCatalogue').hide();
	$('galleryImage').value = '';
	$('startmsg').show();
	$('fakefileform').show();
	$('fileform').show();
	$('lastImg').hide();
	$('nextImg').show();
	$('nextImg').href="javascript:switchImg(1);";
	$('galleryTextUsed').hide();
	$('galleryText').show();
}

function tellaFriend(uid){
	window.document.xmas2008.fadeIn();
	$('tellForm').show();
	$('formLayerMiddle').show();
	//Effect.Appear('entryForm');
	//setTimeout("document.getElementById('entryForm').style.display = 'block'",1000);
	$('dotid_tell').value = uid;
}

function hideTell(deleteForm){
	$('tellForm').hide();
	window.document.xmas2008.fadeOut();
}

function checkTellForm() {
	var pars = "eID=xmas2008&do=tell&";
	pars += $('tell').serialize();
	$('successMessageTell').show();
	$('innerFormTell').hide();
	var myAjax = new Ajax.Request("index.php", {
		method :'post',
		parameters :pars,
		onComplete :updateTell
	});
}

function updateTell(requestObject){
	var missing = 'missing!';
	var fieldColor = '#999999';
	var border = '1px solid #ff6600';
	var xmldoc = requestObject.responseXML;
	xmldoc.getElementsByTagName("answer")[0].normalize();
	xmldoc.getElementsByTagName("errorfname")[0].normalize();
	xmldoc.getElementsByTagName("erroryname")[0].normalize();
	xmldoc.getElementsByTagName("errorymail")[0].normalize();
	xmldoc.getElementsByTagName("errorfmail")[0].normalize();
	xmldoc.getElementsByTagName("errorcaptcha")[0].normalize();
	xmldoc.getElementsByTagName("uid")[0].normalize();
	var answer = xmldoc.getElementsByTagName("answer")[0].firstChild.data;
	var errorfname = xmldoc.getElementsByTagName("errorfname")[0].firstChild.data;
	var erroryname = xmldoc.getElementsByTagName("erroryname")[0].firstChild.data;
	var errorymail = xmldoc.getElementsByTagName("errorymail")[0].firstChild.data;
	var errorfmail = xmldoc.getElementsByTagName("errorfmail")[0].firstChild.data;
	var errorcaptcha = xmldoc.getElementsByTagName("errorcaptcha")[0].firstChild.data;
	var uid = xmldoc.getElementsByTagName("uid")[0].firstChild.data;
	//alert(errorfname+" "+errorlname+" "+errormail+" "+errormsg+" "+errorcountry+" "+errorcaptcha);
	if (errorfname==0&&erroryname==0&&errorymail==0&&errorfmail&&errorcaptcha==0) {
		$('innerFormTell').hide();
		$('successMessageTell').innerHTML = answer;
		$('closelink').href = "javascript:hideTell(2);";
		$('formLayerRight').hide();
		
		//window.document.xmas2008.restartApp();
	} else {
		$('successMessageTell').hide();
		$('innerFormTell').show();
		if (errorfname!=0) {
			document.tell.friendname.style.backgroundColor = fieldColor;
			document.tell.friendname.style.border = border;
			document.tell.friendname.value = missing;
		}
		if (erroryname!=0) {
			document.tell.yourname.style.backgroundColor = fieldColor;
			document.tell.yourname.style.border = border;
			document.tell.yourname.value = missing;
		}
		if (errorymail!=0) {
			document.tell.yourmail.style.backgroundColor = fieldColor;
			document.tell.yourmail.style.border = border;
			document.tell.yourmail.value = missing;
		}
		if (errorfmail!=0) {
			document.tell.friendmail.style.backgroundColor = fieldColor;
			document.tell.friendmail.style.border = border;
			document.tell.friendmail.value = missing;
		}
		if (errorcaptcha!=0) {
			document.tell.securitycode.style.backgroundColor = fieldColor;
			document.tell.securitycode.style.border = border;
			document.tell.securitycode.value = missing;
		}
		//document.message.upload.style.visibility = 'visible';
		
	}
}

function debug(message) {
	///alert(message);
}
