/* ajax request */
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

/* global request object */
var http = createRequestObject();

/* send action to this page 
   tack on more arguments as needed with '&' and '='
*/
function doSend(action) {
    http.open('get', '/ajax/catalogSaleCallbacks.py?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

/* ajax callback function 
   by convention, results return [actionname]`[data]
   splitting on backtick separates, then switch on action name
   allows different actions to be handled differently
*/
function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var array = response.split('`');
        switch(array[0]){
	case 'resortNonMember':
		document.getElementById('catalogNonMemberSales').innerHTML=array[1];
		break;
	case 'resortMember':
		document.getElementById('catalogMemberSales').innerHTML=array[1];
		break;
	default:
		break;
	}
    }
}

function resortNonMember(sortby){
	doSend('resortNonMember&sort='+sortby);
}

function resortMember(sortby){
	doSend('resortMember&sort='+sortby);
}

function showSales(){
	var menustr = 'Today\'s Sales';
	menustr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	menustr += '<a href=\"\" onclick=\"showDisclaimer();return false;\">Disclaimer</a>';
	menustr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	menustr += '<a href=\"\" onclick=\"showHelp();return false;\">Help out</a>';

	var textstr = 'Here\'s what\'s on sale at your co-op today<br />';

	document.getElementById('catsalemenu').innerHTML = menustr;
	document.getElementById('catsalemenutext').innerHTML = textstr;
}

function showDisclaimer(){
	var menustr = 'Disclaimer';
	menustr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	menustr += '<a href=\"\" onclick=\"showHelp();return false;\">Help out</a>';

	var textstr = 'These prices are provided as a best-effort, <b>informational</b> service. ';
	textstr += 'In the event of a discrepancy between the in-store price and the online price, ';
	textstr += 'the in-store price is correct. Sale items cannot be guaranteed in stock. ';

	document.getElementById('catsalemenu').innerHTML = menustr;
	document.getElementById('catsalemenutext').innerHTML = textstr;
}

function showHelp(){
	var menustr = '<a href=\"\" onclick=\"showDisclaimer();return false;\">Disclaimer</a>';
	menustr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	menustr += 'Help out';

	var textstr = 'Want to improve this service for everyone, yourself included?<br />';
	textstr += 'For a variety of reasons, the names of products and brands aren\'t as ';
	textstr += 'accurate as they could be. If you click on an item, you can submit suggestions ';
	textstr += 'for more fitting names. Users may also tag items with an in-store shelf location ';
	textstr += 'or even a picture. Click on the item\'s name, then <i>User Submission Form</i>.';
	textstr += '<br /><br />'
	textstr += 'All user submissions are greatly appreciated, but changes do not go into affect ';
	textstr += 'instantly; they are first screened (by a person) to prevent spam.';  

	document.getElementById('catsalemenu').innerHTML = menustr;
	document.getElementById('catsalemenutext').innerHTML = textstr;
}

