var cnbc_ctc_allNodes = null;
var cnbc_ctc_casToken = "";
var cnbc_ctc_enc = "";
var cnbc_ctc_errorTimer;
var ctc_rootURL = "http://login.cnbc.com";
function cnbc_ctc_getElementsByClassName(cl){
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = document.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	 	var classes = elem[i].className;
	 	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}
function cnbc_ctc_showId(showId, remove){
	var parentElm = document.getElementById("ctcwrapper_main");
	if(cnbc_ctc_allNodes == null){
	 	cnbc_ctc_allNodes = cnbc_ctc_getElementsByClassName('ctcwrapper');
	}
	for(var x=0; x< cnbc_ctc_allNodes.length; x++){
	  if(cnbc_ctc_allNodes[x].id == showId){
	  	cnbc_ctc_allNodes[x].style.display="block";
	 	}else{
	  	if(remove){
	   		parentElm.removeChild(document.getElementById(cnbc_ctc_allNodes[x].id));
	  	}else{
	   		cnbc_ctc_allNodes[x].style.display="none";
	  	}
	 	}
	}
}
function cnbc_ctc_showError(errorMsg, reset){
	if(reset){
		document.getElementById("dowValue_notOpen").innerHTML = "----.--";
		document.getElementById("snpValue_notOpen").innerHTML = "----.--";
	}
	document.getElementById("ctc_error").innerHTML = errorMsg;
	document.getElementById("ctc_notOpen_header").innerHTML = "Track who\'s in the lead on <a href='http://calltheclose.cnbc.com/close_callers.html'>Close Callers</a>";
	cnbc_ctc_showId('ctc_notOpen', true);
}
function cnbc_ctc_isUserLoggedIn(){
	cnbc_ctc_casToken = cnbc_readCookie("CASTOKEN");
	cnbc_ctc_enc = cnbc_readCookie("c_enc");
	try{
		cnbc_ctc_casToken = cnbc_ctc_casToken.replace(/'/g, "");
		cnbc_ctc_casToken = cnbc_ctc_casToken.replace(/"/g, '');
		cnbc_ctc_enc = cnbc_ctc_enc.replace(/'/g, "");
		cnbc_ctc_enc = cnbc_ctc_enc.replace(/"/g, '');
		cnbc_ctc_enc = encodeURIComponent(cnbc_ctc_enc);
	}catch(e){}
	var subInfo = cnbc_readCookie("SUBSCRIBERINFO");
	if(cnbc_ctc_enc != null && cnbc_ctc_enc != 'null' && cnbc_ctc_enc != '' && cnbc_ctc_casToken != null && cnbc_ctc_casToken != 'null' && cnbc_ctc_casToken != '' && subInfo != null && subInfo != 'null' && subInfo != ''){
		return true;
	}else{
		return false;
	}
}
function cnbc_ctc_initCTC(){
	if(cnbc_ctc_isUserLoggedIn()){
		var isOptedInURL = ctc_rootURL + "/tpauth/rest/optin/check.xml?source=contest&source_type=calltheclose&email=" + cnbc_ctc_casToken + "&token=" + cnbc_ctc_enc;
		getData_FlashProxy(isOptedInURL,"cnbc_ctc_handleIsOptedInResponse");
		cnbc_ctc_errorTimer = setTimeout("cnbc_ctc_showError('There was an error processing your request', true)",10000);
	}else{
		cnbc_ctc_showId('ctc_notSignedIn', true);
	}
}
function cnbc_ctc_handleIsOptedInResponse(data){
	clearTimeout(cnbc_ctc_errorTimer);
	if(data != null && data != 'null' && data != ''){
		var resultDoc = getXML(data);
		var isOptedIn = resultDoc.getElementsByTagName("optinstatus")[0].childNodes[0].nodeValue;
		if(isOptedIn == 'true'){
			var getGuessURL = ctc_rootURL + "/tpauth/rest/contest/calltheclose/guess.xml?email=" + cnbc_ctc_casToken + "&token=" + cnbc_ctc_enc;
			getData_FlashProxy(getGuessURL,"cnbc_ctc_handleGetGuessResponse");
			cnbc_ctc_errorTimer = setTimeout("cnbc_ctc_showError('There was an error processing your request', true)",10000);
		}else{
			document.getElementById("ctc_register_signin_links").innerHTML = "<a href='" + ctc_rootURL + "/tpauth/rest/authenticate?source=contest&source_type=calltheclose'>Start Playing</a>";
			cnbc_ctc_showId('ctc_notSignedIn', true);
		}
	}else{
		cnbc_ctc_showError("There was an error processing your request", true);
	}
}
function cnbc_ctc_handleGetGuessResponse(data){
  clearTimeout(cnbc_ctc_errorTimer);
  if(data != null && data != 'null' && data != ''){
	  var resultDoc = getXML(data);
	  var status = resultDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
	  if(status == '0' || status == '5'){
			var dow = resultDoc.getElementsByTagName("dow")[0].childNodes[0].nodeValue;
			var snp = resultDoc.getElementsByTagName("snp")[0].childNodes[0].nodeValue;
			if(dow != null && dow != "" && dow > 0){
				document.getElementById("dowValue_open").innerHTML = dow;
				document.getElementById("dowValue_revising").value = dow;
				document.getElementById("dowValue_notOpen").innerHTML = dow;
			}
			if(snp != null && snp != "" && snp > 0){
				document.getElementById("snpValue_open").innerHTML = snp;
				document.getElementById("snpValue_revising").value = snp;
				document.getElementById("snpValue_notOpen").innerHTML = snp;
			}
			var getOnTimeURL = ctc_rootURL + "/tpauth/rest/contest/calltheclose/ontime.txt?source=contest&source_type=calltheclose&email=" + cnbc_ctc_casToken + "&token=" + cnbc_ctc_enc;
			getData_FlashProxy(getOnTimeURL,"cnbc_ctc_handleTimeResponse");
			cnbc_ctc_errorTimer = setTimeout("cnbc_ctc_showError('There was an error processing your request', false)",10000);
		}else{
		 	cnbc_ctc_showError("There was an error processing your request", true);
		}
  }else{
		cnbc_ctc_showError("There was an error processing your request", true);
  }
}
function cnbc_ctc_handleTimeResponse(data){
	clearTimeout(cnbc_ctc_errorTimer);
	if(data != null && data != 'null' && data != ''){
		if(data == "true"){
			if(document.getElementById("dowValue_revising").value == ''){
			 	cnbc_ctc_showId('ctc_revising', false);
			}else{
			 	cnbc_ctc_showId('ctc_open', false);
			}
		}else{
			cnbc_ctc_showId('ctc_notOpen', true);
		}
	}else{
	 	cnbc_ctc_showError("There was an error processing your request", false);
	}
}
function cnbc_ctc_submitCall(){
	var dowRevise = document.getElementById("dowValue_revising");
	var snpRevise = document.getElementById("snpValue_revising");
	var reviseErrorObj = document.getElementById("ctc_revising_error");
	reviseErrorObj.innerHTML = "";
	if(dowRevise.value == "" || snpRevise.value == ""){
		reviseErrorObj.innerHTML = "Please enter a guess";
	}else if(cnbc_ctc_isSpecialPresent(dowRevise)){
		reviseErrorObj.innerHTML = "Please enter a valid guess";
	}else if(cnbc_ctc_isSpecialPresent(snpRevise)){
		reviseErrorObj.innerHTML = "Please enter a valid guess";
	}else if(dowRevise.value <= 0 || snpRevise.value <= 0){
		reviseErrorObj.innerHTML = "Please enter a valid guess";
	}else if(dowRevise.value > 99999.99 || snpRevise.value > 99999.99){
		reviseErrorObj.innerHTML = "Please enter a valid numeric value < 100,000";
	}else if(dowRevise.value > 0 && snpRevise.value > 0){
		document.getElementById("submitBtn").innerHTML = '<img src="http://media.cnbc.com/i/CNBC/CNBC_Images/registration/loader.gif" alt="Loading"/>&nbsp;Submitting';
		var getOnTimeURL = ctc_rootURL + "/tpauth/rest/contest/calltheclose/ontime.txt?source=contest&source_type=calltheclose&email=" + cnbc_ctc_casToken + "&token=" + cnbc_ctc_enc;
		getData_FlashProxy(getOnTimeURL,"cnbc_ctc_handleSubmitTimeResponse");
		cnbc_ctc_errorTimer = setTimeout("cnbc_ctc_showError('There was an error processing your request', false)",10000);
	}
}
function cnbc_ctc_isSpecialPresent(obj){
 var fieldValue = obj.value;
 var basic = "1234567890.";
 for(i=0;i<basic.length;i++){
   if(basic.indexOf(fieldValue.charAt(i)) == -1){
     return true;
   }
 }
 return false;
}
function cnbc_ctc_handleSubmitTimeResponse(data){
	clearTimeout(cnbc_ctc_errorTimer);
	if(data != null && data != 'null' && data != ''){
		if(data == "true"){
			var dow = document.getElementById("dowValue_revising").value;
			var snp = document.getElementById("snpValue_revising").value;
			var submitCallURL = ctc_rootURL + "/tpauth/rest/contest/calltheclose/saveguess.xml?email=" + cnbc_ctc_casToken + "&token=" + cnbc_ctc_enc + "&dow=" + dow + "&snp=" + snp;
			getData_FlashProxy(submitCallURL,"cnbc_ctc_handlePostGuessResponse");
			cnbc_ctc_errorTimer = setTimeout("cnbc_ctc_errorPosting()",10000);
		}else{
			cnbc_ctc_showError("You cannot submit your call at this time. Please check back after market close.", false);
		}
	}else{
		cnbc_ctc_showError("There was an error processing your request", false);
	}
}
function cnbc_ctc_handlePostGuessResponse(data){
	clearTimeout(cnbc_ctc_errorTimer);
	if(data != null && data != 'null' && data != ''){
		var resultDoc = getXML(data);
		var status = resultDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
	  if(status == '0'){
			var dow = resultDoc.getElementsByTagName("dow")[0].childNodes[0].nodeValue;
			var callTime = resultDoc.getElementsByTagName("created")[0].childNodes[0].nodeValue;
			document.getElementById("callSubmission").innerHTML = dow;
			document.getElementById("callTime").innerHTML = callTime;
			cnbc_ctc_showId('ctc_thankYou', true);
		}else if(status == '7'){
	  	cnbc_ctc_showError("You cannot submit your call at this time. Please check back after market close.", false);
	  }else{
			cnbc_ctc_errorPosting();
		}
	}else{
	 	cnbc_ctc_errorPosting();
	}
}
function cnbc_ctc_errorPosting(){
	document.getElementById("submitBtn").innerHTML = '<a class="ctc_submitBtn" href="javascript:cnbc_ctc_submitCall();">Submit</a>';
	document.getElementById("ctc_open_error").innerHTML = "There was an error posting your guess. Please try again.";
	cnbc_ctc_showId('ctc_open', false);
}
function cnbc_strReplaceCharCodes (str, replaceArray){
	for (var i in replaceArray){
		var replaceMap = replaceArray[i];
		for (var charCodeToReplace in replaceMap){
			var stringToReplaceWith = replaceMap[charCodeToReplace];
			str = cnbc_replaceCharStr(str, charCodeToReplace,stringToReplaceWith);
		}
	}
	return str;
}
function cnbc_replaceCharStr (str, charCodeToReplace, stringToReplaceWith){
    var charToReplace = String.fromCharCode(charCodeToReplace);
    var index = str.indexOf(charToReplace);
    while (index >= 0){
      str = str.replace(charToReplace, stringToReplaceWith);
      index = str.indexOf(charToReplace);
    }
    return str;
}
function cnbc_submit_commentsform(thisform, url){	
	var charReplaceArray = 
	[
	  {8217: "&rsquo;"},
		{8220: "&ldquo;"}, 
	  {8221: "&rdquo;"},
		{34: "&quot;"},
		{39: "&#039"},
		{8218: "&sbquo;"},
		{8216: "&lsquo;"},
		{8222: "&bdquo;"}
	];
	var cnbc_userComment =cnbc_strReplaceCharCodes(thisform.content.value, charReplaceArray);
	cnbc_showLoader('edit_submit','Submitting Comment', 'submit_btn');
	cnbc_com_cacheParams = cnbc_com_cacheParams +"&page="+ cnbc_com_futComid;
	url = url+"?view=json&contentType="+thisform.contenttype.value+"&contentID="+thisform.contentid.value+"&commentText="+encodeURIComponent(cnbc_userComment)+"&partnerId="+cnbc_com_partnerid+"&authkey="+cnbc_readCookie('cnbcCommentsAuthKeyCookie')+"&cachebuster="+encodeURIComponent(cnbc_com_cacheParams)+"&location="+encodeURIComponent(window.location.href);
	document.getElementById("edit_but").style.display = "none";
	getData_FlashProxy(url, "cnbc_SubmitResponseHandler");
	return false;
}
