function toggleItem(itemID){
	if(document.getElementById(itemID).style.display == 'none'){
		document.getElementById(itemID).style.display='inline';
	}else{
		document.getElementById(itemID).style.display='none';
	}
}

function checkForm(theForm){
	for(i=0; i<document.getElementById(theForm).elements.length; i++){
		if(document.getElementById(theForm).elements[i].value == ''){
			alert('Please enter a value for the '+document.getElementById(theForm).elements[i].name+' field');
			document.getElementById(theForm).elements[i].focus();
			return false;
		}
	}
}

function selLink(theMenu){
	window.location.href=document.getElementById(theMenu).value;
}

function confirmAction(theMessage) {
	if(confirm(theMessage)){ return true ; }
	else{ return false ; }
}

function popUp(theUrl,winName,theOpts){
	window.open(theUrl,winName,theOpts);
}

function go(thePage){
	window.location.href=thePage;
	pageTracker._trackPageview(thePage);
}

function emailForm(){
	if(document.getElementById('widget_output_year').innerHTML == ''){
		alert('You must calculate savings before mailing the results.');
	}else{
		var daReferrer = document.referrer;
		var email = "";
		var link = document.getElementById('widget_permalink').value;
		var subject = "Golden Eye DMS";
		var body_message = "One of your colleagues has sent you a link to find out how much you can save with Golden Eye DMS.%0A%0AClick here to see your savings: "+link+"%0AClick here to visit the Golden Eye DMS website: http://goldeneyedms.com";	
		var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;
		win = window.location=mailto_link;
		pageTracker._trackPageview("/kiosk/tell-a-friend");
	}
} 

function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function checkWidgetForm() {
	if(document.getElementById('widget_users').value == ''){
		alert('Please enter number of users.');
		document.getElementById('widget_users').focus();
		return false;
	}
	else if(document.getElementById('widget_provider').value == '' && document.getElementById('widget_costs').value == ''){
		alert('Please enter your costs OR select a provider.');
		document.getElementById('widget_costs').focus();
		return false;
	}
	else return true;
}

function dmsCalc(baseURL) {
	if(checkWidgetForm()){
	
		var provider = document.getElementById('widget_provider').value;
		var users = document.getElementById('widget_users').value;
		var costs = document.getElementById('widget_costs').value.replace(/\,/g,'');
		
		if(provider == 'rr') var providerPrice = '157';
		if(provider == 'adp') var providerPrice = '137';
		if(provider == 'ucs') var providerPrice = '172';
		if(provider == 'arkona') var providerPrice = '97';
		
		var geCost = '68' * users;
		if(costs == '') var providerCost = providerPrice * users;
		else var providerCost = Math.round(costs);
		
		document.getElementById('widget_output_month').innerHTML = '$' + addCommas(providerCost - geCost);
		document.getElementById('widget_output_year').innerHTML = '$' + addCommas((providerCost - geCost) * 8);
		document.getElementById('widget_output_five').innerHTML = '$' + addCommas((providerCost - geCost) * 54);
		document.getElementById('widget_permalink').value = baseURL + '/plink/' + costs + '|' + provider + '|' + users;
		
		ajax('http://www.goldeneyedms.com/widget/tracking.php?users='+users+'&provider='+provider+'&costs='+costs);
		
	}
	return false;
}

function displayCode(whatDiv) {
	document.getElementById('widget_embed').style.display='none';
	document.getElementById('widget_permalink').style.display='none';
	document.getElementById(whatDiv).style.display='block';
	document.getElementById(whatDiv).focus();
	document.getElementById(whatDiv).select();
	pageTracker._trackPageview("/kiosk/"+whatDiv);
}

function printMe() {
	if(document.getElementById('widget_output_year').innerHTML == ''){
		alert('You must calculate savings before printing the results.');
	}else{
		window.print();
		pageTracker._trackPageview("/kiosk/print");
	}
}

function editBlock() {
	document.getElementById('text_display').style.display='none';
	document.getElementById('text_edit').style.display='block';
}

function previewBlock() {
	document.getElementById('text_edit').style.display='none';
	document.getElementById('text_display').style.display='block';
	document.getElementById('text_display').innerHTML = document.getElementById('text_content').value;
}

function ajax(theCall) {
    
    var xhr;
    
    if (window.XMLHttpRequest) { // non-IE
        
        xhr = new XMLHttpRequest();
        
    } else if (window.ActiveXObject) { // IE
    
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //assign a handler for the response
    xhr.onreadystatechange = function() {
    	
    	if(xhr.readyState == 4){
        	
			if(xhr.status  == 200) {
				// document.getElementById(theDiv).innerHTML = xhr.responseText;
				var success = true;
			} else 
				alert('Cannot contact server: ' + theCall);
				
    	}
    }
	
    // open page
    xhr.open('GET', theCall, true);
    xhr.send(null);
}

function newFaqCat() {

	var newCat = prompt("Please enter a name for the new category.");
	
	if(newCat != '' && newCat != null){
	
		var myForm = document.createElement("form");
		myForm.method="post" ;
		myForm.action = "/admin/insert";
		
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "table") ;
		myInput.setAttribute("value", "faq_category");
		myForm.appendChild(myInput) ;
	
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "name") ;
		myInput.setAttribute("value", newCat);
		myForm.appendChild(myInput) ;
		
		document.body.appendChild(myForm) ;
		myForm.submit() ;
		document.body.removeChild(myForm) ;
	
	}else{
	
		return false;
	
	}
	
}

function editFaqCat(faqName,faqID) {

	var newCat = prompt("Edit this field as desired.", faqName);
	
	if(newCat != '' && newCat != null){

		var myForm = document.createElement("form");
		myForm.method="post" ;
		myForm.action = "/admin/updateb";
		
		var myInput = document.createElement("input");
		myInput.setAttribute("name", "table");
		myInput.setAttribute("value", "faq_category");
		myForm.appendChild(myInput) ;
	
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "id") ;
		myInput.setAttribute("value", faqID);
		myForm.appendChild(myInput) ;
	
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "name") ;
		myInput.setAttribute("value", newCat);
		myForm.appendChild(myInput) ;
		
		document.body.appendChild(myForm) ;
		myForm.submit() ;
		document.body.removeChild(myForm) ;

	}else{
	
		return false;
	
	}
}