// Insert AJAX
//Contacts a php script on the site, that curls to the main server and the main server is then to return phone status and also place the call and confirm when it is done.
function lightbox(div, lightW) {
var state = $('filter');
var screenW = screen.width;
if (lightW == null){lightW = 400}
var LightboxW = lightW;
if (state.style.display == 'block')
   {
   new Effect.Fade(state, { duration: 2.0 });
   new Effect.Fade($(div), { duration: 2.0 });
   }
else
   {
   state.style.display = 'block';
   $(div).style.width= LightboxW + 'px';
   $(div).style.left= '' + ((screenW / 2) - (LightboxW /2)) + 'px';
   $(div).style.display= 'block';
   }

}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

function showHide(show,hide) {
element = $(hide);
setTimeout ( "showOnTimer('" +  show +  "')", 500 );
Effect.SlideUp(element, { duration: 0.5 });
}

function showOnTimer(show) {
var element = $(show);
Effect.SlideDown(element, { duration: 0.5 });
}

function placeCall() {
var value = $RF('callSelect', 'call');
new Ajax.Request('themes/sits/code/call.php', {
  method: 'post',
  parameters: { call_type: value,
		number: $('number').value
		},
  onSuccess: callSuccess,
  onFailure: callFail
  });
showHide('attemptingCall','callSelection');
}

function callSuccess(transport) {
	var response = transport.responseText || "failed";
	if (response == "success") {
		showHide('callPlaced','attemptingCall');
		setTimeout ( "resetHide()", 10000 );	
	}
	else {
		showHide('callSelection','attemptingCall');
	}
}

function resetHide() {
	showHide('callSelection','callPlaced');
	lightbox('clickBox',360);
}

function callFail() {
showHide('callSelection','attemptingCall');
}

function checkLine(ext) {
	new Ajax.Request('themes/sits/code/status.php', {
  	method: 'post',
  	parameters: { call_type: ext },
  	onSuccess: checkSuccess,
  	onFailure: checkFail
  	});
}

function checkSuccess(transport) {
	var response = transport.responseText || "no Status?";
	if (response == "salonline") {
		$('sal_stat').className = 'online';
		$('sal_stat').innerHTML = 'Online';
		$('sales').disabled = false;
		checkLine('sal');	
	}
	if (response == "saloffline") {
		$('sal_stat').className = 'offline';
		$('sal_stat').innerHTML = 'Offline';
		$('sales').disabled = true;
		$('sales').checked = false;
		checkLine('sal');	
	}
	if (response == "suponline") {
		$('sup_stat').className = 'online';
		$('sup_stat').innerHTML = 'Online';
		$('support').disabled = false;
		checkLine('sup');	
	}
	if (response == "supoffline") {
		$('sup_stat').className = 'offline';
		$('sup_stat').innerHTML = 'Offline';
		$('support').disabled = true;
		$('support').checked = false;
		checkLine('sup');	
	}
	if (response == "devonline") {
		$('dev_stat').className = 'online';
		$('dev_stat').innerHTML = 'Online';
		$('development').disabled = false;
		checkLine('dev');	
	}
	if (response == "devoffline") {
		$('dev_stat').className = 'offline';
		$('dev_stat').innerHTML = 'Offline';
		$('development').disabled = true;
		$('development').checked = false;
		checkLine('dev');	
	}
	if (response == "genonline") {
		$('gen_stat').className = 'online';
		$('gen_stat').innerHTML = 'Online';
		$('general').disabled = false;
		checkLine('gen');	
	}
	if (response == "genoffline") {
		$('gen_stat').className = 'offline';
		$('gen_stat').innerHTML = 'Offline';
		$('general').disabled = true;
		$('general').checked = false;
		checkLine('gen');	
	}
}

function checkFail() {
//alert('Could not test send request...');
}

function checkAllLines() {
	checkLine('sal'); 
	checkLine('sup'); 
	checkLine('dev'); 
	checkLine('gen');
}
