
// Below are the message definitions that are used in the sample SMS meeting notifications.
// They should have the keywords: _FIRSTNAME_, _LASTNAME_, _BIO_, and _TELNUM_.
// These definitions must be kept in sync with those in the engine/gms/config/arnomsgtext.properties file.
// If changes are made to the definitions below, then they must also be made to said file.
// Note also that the definitions below must be quoted with semi-colons at the end,
// while those in the said file above are not.
USER_PROXIMITY_MATCH_MSG_WITH_PHONENUM_WITH_BIO = "_FIRSTNAME_ _LASTNAME_, _BIO_, is nearby. Call _TELNUM_ to connect.";
USER_PROXIMITY_MATCH_MSG_WITH_PHONENUM_WO_BIO   = "_FIRSTNAME_ _LASTNAME_ is nearby. Call _TELNUM_ to connect.";
USER_PROXIMITY_MATCH_MSG_WO_PHONENUM_WITH_BIO   = "_FIRSTNAME_ _LASTNAME_, _BIO_, is nearby.";
USER_PROXIMITY_MATCH_MSG_WO_PHONENUM_WO_BIO     = "_FIRSTNAME_ _LASTNAME_ is nearby.";

function buildMtgNotice(firstNameField, lastNameField, includeDevField, telNumField,
            userBioField, noticeDescField, sampleField) {

	var firstName = "";
	var lastName = "";
	var messagingNumber = "";
	var userBio = "";

	if (document.getElementById(firstNameField)) {
		firstName = document.getElementById(firstNameField).value;
		firstName = firstName.replace(/^\s+/, '');
		firstName = firstName.replace(/\s+$/, '');
	}
	if (document.getElementById(lastNameField)) {
		lastName = document.getElementById(lastNameField).value;
		lastName = lastName.replace(/^\s+/, '');
		lastName = lastName.replace(/\s+$/, '');
	}

	if (document.getElementById(includeDevField).checked && document.getElementById(telNumField)) {
		messagingNumber = document.getElementById(telNumField).value;
		messagingNumber = messagingNumber.replace(/^\s+/, '');
		messagingNumber = messagingNumber.replace(/\s+$/, '');
	}

	if (document.getElementById(userBioField)) {
		userBio = document.getElementById(userBioField).value;
		userBio = userBio.replace(/^\s+/, '');
		userBio = userBio.replace(/\s+$/, '');
	}
	
	var sampleSMS = buildMtgNoticeFromValues(firstName, lastName, messagingNumber, userBio);
	document.getElementById(sampleField).innerHTML = sampleSMS;

	// buildNoticeDesc(includeDevField, noticeDescField);
}

function buildMtgNoticeWithCC(firstNameField, lastNameField, includeDevField, countryCodeField,
			telNumField, userBioField, noticeDescField, sampleField) {

	var firstName = "";
	var lastName = "";
	var countryCode = "";
	var messagingNumber = "";
	var userBio = "";

	if (document.getElementById(firstNameField)) {
		firstName = document.getElementById(firstNameField).value;
		firstName = firstName.replace(/^\s+/, '');
		firstName = firstName.replace(/\s+$/, '');
	}
	if (document.getElementById(lastNameField)) {
		lastName = document.getElementById(lastNameField).value;
		lastName = lastName.replace(/^\s+/, '');
		lastName = lastName.replace(/\s+$/, '');
	}

	if (document.getElementById(includeDevField).checked && document.getElementById(countryCodeField)) {
		var elmt = document.getElementById(countryCodeField);
		countryCode = elmt.options[elmt.selectedIndex].value;
		countryCode = countryCode.replace(/^\s+/, '');
		countryCode = countryCode.replace(/\s+$/, '');
	}

	if (document.getElementById(includeDevField).checked && document.getElementById(telNumField)) {
		messagingNumber = document.getElementById(telNumField).value;
		messagingNumber = messagingNumber.replace(/^\s+/, '');
		messagingNumber = messagingNumber.replace(/\s+$/, '');
		if ((countryCode != "") && (messagingNumber != "")) {
			messagingNumber = "+" + countryCode + " " + messagingNumber;
		}
	}

	if (document.getElementById(userBioField)) {
		userBio = document.getElementById(userBioField).value;
		userBio = userBio.replace(/^\s+/, '');
		userBio = userBio.replace(/\s+$/, '');
	}
	
	var sampleSMS = buildMtgNoticeFromValues(firstName, lastName, messagingNumber, userBio);
	document.getElementById(sampleField).innerHTML = sampleSMS;

	// buildNoticeDesc(includeDevField, noticeDescField);
}

