// JavaScript Document
var stat = true;
var bd, dumOut, nfo, mem, msg;


function init(){
	//
	mem = document.getElementById("infoBody").innerHTML;
	//
	bd = document.getElementById("mainBody");
	dumOut = document.getElementById("dummyOut");
	nfo = document.getElementById("info");
	
	bd.style.left = dumOut.offsetLeft-bd.offsetWidth/2 + "px";
	//bd.style.top = dumOut.offsetTop-bd.offsetHeight/2 + "px";
	
	nfo.style.left = dumOut.offsetLeft-nfo.offsetWidth/2 + "px";
	//nfo.style.top = dumOut.offsetTop-nfo.offsetHeight/2 + "px";
}

function repoz(){
	bd.style.left = dumOut.offsetLeft-bd.offsetWidth/2 + "px";
	//bd.style.top = dumOut.offsetTop-bd.offsetHeight/2 + "px";
	
	nfo.style.left = dumOut.offsetLeft-nfo.offsetWidth/2 + "px";
	//nfo.style.top = dumOut.offsetTop-nfo.offsetHeight/2 + "px";
}

function forget(){
	if(stat){
		
		bd.style.visibility = "hidden";
		nfo.style.visibility = "visible";
		stat = false;
		
	} else {
		
		bd.style.visibility = "visible";
		nfo.style.visibility = "hidden";
		stat = true;
		
	}
}

function sndMail(){
	
	var checkfrom = document.emailForm.fromWho.value;
	
	if(checkfrom == ""){
		document.getElementById("showEmailEml").innerHTML = "* e-mail required";
		return;
	} else {
		document.getElementById("showEmailEml").innerHTML = "";
	}
	
	var str = "lookForData=1&" + getForm(document.emailForm);
	var url = "mailinfo.asp";
	
	document.emailForm.reset();
	
	makeRequest(url, str, "forget");
}

function sndMsg(){
	
	var checkfrom = document.msgForm.fromWho.value;
	var checkmsg = document.msgForm.msgToSnd.value;
	
	if(checkfrom == ""){
		document.getElementById("showEmailMsg").innerHTML = "* e-mail required";
		return;
	} else {
		document.getElementById("showEmailMsg").innerHTML = "";
	}
	
	if(checkmsg == ""){
		document.getElementById("showMsgMsg").innerHTML = "* a messege is required";
		return;
	} else {
		document.getElementById("showMsgMsg").innerHTML = "";
	}
	
	var str = "lookForData=0&" + getForm(document.msgForm);
	var url = "mailinfo.asp";
	
	document.msgForm.reset();
	
	makeRequest(url, str, "forget");
}

function sendContactMsg() {
	
	document.getElementById("formToSnd").style.visibility = "hidden";
	document.getElementById("msgSnd").style.visibility = "visible";
	
	var str = "lookForData=2&" + getForm(document.msgForm);
	var url = "mailinfo.asp";
	
	//alert(str)
	makeRequest(url, str, "contact");
	
}

//custom escape for asp '%0' (enter for text area problem when send e-amil)
function getForm(obj){
	
	var str = "";
	var strTmp = new String();
	var regEx = new RegExp('%0', 'gi');
	
	for(var i=0; i<obj.length; i++){
		if(obj[i].type == "text" || obj[i].type == "textarea"){
			//this was added to send e-mail with new line when enter is press
			//remember that the e-mail must be in HTML format;
			//to perform normal escape replace the whole 'if() else' with:
			//str += obj[i].name + "=" + escape(obj[i].value) + "&";
			//==============================================================
			if(obj[i].type == "textarea"){
				strTmp = escape(obj[i].value);
				strTmp = strTmp.replace(regEx, "<br />%0")
				str += obj[i].name + "=" + strTmp + "&";
			} else {
				str += obj[i].name + "=" + escape(obj[i].value) + "&";
			}
			//===============================================================
		}
	}
	str = str.substr(0, str.length-1);
	return str;
}

function whileLoading(who){
	switch(who){
		case "forget" :
		document.getElementById("infoBody").innerHTML = "Sending ...";
		break;
		case "contact" :
		document.getElementById("showMsg").innerHTML = "<b>Sending ...<br />Please wait.</b><br /><br />This might take longer depending of your internet connection";
		break;
	}
}

function requestResponse(who) {
	
	switch (who) {
		
		case "forget" :
		document.getElementById("infoBody").innerHTML = msg;
		this.setTimeout(restoreForget, 6000);
		break;
		
		case "contact" :
		document.getElementById("showMsg").innerHTML = msg;
		this.setTimeout(restoreContact, 6000)
		break;
		
	}
	
	function restoreForget() {
		
		document.getElementById("infoBody").innerHTML = mem;
		
	}
	
	function restoreContact() {
		
		document.msgForm.reset();
		document.getElementById("formToSnd").style.visibility = "visible";
		document.getElementById("msgSnd").style.visibility = "hidden";
		
	}
	
}

//fct ajax
function makeRequest(url, data, who) {
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			//FF bug?
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
				try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot send the message!\nPlease try again later!');
		return false;
	}
	http_request.onreadystatechange = function() {
		if (http_request.readyState != 4) {
			//msg while is sending
			whileLoading(who);
		}
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				eval(http_request.responseText);
				requestResponse(who);
				
			} else {
				alert('There was a problem with the request.\nPlease try again later!');
			}
		}
	};
	http_request.open("POST", url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", data.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(data);
}