var craigslist = {
	populateLocation : function() {
		var clLoc = _gel('clLocationId'), i = 0; clLoc.options.length = 0;
		for (var id in craigsList.cities) { clLoc.options[i] = new Option(craigsList.cities[id],id); i++; }
	},	
	populateCategory : function() {
		var clCat = _gel('clCategoryId'); clCat.options.length = 0;
		for (var i=0; i<craigsList.cats.length; i++) clCat.options[i] = new Option(craigsList.cats[i].name,craigsList.cats[i].id);
	},	
	refreshSubCats : function() {
		var clCat = _gel('clCategoryId'), currCatIndex = clCat.selectedIndex, clSubCat = _gel('clSubjectId'); clSubCat.options.length = 0;
		_dsp('clSubjectDiv',craigsList.cats[currCatIndex].sub != null);
		if (craigsList.cats[currCatIndex].sub != null) { var i = 0; for (var id in craigsList.cats[currCatIndex].sub) { clSubCat.options[i] = new Option(this.capWords(craigsList.cats[currCatIndex].sub[id]),id); i++; } }
	},
	getQueryFromUrl : function(rssUrl) {
		var beg = rssUrl.indexOf('?query=') + '?query='.length, end = rssUrl.indexOf('&format=');
		return unescape(rssUrl.slice(beg,end));
	},	
	getCityIdFromUrl : function(rssUrl) { // var rssUrl = 'http://sfbay.craigslist.org/search/apa?query=&format=rss';
		var beg = 'http://'.length, end = rssUrl.indexOf('.craigslist.org');
		return rssUrl.slice(beg,end);
	},	
	getSubCatFromUrl : function(rssUrl) {
		var beg = rssUrl.indexOf('.craigslist.org') + '.craigslist.org/search/'.length, end = rssUrl.indexOf('?query=');
		return rssUrl.slice(beg,end);
	},	
	deduceCatIdFromSubCatId : function(subCatId) {
		for (var i=0; i<craigsList.cats.length; i++) {
			if (craigsList.cats[i].id=='eee' || craigsList.cats[i].id=='res') continue;
			for (var id in craigsList.cats[i].sub) { if (id == subCatId) return craigsList.cats[i].id; }
		}
	},	
	capWords : function(str) { // Capitalize all words in a string
		var inputString = str, tmpStr, tmpChar, preString, postString, strlen; tmpStr = inputString.toLowerCase(); stringLen = tmpStr.length;
		if (stringLen > 0) { for (i = 0; i < stringLen; i++) { if (i == 0)	{ tmpChar = tmpStr.substring(0,1).toUpperCase(); postString = tmpStr.substring(1,stringLen); tmpStr = tmpChar + postString; } else { tmpChar = tmpStr.substring(i,i+1); if (tmpChar == " " && i < (stringLen-1)) { tmpChar = tmpStr.substring(i+1,i+2).toUpperCase(); preString = tmpStr.substring(0,i+1); postString = tmpStr.substring(i+2,stringLen); tmpStr = preString + tmpChar + postString; } } } }
		return tmpStr;
	}
}

var rssUI = {
	fldIds : { alertObject:'alertObject', deliveryType:'deliveryType', alertName:'alertName' },
	checkAllDays : function(checkOrNot) { for (var i=0; i<dayCheckboxes.length; i++) _chk(dayCheckboxes[i], checkOrNot); },
	isDaily : function() { return _isChecked('radioDaily'); },
	loadCraigslistUrl : function(rssUrl) {
		var clCityId = craigslist.getCityIdFromUrl(rssUrl), clSubCatId = craigslist.getSubCatFromUrl(rssUrl);
		var clCatId = craigslist.deduceCatIdFromSubCatId(clSubCatId), clQuery = craigslist.getQueryFromUrl(rssUrl); // craigslist search keyword
		if (clCatId==null) clCatId = clSubCatId; //alert('City= '+clCityId+' Cat='+clCatId+' SubCat='+clSubCatId);			
		_gel('clLocationId').value = clCityId; _gel('clCategoryId').value = clCatId; craigslist.refreshSubCats();
		_gel('clSubjectId').value = clSubCatId; _gel('fldKeywords').value = clQuery!=null?clQuery:'';	
	},
	buildRssUrlFromCL : function() { // create craigslist url, sample url: http://sfbay.craigslist.org/search/hhh?query=view&srchType=B&format=rss
		var clLocationId = _val('clLocationId'), clCategoryId = _val('clCategoryId'), clSubjectId  = _val('clSubjectId');
		if (clCategoryId == "eee" || clCategoryId == "res") clSubjectId = clCategoryId; // events and resumes do not have subjects, use category instead
		return "http://" + clLocationId + ".craigslist.org/search/" + clSubjectId + "?query=" + escape(_trim(_val('fldKeywords'))) + "&format=rss";
	},
	extractAndShowCLKeyword : function() {
		_gel('fldKeywords').value = craigslist.getQueryFromUrl(_val(ID.alertObject));
	},
	collectInputs : function() {
		var t = rssUI, ID = t.fldIds;
		_gel(ID.deliveryType).value = rssUI.isDaily() ? _val('selDailyType') : 'event';		
		if (t.isCraigslist) _gel(ID.alertObject).value = t.buildRssUrlFromCL();
		else if (t.isRealNames) {
			_gel(ID.alertObject).value = 'http://www.4infoalerts.com/services/realnames/realnames-rss.xml';
			_gel(ID.deliveryType).value = 'daily.latest';
			_gel(ID.alertName).value = 'Real Names';
		}
	},
	onChooseWhen : function() {
		var isDaily = rssUI.isDaily();
		_dsp('divEventOptions', !isDaily);
		_dsp('divDailyOptions', isDaily);
		if (isDaily) checkAllIfNoneChecked(arrDayIds);
	},
	initCraigslistMenus : function() {
		craigslist.populateLocation();
		craigslist.populateCategory();
		_gel('clLocationId').value = 'sfbay';
		_gel('clCategoryId').value = 'hhh';		
		craigslist.refreshSubCats();		
	},
	init : function(obj) {
		var t = rssUI, defTo = obj ? obj.defaultTo : 'customRss', ID = t.fldIds;
		if (t.isEditMode) {
			ID.alertObject = 'alert_alertObject';
			ID.deliveryType = 'alert_deliveryType';
			ID.alertName = 'alert_alertName';
		}
		t.isCraigslist = defTo=='Craigslist', t.isCustomRss = defTo=='Custom RSS', t.isRealNames = defTo=='Real Names';
		if (t.isCraigslist) { 
			t.initCraigslistMenus(); 
			if (t.isEditMode) rssUI.loadCraigslistUrl(_val(ID.alertObject));
		}
		t.onChooseWhen();
	}
}

function validateInput() {
	var ID = rssUI.fldIds;
	rssUI.collectInputs();
	
	if (rssUI.isCraigslist && !_trim(_val('fldKeywords'))) {
		var contAnyway  = confirm('You have not chosen any keyword filters. Please note that you may receive frequent alert messages as this feed is updated many times a day. To create this alert anyway, click OK.  Otherwise, click Cancel and enter one or more keywords.');	
		if(!contAnyway) { _foc('fldKeywords'); return false; }
	}
	
	if (!_trim(_val(ID.alertName))) {
		alert('Please enter a name for this alert to continue.');
		_foc(ID.alertName);
		return false;
	}
	
	if (rssUI.isDaily() && !atLeastOneChecked(arrDayIds)){
		alert(ERROR.DAYS_OF_WEEK);
		return false;
	}
	return true;
}