// this file needs to include commonnew.js, ajaxcaller and ajaxutils
function submit_mkt_feedback(frm,btn) {
	with(frm) {
		if(mktfeedback.value.replace(/ /gi,'') == '') {
			alert('Please enter your feedback.');
			putClass('fbimgloader','gone');
			putClass('btnmktfbsubmit','notgone');
			return false;
		}
	}
	form_postForPlainText('/equities/mkt_feedback.cfm',frm,callLoadPage,null,btn,'mktfeedbackerror');
	return false;
}
function callLoadPage(text,callingContext,headers,arrParams) {
	window.document.getElementById(callingContext).innerHTML = text;
	setJSFunctions(callingContext,text);
}
function navRatios(lststr,ctr,mktcode) {
	var imgPrev = document.getElementById('imgRatioPrev');
	var imgNext = document.getElementById('imgRatioNext');
	arrRatios = lststr.split(",");
	elLabel = document.getElementById('lblRatios');
	currpos += ctr;
	if(currpos < 1 || currpos > arrRatios.length) {
		currpos -= ctr;
	}
	else {
		document.getElementById('ratiostable').className = 'gone';
		document.getElementById('ratioloader').className = 'notgone';
		ajaxCaller.getPlainText("/equities/ana_ratios.cfm?ajax&mktcode=" + mktcode + "&dt=" + arrRatios[currpos-1], loadData, 'ratioscontent');
	}
	elLabel.innerHTML = (arrRatios[currpos-1] == 'Latest' || arrRatios[currpos-1] == 'Current') ? arrRatios[currpos-1] : Right(arrRatios[currpos-1],4);
	return false;
}
function newWindow(marketurl) { 
	window.open(marketurl, 'marketinfo', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=600,height=550'); 
}
function setCustomFromToValues() {
	document.getElementById("datefrom").value = document.getElementById("datefromCustom").value;
	document.getElementById("dateto").value = document.getElementById("datetoCustom").value;
}
function getElementsByClassName(classname, node) {    
	if(!node) node = document.getElementsByTagName("body")[0];    
	var a = [];    
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");    
	for(var i=0,j=els.length; i<j; i++)        
		if(re.test(els[i].className))
			a.push(els[i]);    
	return a;
}	
function isCalendarShown() {
	var objs = getElementsByClassName("calendar",document.body);
	for(i=0; i<objs.length;i++) {
		if(objs[i].style.display == 'block') return true;
	}	
	return false;
}
function putDates(from,to,datecode) {
	var frm = document.getElementById('frmDates');
	with (frm) {
		fromdate.value = from;
		todate.value = to;
		dtcode.value = datecode;
		lblfrom = from.replace(/-/gi,'');
		lblfrom = Left(lblfrom,5) + Right(lblfrom,2);
		lblto = to.replace(/-/gi,'');
		lblto = Left(lblto,5) + Right(lblto,2);
		document.getElementById('btnmenu2').style.width = "auto";
		el_lbl = document.getElementById('lblmktdates');
		el_lbl.innerHTML = 'Period';
		el = document.getElementById('mktdates');
		el.innerHTML = '(' + lblfrom + ' to ' + lblto + ')';
		if(mktcode.value != '') {
			if(!submitDatesForm(frm)) 
				return false;
			submit();
		} 
	}
	return false;
}
function submitThis(code,lbl) {
	var frm = document.getElementById('frmDates');
	if (typeof(lbl) == 'undefined' || typeof(lbl) == 'null' || lbl == '') reload = '';
	document.getElementById('btnmenu1').style.width = "auto";
	el_lbl = document.getElementById('lblmktlabel');
	el_lbl.innerHTML = 'Market';
	el = document.getElementById('mktlabel');
	el.innerHTML = lbl;
	with (frm) {
		mktcode.value = code;
		if(fromdate.value != '' && todate.value != '') {
			if(!submitDatesForm(frm)) 
				return false;
			submit();
		} 
	}
	return false;
}
function submitDatesForm(frm) {
	with(frm) {
		if(!(dt2num(fromdate.value) <= dt2num(todate.value) && dt2num(fromdate.value) <= dt2num(dtnow.value) && dt2num(todate.value) <= dt2num(dtnow.value))) {
			alert('Invalid dates, please try again.');
			return false;
		}
	}
	return true;
}

function getDates (frm, sel) {
	with (frm) {
		fromdate.value = sel.options[sel.selectedIndex].getAttribute("from");
		todate.value = sel.options[sel.selectedIndex].getAttribute("to");
		dtcode.value = sel.options[sel.selectedIndex].value;
		if(dtcode.value == 'c') 
			putClass('mkt_ana_dates','notgone');
		else
			putClass('mkt_ana_dates','gone');	
	}
	return false;
}
function submitMktAnaForm(frm) {
	with (frm) {
		if(trim(mktcode.value) == '') {
			alert("Please select Market.");
			return false;
		}
		if(trim(fromdate.value) == '' || trim(todate.value) == '') {
			alert("Please select Period.");
			return false;
		}
		if(!(dt2num(fromdate.value) <= dt2num(todate.value) && dt2num(fromdate.value) <= dt2num(dtnow.value) && dt2num(todate.value) <= dt2num(dtnow.value))) {
			alert('Invalid dates, please try again.');
			return false;
		}
	}
	return true;
}

function toggCurrency(num) {
	try {
		for(i=1; i<=num; i++) {
			try { 
					el = document.getElementById("tradinganalysis_" + i);
					el.className = el.className.indexOf('local') != -1 ? el.className.replace('local','usd') : el.className.replace('usd','local');
			} catch(e) {}	
		}
	} catch(e) {}	
	return false;
}