function buildMtgNoticeFromValues(firstName, lastName, messagingNumber, userBio) {

	var sampleSMS = "";
	if ((firstName.length > 0) || (lastName.length > 0)) {
		if ((userBio.length > 0) && (messagingNumber.length > 0)) {
			sampleSMS = USER_PROXIMITY_MATCH_MSG_WITH_PHONENUM_WITH_BIO;
		} else if ((userBio.length == 0) && (messagingNumber.length > 0)) {
			sampleSMS = USER_PROXIMITY_MATCH_MSG_WITH_PHONENUM_WO_BIO;
		} else if ((userBio.length > 0) && (messagingNumber.length == 0)) {
			sampleSMS = USER_PROXIMITY_MATCH_MSG_WO_PHONENUM_WITH_BIO;
		} else if ((userBio.length == 0) && (messagingNumber.length == 0)) {
			sampleSMS = USER_PROXIMITY_MATCH_MSG_WO_PHONENUM_WO_BIO;
		}
		sampleSMS = sampleSMS.replace(/_FIRSTNAME_/, firstName);
		sampleSMS = sampleSMS.replace(/_LASTNAME_/, lastName);
		sampleSMS = sampleSMS.replace(/_BIO_/, userBio);
		sampleSMS = sampleSMS.replace(/_TELNUM_/, messagingNumber);
		sampleSMS = sampleSMS.replace(/  /g," ");
		sampleSMS = sampleSMS.replace(/^ /g,"");
		sampleSMS = sampleSMS.replace(/ ,/g,",");
	}
	return sampleSMS;

}

function buildNoticeDesc(includeDevField, noticeDescField) {
	var mobile = (document.getElementById(includeDevField).checked) ? ", Mobile Number" : "";
	document.getElementById(noticeDescField).innerHTML =
	  "The notice people will receive if you match locations.<br>" +
	  "Contains your Name" + mobile + " and Title/Role.";
}

function cleanupText(str) {
    str = str.replace(/\+/g, " ");
    str = decodeURIComponent(str);
    return str;
}

function cleanupDisplayText(str) {
    str = cleanupText(str);
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    return str;
}

function formatUrl(field) {
	var url = document.getElementById(field).value;
	if (url.length == 0) return;
	if ((url.substr(0,7) == "http://") || (url.substr(0,8) == "https://")) return;
	document.getElementById(field).value = "http://" + url;
}

function updateFormError(field, message) {
    if(message == ""){
        document.getElementById(field+"Error").innerHTML = "&nbsp;";
        document.getElementById("pageErrors").innerHTML = "&nbsp;";
    }
    else{
    	if (document.getElementById("contextID")) {
        document.getElementById(field+"Error").innerHTML = 
            "<img src='" + document.getElementById("contextID").value + "/images2/arrows/errorArrow.gif' alt='Error Here' width='13' height='13'>";
        }
        else {
        	document.getElementById(field+"Error").innerHTML = 
            '<img src="../images2/arrows/errorArrow.gif" alt="Error Here" width="13" height="13">';
        }    
        document.getElementById("pageErrors").innerHTML = cleanupDisplayText(message);
        document.getElementById(field).focus();
    }
}

function notifyError(fieldId, message){
	if(updateFormError && updateFormError instanceof Function){
		updateFormError(fieldId, message);
	}
}

function toggleLayer( whichLayer ){
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display == 'none')
    vis.display = 'block';
  else
    vis.display = 'none';
}

function showErrors(failureReason){	
	if(failureReason == null){
		document.getElementById('errors').style.visibility = 'hidden';
	}else{
		document.getElementById('errors').style.visibility = 'visible';
		document.getElementById("errors").innerHTML = failureReason;	
	}	
}
