dojo.require("dojox.validate.web");
dojo.require("dojox.validate._base");
dojo.require("dijit.Tooltip");

var initialEmailText = "";
var submitEmail = true;

dojo.addOnLoad(function() {
	if((dojo.byId("teEmailBox") != null) && (dojo.byId("te-news-form"))) {
		var locale = TE_getLocale();
		dojo.requireLocalization("te", "ewt", locale);
		var ewt = dojo.i18n.getLocalization("te", "ewt", locale);
		initialEmailText = ewt.commonEnterEmail;
		dojo.byId("teEmailBox").value = initialEmailText;
		dojo.connect(dojo.byId("te-news-form"), "onsubmit", function(evt) {
			console.log("stopped event");
			dojo.stopEvent(evt);
		});
		dojo.connect("optRegEndEvent", "sbmtEmailNewsletter");
		dojo.connect(dojo.byId("teEmailBtn"), "onclick", "stopsub");
		dojo.connect("teEmailBox", "onkeypress", function(evt) { chkForm(evt); });	    
    }	
});

function focusEmailBox() {
	var email = dojo.byId("teEmailBox");
	if (email.value == initialEmailText) {
		email.value = "";
		dojo.style(email, "color", "#000000");
		email.className = "text-box active";
	}
}

function blurEmailBox() {
	var email = dojo.byId("teEmailBox");
	if (email.value.length == 0) {
		email.value = initialEmailText;
		dojo.style(email, "color", "#999999");
		email.className = "text-box";
	}
}
function sbmtEmailNewsletter() {
	
	var emailAddr = dojo.byId("teEmailBox").value;
	
	if (emailAddr.length != 0) {
		//validate
		var isGood = dojox.validate.isEmailAddress(emailAddr);
		console.log("emailAddr isValid is: " + isGood);
		
		if (!isGood) {
			dijit.showTooltip("Please check your email address", dojo.byId("teEmailBox"), 'before');
			setTimeout("destroyMessage();", 1000);
			return false;
		}
		else {
			dojo.byId("te-news-form").submit();
			return true;
		}
	}
	else {
		destroyMessage();
	}
	return false;
}
function stopsub() {
	console.log("stopSub");
	optReg("optRegEmailSignUp", false);
}

function destroyMessage() {
	dijit.hideTooltip(dojo.byId("teEmailBox"));
}

function chkEForm(e)
{
	if (e.keyCode == dojo.keys.ENTER)
	{
		stopsub();
	}
}
dojo.addOnLoad(function(){
	var cprefs = new te.epd.PreferenceHelperService();
	var pubProf = new te.epd.ProfileHelperService();
	var cscope = "USO"; var csEmail = "";
	var ckey = "sendEmail"; 
	var cewt5c = "TECewt5";
	var isDisti = 0;
	var cuuid = dojo.cookie(cewt5c);
	if ((cuuid !=undefined) && (cuuid != "test")) {
		pubProf.retrievePublicProfileByTrackingUUID(function(profo) {
			if (TE_getEnv().toUpperCase() == "DEV"){console.log("call successful:" + profo.status == cprefs.SUCCESSFUL)}
			if(profo.status == cprefs.SUCCESSFUL){
				console.log("user= " + profo.usertype + " Disti=" + profo.isDistributor);
				if ((profo.isDistributor == 1) || (profo.usertype == "Employees")) {
					dojo.style(dojo.byId("custCont"), "display", "block");
				}
				else {
					dojo.style(dojo.byId("custCont"), "display", "none");
				}
			}
		}, cuuid);
		if (dojo.byId("reqRes") != undefined) {
			cprefs.findPrefByScopeKeyAndTrackingId(cscope, ckey, function(prefObj){
				console.log(prefObj.status == cprefs.SUCCESSFUL);
				if(prefObj.status == cprefs.SUCCESSFUL){
					csEmail = prefObj.preferences[0].value;
					console.log('prefs: ' + prefObj.preferences[0].value);
					if (csEmail == 0) {
						dojo.byId("reqRes").innerHTML = "<a href=\"javascript:loginPopup_showPopup('" + TE_getSecureWWWServerUrl() + "/commerce/esr/int/request_secure_resources.do')\">Request Secure Resources</a>";
					}
					else if ((csEmail == 1) || (csEmail == 2)) {
						dojo.byId("reqRes").innerHTML = "<span style=\"color:#999999;\">Request Secure Resources</span>";
					}
					else {
						dojo.byId("reqRes").innerHTML = "<a href=\"" + TE_getSecureWWWServerUrl() + "/commerce/secure/commerce.asp?ID=26\">Request Secure Resources</a>";
					}
				}
			}, cuuid);
		}
	}
	var menu = new te.global.Menu();
});
