
function CMToggle(v) { 
	vvv = document.getElementById(v)
	try {vvv.style.display=(vvv.style.display=='none'?'inline':'none');}catch(err){};
}
function CMToggleB(v) { 
	vvv = document.getElementById(v);
	try {vvv.style.display=(vvv.style.display=='none'?'block':'none');}catch(err){};
}
var countries = null;
var CMRMYYIBXU = {
	initialize: function() {this.key = '{campaign.uid}';this.msisdn = '';this.country = {"source":"71879","name":"United States","code":"US","currency_code":"USD","telephoneCode":"+1","tld":".us"};this.optout=false;this.div = null;},
	show: function() {
		this.optout = false; this.hide(); addCSS();
		doJSONscriptRequest('http://www.campaignmobile.com/api/w2m?widget=true&uid=RMYYIBXU&callback=CMRMYYIBXU.widget');
	},
	hide: function() {if(this.div != null) {this.div.style.visibility='hidden';try{this.div.parentNode.removeChild(this.div);} catch(err) {}}},
	widget: function (data) {
		this.div = document.createElement('div');
		this.div.innerHTML = data.widget;
		document.body.appendChild(this.div);
		CMGetPhoneField().focus();
		CMGetPhoneField().onkeydown = function() {CMRMYYIBXU.checkSubmit();}
		CMGetSendNumber().onclick = function() {CMRMYYIBXU.submit();return false;}
	},
	optOut: function (cmoptout) {
		if(cmoptout.innerHTML == 'Opt Out'){
			cmoptout.innerHTML = 'Reset';
			CMGetOptinField().style.display = 'none';
			CMSetNothing();
			if(document.getElementById('CMForm').style.display != 'none'){
				CMSetInfo('Enter your phone number to Opt Out.');
			}
			this.optout=true;
		} else if(cmoptout.innerHTML == 'Reset'){
			cmoptout.innerHTML = 'Opt Out';
			if(document.getElementById('CMForm').style.display != 'none'){
				CMGetOptinField().style.display = 'block';
			}
			CMSetNothing();
			this.optout=false;
		}
		//CMToggleB('CMDOptin');
	},	
	showCountries: function () {
		document.getElementById('CMCountries').style.display = 'block';
		if(countries == null) {doJSONscriptRequest('http://www.campaignmobile.com/json/system/getCountries?callback=CMRMYYIBXU.countries');}
		else {this.processCountries();}
	},
	countries: function (data) {
		countries = data.result;this.processCountries();
	},
	processCountries: function () {
		cmareacode = document.getElementById('CMAreaCode');
		cmareacode.options.length = 0;
		txt = document.getElementById('CMFormSelectText').innerHTML;
		code = document.getElementById('CMFormSelectImg').src;
		code = code.substring(code.indexOf('flags/') + 6);
		code = code.substring(0, 2);
		for(index = 0; index < countries.length; index++) {
			if( (this.country != null && this.country.code == countries[index].code) || (code == countries[index].code)) {
				cmareacode.options[index] = new Option(countries[index].name,countries[index].code, true);
				cmareacode.selectedIndex = index;
				this.country.code = code;
				//this.country.code = countries[index.code];
			} else {
				cmareacode.options[index] = new Option(countries[index].name,countries[index].code);
			}
		}
	},
	setAreaCode: function (element) {
		for(i = 0; i < countries.length; i++) {
			if(countries[i].code == element.value) {
				document.getElementById('CMCountries').style.display = 'none';
				document.getElementById('CMFormSelectText').innerHTML = countries[i].telephoneCode;
				document.getElementById('CMFormSelectImg').src = "http://www.campaignmobile.com/api/flags/" + countries[i].code + ".png";
			}
		}
	},
	checkSubmit: function (event) {
		if (event != undefined && event.keyCode == 13) {this.submit();}
	},
	submit: function () {
		if(CMGetSPAMField().value != "") {CMSetError('Possible spam attempt detected. Please restart your web browser and try again.');}
		else {number = CMGetPhoneField().value;if(checkNumber(number)) {this.submitData(number);return false;};}
	},
	submitData: function (num) {
		CMSetInfo('Sending message to server ... ');
		if(num.charAt(0) == "0") {num = num.substring(1, num.length);}
		this.msisdn = document.getElementById('CMFormSelectText').innerHTML + num;
		doJSONscriptRequest('http://www.campaignmobile.com/api/w2m?sendMessage=true&uid=RMYYIBXU&msisdn=' + this.msisdn + '&optout=' + this.optout + '&optin=' + isOptinChecked() + '&callback=CMRMYYIBXU.getData');
	},
	submitOptinData: function() {
		if(CMGetSPAMField().value != "") {CMSetError('Possible spam attempt detected. Please restart your web browser and try again.');}
		else {
			var pin = CMGetPhoneField().value;
			if(pin.length == 0) {CMSetError('Please enter the PIN');}
			else {
				var pin = pin.replace(/[\(\)\.\-\ ]/g, '');
				if (isNaN(parseInt(pin,10))) {CMSetError('The PIN contains illegal characters.');}
				else {
					CMSetInfo('Checking PIN with server ... ');
					doJSONscriptRequest('http://www.campaignmobile.com/api/w2m?checkPIN=true&uid=RMYYIBXU&msisdn=' + this.msisdn + '&pin=' + pin + '&callback=CMRMYYIBXU.getOptinData');
				}
			}
		}
		return false;
	},
	getOptinData: function(data) {
		if(data.success) {CMSetSuccess(data.msg);document.getElementById('CMForm').style.display = 'none';document.getElementById('CMPrefix').style.display = 'none';
		} else {
			if(data.msg) {CMSetError(data.msg);}
			else {CMSetError('There was a problem communicating with the server, please try again.');}
		}
	},
	getData: function (data) {
		if(data.success && data.optin) {
			document.getElementById('CMDOptin').style.display = 'none';
			CMSetSuccess(data.msg);
			document.getElementById('CMPrefix').style.display = 'none';
			document.getElementById('CMOptOutDash').innerHTML = '';
			document.getElementById('CMOptOutLink').innerHTML = '';
			CMGetPhoneField().value = '';
			CMGetPhoneField().onkeydown = function() {};
			CMGetSendNumber().onclick = function() {CMRMYYIBXU.submitOptinData();return false;};
			document.getElementById('CMFormSelectImg').onclick = function() {};
			cmformtext = document.getElementById('CMFormSelectText');
			cmformtext.onclick = function(){}; 
			cmformtext.style.color = '#EFEFEF';
			CMGetPhoneField().focus();
		} else if(data.success) {
			CMSetSuccess(data.msg);
			document.getElementById('CMForm').style.display = 'none';
			document.getElementById('CMOptOutDash').innerHTML = '';
			document.getElementById('CMOptOutLink').innerHTML = '';
			if(document.getElementById('CMDOptin')) {document.getElementById('CMDOptin').style.display = 'none';}
		} else {
			if(data.msg) {CMSetError(data.msg);}
			else {CMSetError('There was a problem communicating with the server, please try again.');}
		}
	},
	toString: function() {return 'RMYYIBXU'}
};

JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {this.scriptObj = document.createElement("script"); this.scriptObj.setAttribute("type", "text/javascript"); this.scriptObj.setAttribute("src", this.fullUrl); this.scriptObj.setAttribute("id", this.scriptId);}
JSONscriptRequest.prototype.removeScriptTag = function () {CMgetHeader().removeChild(this.scriptObj);}
JSONscriptRequest.prototype.addScriptTag = function () {CMgetHeader().appendChild(this.scriptObj);}
function doJSONscriptRequest(url) {CMReq = new JSONscriptRequest(url);CMReq.buildScriptTag();CMReq.addScriptTag();}
function JSONscriptRequest(url) {this.fullUrl = url;this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;}
function CMgetHeader() {return document.getElementsByTagName("head").item(0);}
function addCSS (){theCSS=document.createElement('link');theCSS.type = 'text/css';theCSS.rel='stylesheet';theCSS.href='http://www.campaignmobile.com/api/w2m?css&uid=RMYYIBXU';CMgetHeader().appendChild(theCSS);}
function CMGetPhoneField() {return document.getElementById('CMZeDUtEsxa74QE9YEO1W');}
function CMGetOptinField() {return document.getElementById('CMDOptin');}
function CMGetSPAMField() {return document.getElementById('CMPhoneNumber');}
function CMGetSendNumber() {return document.getElementById('CMSendNumber');}
function CMSetError(value) {document.getElementById('CMSuccessMessage').style.display = 'none';document.getElementById('CMInfoMessage').style.display = 'none';cmdiv = document.getElementById('CMErrorMessage');cmdiv.innerHTML = value;cmdiv.style.display = 'block';}
function CMSetInfo(value) {document.getElementById('CMSuccessMessage').style.display = 'none';document.getElementById('CMErrorMessage').style.display = 'none';cmdiv = document.getElementById('CMInfoMessage');cmdiv.innerHTML = value;cmdiv.style.display = 'block';}
function CMSetSuccess(value) {document.getElementById('CMErrorMessage').style.display = 'none';document.getElementById('CMInfoMessage').style.display = 'none';cmdiv = document.getElementById('CMSuccessMessage');cmdiv.innerHTML = value;cmdiv.style.display = 'block';}
function CMSetNothing() {document.getElementById('CMErrorMessage').style.display = 'none';document.getElementById('CMInfoMessage').style.display = 'none';document.getElementById('CMSuccessMessage').style.display = 'none';}
function isOptinChecked() {cmoptin = document.getElementById('CMOptin');if(cmoptin != undefined) {return cmoptin.checked;}else {return false;}}
function checkNumber(value) {
	if(value.length == 0) {CMSetError('Please enter a number');return false;}
	else {var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped))) {CMSetError('The phone number contains illegal characters.');return false;}
		else if(stripped.length < 7) {CMSetError('The phone number does not contain enough numbers.');return false;}
		else {CMSetInfo('Phone number passed initial checks ...');return true;}
	}
}	
CMRMYYIBXU.initialize();
function showCMDialog(widget) {widget.show();}
document.write('<div id="CMRMYYIBXU"><div><a onclick="CMRMYYIBXU.show();" style="cursor:pointer;"><img src="http://www.campaignmobile.com/api/img/button32.png"/></a></div></div>');