// JavaScript Document
var d = document, w = window;

//GET THE SERVER NAME TO USE ABSOLUTE URLS WHERE NEEDED
function serverName()
{
	var s, url = location.href;
	if(url.indexOf("localhost") != -1)		{ s = "http://localhost:8080/KLStudios"; }
	else if(url.indexOf("https://") != -1)	{ s = "https://www.klstudios.ie"; }
	else									{ s = "http://www.klstudios.ie"; }
	if(url.indexOf("_office") != -1) 		{ s += "/_office"; } return s;
}

function getSelectedIndex(cellID) //GET THE SELECTED INDEX VALUE OF A SELECT BOX
{
	var i = d.getElementById(cellID).selectedIndex; return d.getElementById(cellID).options[i].value;
}

function windowSize(type)
{
	var wi = 0; var hi = 0; if(!window.innerWidth) { 
		if(!(d.documentElement.clientWidth == 0)) { wi = d.documentElement.clientWidth; hi = d.documentElement.clientHeight; } //IE STRICT
		else 									  { wi = d.body.clientWidth; hi = d.body.clientHeight; }} //QUIRKS MODE
	else 										  { wi = w.innerWidth; hi = w.innerHeight; } // W3C
	if(type=="width") { return wi; } else { return hi; }
}

function doNav(mainNavId,action)
{
	var div = '#mainNav'+mainNavId; if(action) { $(div).show(); } else { $(div).hide(); }
}

function doContact(action)
{
	if(action) 	{	closeContact(); }
	else		{	var wi = ((windowSize('width')-800)/2)+"px"; scrollTo(0,0);
				 	$('#popupBackground').show().fadeTo('fast',0.5);
				 	$('#contactUs').css({'left':wi}).show().animate({'height':'450px'}); $('#phoneContact').show(); }
}

function closeContact()
{
	$('.input').val(""); $('.inputInfo').hide(); $('#contactResult').html('&nbsp;'); $('#popupBackground').fadeOut(); $('#contactUs').animate({'height':'0px'}).fadeOut();
}

//process forms
$(function() { 
	//LOGIN FORM
	$('#submitContactForm').click(function() {	$('#loadingForm').show(); setTimeout('processForm()',500); return false; });
});

function processForm()
{
	var sendForm = true; var data = Array(); var query = "formname=contact"; var arrCells = Array('name','phone','email','message','spam'); 
	for(var i = 0; i < arrCells.length; i++) { data[i] = $('#'+arrCells[i]).val(); if(i == 4) {
	var spamCode = $('#spamcode').val(); if(spamCode != data[i]) { $('#inputInfospam').show(); sendForm = false; }
	} else { if(data[i] == "") { $('#inputInfo'+arrCells[i]).show(); $('#contactUs').css({'height':'500px'}); sendForm = false; } 
	else { 	query += "&"+arrCells[i]+"="+data[i]; }
	var company = $('#company').val(); if(company != "") { query += "&company="+company; } } }
	if(sendForm == true) { $.ajax ({ url: serverName()+"/_lib/ajax.php", type: 'POST', data: query,
	success: function(result) { $('#contactResult').html(result); setTimeout('closeContact()',3000); } }); 
	} else { $('#contactResult').html('There are errors on your form, please review the details above'); } $('#loadingForm').hide(); $('#phoneContact').hide(); 
}

function doInfo(cellId)
{
	if($('#'+cellId).val() == "") { $('#inputInfo'+cellId).fadeIn(); } else { $('#inputInfo'+cellId).fadeOut(); } $('#contactResult').html('&nbsp;');
}

function doWebsite(websiteId,show)
{
	var loader = "<img src=\""+serverName()+"/_images/page/load.gif\" alt=\"Loading\" style=\"margin : 230px 430px;\" />";
	if(websiteId==0) { $('#popupBackground').fadeOut(); $('#websitesDetails').html(loader).animate({'height':'50px'}).fadeOut(); }
	else { if(show) {
		var wi = ((windowSize('width')-990)/2)+"px"; scrollTo(0,0); $('#popupBackground').show().fadeTo('fast',0.5);
		$('#websitesDetails').css({'left':wi}).show().animate({'height':'530px'}); }
	else { 	$('#websitesDetails').html(loader); }
	$.ajax ({ url: serverName()+"/_lib/ajax.php", type: 'POST', data: "website="+websiteId, 
			  success: function(result) { $('#websitesDetails').html(result); } }); }
}
