
var iqmetrix_rules = {
	'div.message' : function(el) {
		new Effect.Highlight(el, {startcolor:'#DEE8F2', endcolor:'#FFFFFF', duration: 4.0});
	},
	'div.flash_message' : function(el) {
		new Effect.Highlight(el, {startcolor:'#DEE8F2', endcolor:'#FFFFFF', duration: 4.0});
	},
	'div.error_message' : function(el) {
		new Effect.Highlight(el, {startcolor:'#EF4A4A', endcolor:'#FFEFEF', duration: 4.0});
	},
	'#category_list' : function(el) {
		Sortable.create(el,{constraint:false, onUpdate:saveOrder });
	},
	'#hardware_sort_list' : function(el) {
		Sortable.create(el,{constraint:false, onUpdate:saveHardwareOrder });
	},
	'div.trainingSection' : function(el) {
		el.style.display="none";
	},
	'span.helptip' : function(e1) {
		initBox('tooltip');
		var theObject = $(e1.id);
		theObject.onclick = function(e) {
			var tiphtml = '';
			tiphtml += '<img src="/img/close.png" class="close_button" onclick="_hideBox(\'tooltip\', 0.3);" alt="Close" title="Close" />';
			tiphtml += '<div class="tooltip">' + theObject.getAttribute('helptip') + '</div>';
			_showBox(e, 300, 'auto', tiphtml, 'tooltip', 0.8, 2, 2);
			
		}
	}

}

Behaviour.register(iqmetrix_rules);


function saveOrder() {
	//alert(Sortable.serialize('category_list'));
	new Ajax.Updater('order_status', '/admin/save_category_order/?' + Sortable.serialize('category_list'), {asynchronous:true, evalScripts:true});
}

function saveHardwareOrder() {
	//alert(Sortable.serialize('category_list'));
	new Ajax.Updater('order_status', '/admin/save_hardware_order/?' + Sortable.serialize('hardware_sort_list'), {asynchronous:true, evalScripts:true});
}


function toggleCategoryDisplay(contentDiv, arrowDiv) {
	new Effect.toggle(contentDiv, 'blind', {duration:1, queue: 'end', beforeStart:toggleCategoryArrow(contentDiv, arrowDiv)});
}
function toggleCategoryArrow(contentDiv, arrowDiv) {
	if ($(contentDiv).style.display == 'none') {
		$(arrowDiv).innerHTML = '<img src="/img/cat-arrow-down.gif" alt="" />';
	}
	else {
		$(arrowDiv).innerHTML = '<img src="/img/cat-arrow.gif" alt="" />';
	}
}

function showAnswer(qid) {
	//$('answer_content').innerHTML = '<p>Loading...</p>';
	//new Ajax.Updater('answer_content', '/content/answer/' + qid + '/', {asynchronous:true});
	
	//$('answer_content').style.position = 'absolute';
	
	//new Effect.Appear('answer_float', { duration: 1.0 });
	
	modal_ajax('<p>Loading...</p>','/content/answer/' + qid + '/');
	
}
function printAnswer(qid) {
	window.open('/content/printanswer/' + qid + '/','print_window','width=760,height=400,toolbar=yes,menubar=yes,scrollbars=yes');
}

function hideAnswer() {
	//new Effect.Fade('answer_float', { duration: 1.0 });
	Control.Modal.close();
}

function initBox(divname) {
		// 'divoverlay'
	if (!document.getElementById(divname)) {
		var objBody = document.getElementsByTagName("body").item(0);
		
		var divOverlay = document.createElement("div");
		divOverlay.setAttribute('id',divname);
		divOverlay.style.display = 'none';
		divOverlay.style.position = 'absolute';
		divOverlay.style.top = '0';
		divOverlay.style.left = '0';
		divOverlay.style.zIndex = '220';
		//divOverlay.style.width = '140px';
		//divOverlay.style.height = '90px';
		objBody.insertBefore(divOverlay, objBody.firstChild);
	}
}

function showBox(e, divWidth, divHeight, theInnerHtml) {
	_showBox(e, divWidth, divHeight, theInnerHtml, 'divoverlay', 0.5, 0, 0);
}

function _showBox(e, divWidth, divHeight, theInnerHtml, whichDiv, dur, x_offset, y_offset) {
	
	initBox('divoverlay');
	
	var objDivOverlay = $(whichDiv);
	
	if (!e) var e = window.event;
	var left = Event.pointerX(e);
	var top = Event.pointerY(e);
		// Check to be sure it doesn't run off the page
	var windowWidth = 99999999;
	if (typeof window.innerWidth!='undefined') {
			windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && typeof document.documentElement.clientWidth!='undefined' && document.documentElement.clientWidth!=0) {
				windowWidth = document.documentElement.clientWidth;
		} 
		else {
			if (document.body && typeof document.body.clientWidth!='undefined') {
				windowWidth = document.body.clientWidth;
			}
		}	
	}
	if ((left + parseInt(divWidth) + 10) > windowWidth) {
		left = left - parseInt(divWidth);
	}
	
	left = left + x_offset;
	top = top + y_offset;
	
	objDivOverlay.style.top = top + 'px';
	objDivOverlay.style.left = left + 'px';
	objDivOverlay.style.width = divWidth + 'px';
	if (divHeight != 'auto') {
		objDivOverlay.style.height = divHeight + 'px';
	}
	
	
	objDivOverlay.innerHTML = theInnerHtml;
	objDivOverlay.style.display = '';
	//Effect.Appear(objDivOverlay, { duration: dur,  queue: {position:'front', scope: 'floating_div', limit:1} });
}

