// JavaScript Document
// written by Mark Tank ©2009 all rights reserved

// the path to the _contact.php is set there
var contactPath = "";

Element.observe(window,'load',function() {
		selectEmp();						   
		$("contact").observe("submit",function(event) {
		Event.stop(event);
		trySumbit();
	});
});

function trySumbit() {
	response = true;
	var cf = new checkForm("#ccccff","#ffffff");
	cf.error = "showErrors";
	cf.text = "fname,message,subject";
	cf.email = "email";
	cf.phone = "phone";
	response = cf.checkText(true);
	response = cf.checkEmail();
	response = cf.checkPhone();
	if(response == true) {
		showWorking(true);
		newAJX = new Ajax.Request(contactPath+'_contact.php',
		{
			method:'post',
			parameters: $('contact').serialize(true),
			onSuccess: function(transport){
				var response = transport.responseText;
				if(response == "true") {
					var sto = setTimeout(function() {
						window.location = "contact.php"
					},3000);
				}
				else {
					showWorking(false);
					$("showErrors").update(response);
				}
			 },
			onFailure: function(){ 
				alert('Contact ERROR: 101')
				showWorking(false);
			}
		});
	}
	else {
		window.scrollTo(0,0)
		return false;
	}
}

function showWorking(status) {
	if(status == true) {
		formBox = $("contact");
		var dimen = formBox.getDimensions();
		//make gray background
		var grayBackground = new Element('div', { 'id': 'grayBC'});
		grayBackground.setStyle({ position: "absolute",top: "0px",left: "0px",width : dimen.width+"px",height: dimen.height+"px",backgroundColor: "white"});
		grayBackground.setOpacity(.4);
		formBox.appendChild(grayBackground);
		formBox.makePositioned();
		//make progress
		var prog = new Element('div', { 'id': 'progress'});
		topOffset = Math.round((dimen.height - 39) / 2);
		leftOffset = Math.round((dimen.width - 152) / 2);
		prog.setStyle({ position: "absolute", border: "solid 2px black", padding: "12px",top: topOffset+"px",left: leftOffset+"px",width : "148px",height: "auto",backgroundColor: "white"});
		var progGif = new Element('img', { 'src': 'images/long-progress.gif',"width":"152px","height":"15px"});
		progGif.setStyle({float:"none",margin:"0",padding:"0"});
		prog.appendChild(progGif);
		formBox.appendChild(prog);
		
	}
	else {
		$("grayBC").remove();
		$("progress").remove();
	}
}

function selectEmp() {
	
	var empImg = $('mainBoard').select('.emaiIcon');
	for(i=0;i< empImg.length; i++) {
		empImg[i].observe('click', function() {
			Effect.Appear("mailBox");
			$("message").value = "Dear "+this.alt+",";
			$("emailTo").value = this.alt;
			window.scrollTo(0,window.outerHeight);
		});
	}
}