function hideBox() {
	_hideBox('divoverlay', 0.5);
}

	
function _hideBox(whichDiv, dur) {
	var objDivOverlay = $(whichDiv);
	//Effect.Fade(objDivOverlay, { duration: dur,  queue: {position:'end', scope: 'floating_div', limit:1} });
	objDivOverlay.style.display = 'none';
}


function addHardwareRequest() {
	var rowList = document.getElementsByClassName("hardware_row");
	var thisIndex = rowList.length;
	var content = '<tr class="hardware_row">';
	content += '<td><input type="text" name="request[' + thisIndex + '][location]" class="location" /></td>';
	content += '<td><input type="text" name="request[' + thisIndex + '][router]" class="router" /></td>';
	content += '<td><input type="text" name="request[' + thisIndex + '][wan]" class="wan" /></td>';
	content += '<td><input type="text" name="request[' + thisIndex + '][workstation]" class="workstation" /></td>';
	content += '</tr>';
	new Insertion.After(rowList[rowList.length - 1], content);
}
function addNewHardwareRequest() {
	var rowList = document.getElementsByClassName("hardware_row");
	var thisIndex = rowList.length;
	var typelist = $('typeCopy').innerHTML;
	var content = '<tr class="hardware_row">';
	content += '<td><input type="text" name="request[' + thisIndex + '][location]" class="location" /></td>';
	content += '<td><select name="request[' + thisIndex + '][router]" class="' + thisIndex + '" onchange="toggleOther(this);">' + typelist + '</select><div id="Other' + thisIndex + '" style="display: none;">Other: <input type="text" name="request[' + thisIndex + '][other]" style="other" /></div></td>';
	content += '<td><input type="text" name="request[' + thisIndex + '][wan]" disabled="true" class="wan" style="width: 75px;" id="wan' + thisIndex + '" /><br /><input type="checkbox" id="dhcp' + thisIndex + '" name="request[' + thisIndex + '][wantype]" value="DHCP" checked="true" onclick="$(\'wan' + thisIndex + '\').disabled = this.checked;" />DHCP</td>';
	content += '</tr>';
	new Insertion.After(rowList[rowList.length - 1], content);
}
function addSoftwareRequest() {
	var rowList = document.getElementsByClassName("software_row");
	var thisIndex = rowList.length;
	var content = '<tr class="software_row">';
	content += '<td><input type="text" name="request[' + thisIndex + '][firstname]" class="firstname" /></td>';
	content += '<td><input type="text" name="request[' + thisIndex + '][lastname]" class="lastname" /></td>';
	content += '</tr>';
	new Insertion.After(rowList[rowList.length - 1], content);
}
function addNewSoftwareRequest() {
	var rowList = document.getElementsByClassName("software_row");
	var thisIndex = rowList.length;
	var content = '<tr class="software_row">';
	content += '<td><select name="type" class="' + thisIndex + '" onChange="togglerow(this);"><option value="user">Employee</option><option value="location">Location</option></select></td>';
	content += '<td><input class="vpnlocation' + thisIndex + '" disabled="true" type="text" name="request[' + thisIndex + '][location]" class="location" /></td>';
	content += '<td><input class="vpnuser' + thisIndex + '" type="text" name="request[' + thisIndex + '][firstname]" class="firstname" /></td>';
	content += '<td><input class="vpnuser' + thisIndex + '" type="text" name="request[' + thisIndex + '][lastname]" class="lastname" /></td>';
	content += '</tr>';
	new Insertion.After(rowList[rowList.length - 1], content);
}
var modal_div = function(div) {
	m = Control.Modal.current;
	if (m == false) {
		m = new Control.Modal(false);
	}

	m.initialize(false,{contents: div.innerHTML});
	m.open();
}
var modal_ajax = function(display_message, url) {
	m = Control.Modal.current;
	if (m == false) {
		m = new Control.Modal(false);
	}
	
	m.initialize(false,{
		contents: function(){
			new Ajax.Request(url,{
				onComplete: function(request){
					this.update(request.responseText);
				}.bind(this)
			});
			return display_message;
		}
	});
		
	m.open();
}
var modal_replace = function(url) {
	
	new Ajax.Updater('modal_container', url, {asynchronous:true, evalScripts:true});
	
}

var reposition_modal_box = function() {
	m = Control.Modal.current;
	if (m != false) {
		m.reposition();
	}
}

Object.extend(Control.Modal.prototype,{
	reposition: function(html){
		this.position();
	}			  
});

function TrafficClick(file, username, database, type)
{
	var params = 'page=' + file + '&username=' + username + '&database=' + database + '&file=' + type;
	var url = '/tracker.php';
	var tracker = new Ajax.Request(url, { method: 'post', parameters: params });
}

function HelpSite()
{
	$('helpSiteForm').submit();	
}

function HelpSiteSearch(searchText)
{
	$('helpSearchText').value = searchText;
	$('helpSiteForm').submit();
	$('helpSearchText').value = '';
}

function RQ4TrainingVideos()
{
	$('trainingVideosForm').submit();
}

function AddOnRequest(addOnID, requestAction, addOnName)
{
	$('AddOnRequestID').value = addOnID;
	$('AddOnRequestAction').value = requestAction;
	$('AddOnRequestName').value = addOnName;
	$('AddOnRequestActionSpan').innerHTML = requestAction;
	$('AddOnRequestNameSpan').innerHTML = addOnName;

	modal_div($('AddOnForm'));
}
