var mainAppDir = 'main';
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};

function openSamples(tab, cat){
	window.open('/'+mainAppDir+'/marketing1/Samples.jsp?tab='+tab+'&cat='+cat, 'Tutorials','toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=yes,width=1010,height=650,left=100,top=60');
}
function openTutorial(id){
	window.open('/'+mainAppDir+'/tutorial?id='+id, 'Tutorials','toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=yes,width=900,height=550,left=100,top=60');
}
function openURL(url){
	window.open(url, 'vFlyerSamples','toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=yes,width=900,height=1000');
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1){
					return data[i].identity;
				}
			}
			else if (dataProp){
				return data[i].identity;
			}
		}
		return "";
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1){ 
			return "";
		}else{
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		}
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};
BrowserDetect.init();

function processStats(baseURL) {
	var url = baseURL + encodeURIComponent(document.referrer);
	alert(url); 
	var ajax = new AJAXInteraction(url, nothingCallBack);
	ajax.doGet();
}

function nothingCallBack() {
	//DO NOTHING
}

function setFocus(id){
	var nodeId = id;
	try{
		if(document.getElementById(nodeId) !== null){
			document.getElementById(nodeId).focus();
		}
	}catch(e){
	
	}
}

function switchAcct(acctId){
	if (acctId != 'new') {
		var url = "/"+mainAppDir+"/action?action=SwitchAccount&acctId="+ encodeURIComponent(acctId); 
		var ajax = new AJAXInteraction(url, noActionCallBack); 
		ajax.doGet();
	}
	else {
		window.location.href='/'+mainAppDir+'/CreateAccount.jsp';
	}
}

function noActionCallBack(responseXML){
   if(responseXML.getElementsByTagName("switch")[0] !== null){
	   var msg = responseXML.getElementsByTagName("switch")[0].firstChild.nodeValue;
	   if (msg == "success") {
	   	   var url = window.location.pathname;
	       window.location.href = unescape(url);
	    } else {
	       //do nothing
	    }
    }
}

function loadStates(baseURL, country){
    var url = baseURL+'/data?get=states&country='+country;
    var ajax = new AJAXInteraction(url, processLoadStates); 
    ajax.doGet();
}

function processLoadStates(responseXML){
	var tags = responseXML.getElementsByTagName('state');
	var html = '';
	if(tags != null && tags.length > 0){
		html = '<select name="state">';
		for(var i = 0; i< tags.length; i++){
			var tag = tags[i];
			var name = tag.firstChild.nodeValue;
			var code = tag.getAttribute("code") 
			html = html + '<option value="'+code+'">'+name+'</option>';
		}
		html = html + '</select>';	
	}else{
		html = '<input type="text" class="inputMed" value="" name="state"/>';
	}
	
	var targetNode = document.getElementById('statesHolder');
	targetNode.innerHTML = html;
}

function openNewWindow(url, title){
	var temp1 = url;
	if(temp1.indexOf('Listing.jsp') != -1){
		temp1 = '/home/'+url;
	}
	var temp2 = title.replace(/[\s]+/g, "-");
	var brow = BrowserDetect.browser;
	if(brow == 'Explorer'){
		temp2 = 'AccountProfile';
	}
	window.open(temp1, temp2, 'toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=yes,resizable=yes,copyhistory=yes,width=1000,height=700,left=100,top=60');
}

function openWindowURLxWxH(url, w, h){
	var temp = url;
	window.open(url, 'Preview','toolbar=no,location=yes,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=yes,width='+w+',height='+h+',left=100,top=60');
}

function handleUnknownCountry(country){
	var targetNode = document.getElementById('countryOther');
	if(targetNode != null){
		if(country == ''){
			targetNode.style.display = "inline";
		}else{
			targetNode.style.display = "none";					
		}
	}
}

function spellCheck(baseURLPath, id) {
    // Build an array of form elements (not there values)
    var elements = new Array(0);
    // Your form elements that you want to have spell checked
    elements[elements.length] = document.getElementById(id);
    // Start the spell checker
    startSpellCheck( baseURLPath+'/spelling/jspspellcheck/', elements );
}

function showOption(val) {
	var text = val;
	if(text != null && text == 'Real Estate Broker'){
		document.getElementById("roleDetail").style.display = 'block';
	}else{
		document.getElementById("roleDetail").style.display = 'none';
	}
}

assocInfoArray = new Array (
		["AMTA","American Massage Therapy Association","<a href=\"http://www.amtamassage.org/\" target=\"_blank\">AMTAMASSAGE.org</a>"],
		
		["NADA","National Automobile Dealers Association","<a href=\"http://www.nada.org/\" target=\"_blank\">NADA.org</a>"],
		
		["R","National Associations of Realtors","<a href=\"http://www.realtor.com/\" target=\"_blank\">REALTOR.com</a>"],
		
		["HR","Society for Human Resource Management","<a href=\"http://www.shrm.org/\" target=\"_blank\">SHRM.org</a>"],
		
		["NIADA"," National Independent Automobile Dealers Association","<a href=\"http://www.niada.com/\" target=\"_blank\">NIADA.com</a>"],
		
		["PPD","Political Party - Democrat","<a href=\"http://www.democrats.org/\" target=\"_blank\">DEMOCRATS.org</a>"],
		
		["PPL","Political Party - Libertarian","<a href=\"http://www.lp.org/\" target=\"_blank\">LP.org</a>"],
		
		["PPR","Political Party - Republican","<a href=\"http://www.gop.com/\" target=\"_blank\">GOP.com</a>"],
		
		["EHL","Equal Housing Lender Association","<a href=\"http://www.hud.gov/\" target=\"_blank\">HUD.gov</a>"],
		
		["ESP","Energy Star","<a href=\"http://www.energystar.gov/\" target=\"_blank\">ENERGYSTAR.gov</a>"],
		
		["NARI","National Association of the Remodeling Industry","<a href=\"http://www.nari.org/\" target=\"_blank\">NARI.org</a>"],
		
		["ECOB","EcoBroker","<a href=\"http://www.ecobroker.com/\" target=\"_blank\">ECOBROKER.com</a>"],
		
		["BBB","Better Business Bureau","<a href=\"http://welcome.bbb.org/\" target=\"_blank\">BBB.org</a>"],
		
		["ASEC","Automotive Service Excellence","<a href=\"http://www.asecert.org/\" target=\"_blank\">ASECERT.org</a>"],
		
		["CRS","Council of Residential Specialists","<a href=\"http://www.crs.com/\" target=\"_blank\">CRS.com</a>"],
		
		["VRMA","Vacation Rental Managers Association","<a href=\"http://www.vrma.com/\" target=\"_blank\">VRMA.com</a>"],
		
		["NPPA","National Press Photographers Association","<a href=\"http://www.nppa.org/\" target=\"_blank\">NPPA.org</a>"],
		
		["IVAA","International Virtual Assistants Association","<a href=\"http://www.ivaa.org/\" target=\"_blank\">IVAA.org</a>"], 
		
		["CDPE","Certified Distressed Property Expert","<a href=\"http://cdpe.com/\" target=\"_blank\">CDPE.org</a>"],
		
		["EHO","Equal Housing Opportunity","<a href=\"http://hud.gov/\" target=\"_blank\">HUD.gov</a>"],
		
		["NAA","National Apartment Association","<a href=\"http://naahq.org/\" target=\"_blank\">NAAHQ.org</a>"],
		
		["NARPM","National Association of Residential Property Managers","<a href=\"http://narpm.org/\" target=\"_blank\">NARPM.org</a>"],
		
		["REBNY","Real Estate Board of New York","<a href=\"http://rebny.com/\" target=\"_blank\">REBNY.com</a>"]
);

function chgLogo(element){
	var id = element.id;
	var value = document.getElementById(id).value;
	var assocImageName = (id == 'logoSelect') ? "assocLogoImage" : "assocLogoImage2";
	
	if(value == "noLogo"){
		document.getElementById(assocImageName).src = "images/logoPreview.png";
	}else{
		for(var i=0; i<assocInfoArray.length; i++){
			if(value == assocInfoArray[i][0]){
				document.getElementById(assocImageName).src = "images/RealtorLogos/Preview/"+ value +".png";
			}
		}
	}
}

function searchOptions(){
	var showSearchObj = document.getElementById('showSearch');
	if(showSearchObj != null && showSearchObj.checked){
		if(document.getElementById('searchBarOptions') != null){
			document.getElementById('searchBarOptions').style.display='block';
		}
		if(document.getElementById('searchBarOptionsHdr') != null){
			document.getElementById('searchBarOptionsHdr').style.display='block';
		}
	}else{
		if(document.getElementById('searchBarOptions') != null){
			document.getElementById('searchBarOptions').style.display='none';
		}
		if(document.getElementById('searchBarOptionsHdr') != null){
			document.getElementById('searchBarOptionsHdr').style.display='none';
		}
	}
}

function onABCommComplete() {
	  var list = new String(document.getElementById('friendsEmailhidden').value);
	  var temp = new String("");
	  var array = new Array();
	  array = list.split(",");
	  var list = document.getElementById('friendsEmail').value;
	  for(i=0; i< array.length; i++){
	   	if(list == ""){
	   		list = array[i].substr(array[i].indexOf("<")+1,array[i].indexOf(">"));
	   	}else{
	   		list = list+","+array[i].substr(array[i].indexOf("<")+1,array[i].indexOf(">"));
	   	}
	  }
	  list = list.replace(/>/g,"");
	  document.getElementById('friendsEmail').value = list;
}

function addListeners(){
	//if (document.addEventListener) { 
	//	var obj = document.getElementById("listId");
	//	obj.addEventListener('change',changeAction,false);
	//}else if(document.attachEvent){
	//	var obj = document.getElementById("listId");
	//	obj.attachEvent('onchange',changeAction);
	//}else{
	//	alert("Your browser do not support. We recommend using Firefox or IE. If you are using one of these browsers please upgrade to the higer version.");
	//}
	//changeAction;
}

function updateGenerateHTMLButton(){
	var listId = document.getElementById("listId").value;
	var acctId = document.getElementById("acctId").value;
	var layoutId = document.getElementById("layoutId").value;
	var themeId = document.getElementById("themeId").value;
	var qryStr = 'acctId='+acctId+'&listId='+listId+'&layoutId='+layoutId+'&themeId='+themeId;
	document.getElementById('generateHTML').href = 'EmailListHTML.jsp?'+qryStr;
	document.getElementById('showPreview').innerHTML = '<input type="button" value="Preview" class="button-cancel" onclick="javascript:openNewWindow(\'EmailListPreview.jsp?acctId='+acctId+'&listId='+listId+'&layoutId='+layoutId+'&themeId='+themeId+'\',\'Preview\');"/>';
}

function changePreview(layout){
	var obj = document.getElementById("preview");
	if(obj != null){
		obj.src = "images/email/layout"+layout+".gif";
	}
	updateGenerateHTMLButton();
}

function changeAction(){
	var obj1 = document.getElementById("editList");
	var obj = document.getElementById("listId");
	if(obj != null){
		var value = obj.value;
		if(value == 0){
			obj1.innerHTML = '<a rel="iframe" rev="width:640 height:400 showClose:false" href="ListEditor.jsp?listId=new&src=email">Create New List</a>';
		}else{
			obj1.innerHTML = '<a rel="iframe" rev="width:640 height:400 showClose:false" href="ListEditor.jsp?listId='+obj.value+'&src=email">Edit List</a> | <a rel="iframe" rev="width:640 height:400 showClose:false" href="ListEditor.jsp?listId=new&src=email">Create New List</a>';
		}
	}else{
		obj1.innerHTML = '<a rel="iframe" rev="width:640 height:400 showClose:false" href="ListEditor.jsp?listId=new&src=email">Create New List</a>';
	}
	initfb('true');
	updateGenerateHTMLButton();	
}

function checkSubject() {
	var subjectObj = document.getElementById('subject');
	if(subjectObj != null){
		var subject = subjectObj.value.trim();
		if (subject == '') {
			alert('Subject is a required field'); 
			return false;
		}
	}
	return true;
}

function formatPhone(id){
	var obj = document.getElementById(id);
	var str = obj.value;
	if(str.length == 3 || str.length == 7){
		obj.value = str +"-";
	}
}

function confirmDelete(currAcctId, acctGrantId, msg){
	var flag = confirm(msg);
	if(flag){
		window.location.href = "/"+mainAppDir+"/action?action=DisableAccount&currAcctId="+currAcctId+"&acctGrantId="+acctGrantId;
	}
}

function confirmDeleteAcct(currAcctId, acctId, msg){
	var flag = confirm(msg);
	if(flag){
		window.location.href = "/"+mainAppDir+"/action?action=DisableAccount&currAcctId="+currAcctId+"&acctId="+acctId;
	}
}

function closeDivNode(id){
	if(id != null){
		var obj = document.getElementById(id);
		if(obj != null){
			obj.style.display = 'none';
		}
	}
}

function validateAcctName(ajaxPath, id){
	var target = document.getElementById(id); 
	var url = ajaxPath+ encodeURIComponent(target.value.toLowerCase()); 
	var ajax = new AJAXInteraction(url, validateAcctNameCallback); 
	ajax.doGet(); 
}

function validateAcctNameCallback(responseXML) {
   if(responseXML.getElementsByTagName("valid")[0] != null){
	   var msg = responseXML.getElementsByTagName("valid")[0].firstChild.nodeValue;
	   if (msg == "false") {
		   var mdiv = document.getElementById("message");
		   // set the style on the div to invalid
		   mdiv.className = "error";
		   mdiv.innerHTML = "<span class=\"red\">Account Name not available</span>";
		   var submitBtn = document.getElementById("register.x");
		   submitBtn.disabled = true;
		} else if(msg == "invalid") {
		   var mdiv = document.getElementById("message");
		   // set the style on the div to invalid
		   mdiv.className = "error";
		   mdiv.innerHTML = "<span class=\"red\">Use only letters and numbers</span>";
		   var submitBtn = document.getElementById("register.x");
		   submitBtn.disabled = true;
		} else if(msg == "required") {
		   var mdiv = document.getElementById("message");
		   // set the style on the div to invalid
		   mdiv.className = "error";
		   mdiv.innerHTML = "Required Field";
		   var submitBtn = document.getElementById("register.x");
		   submitBtn.disabled = true;
		}else {
			 var mdiv = document.getElementById("message");
		   // set the style on the div to valid
		   mdiv.className = "valid";
		   mdiv.innerHTML = "<span class=\"green\">Account Name available</span";
		   var submitBtn = document.getElementById("register.x");
		   submitBtn.disabled = false;
		}
	}
}

function selectZone(ajaxPath, country) {
	var targetNode = document.getElementById('timezone');
	country = country.toUpperCase();
	if(country == 'US'){
		targetNode.style.display="none";
	}else{
		targetNode.style.display="table-row";
		var url = ajaxPath+country;
		var ajax = new AJAXInteraction(url, processSelect); 
		ajax.doGet();
	}
}

function processSelect(responseXML) {
	var tags = responseXML.getElementsByTagName('zone');
	if(tags != null && tags.length > 0){
		var tag = tags[0];
		var name = tag.firstChild.nodeValue;			
		var targetNode = document.getElementById('zoneHolder');
		targetNode.value = name;	
	}
}

function checkConfirmPassword(idPassword, idConfirmPassword, idMsg){
	var pass = document.getElementById(idPassword).value;
	var cpass = document.getElementById(idConfirmPassword).value;
	var m1Div = document.getElementById(idMsg);
	if(pass != cpass){
		m1Div.className = "error";
		m1Div.innerHTML = "<span class=\"red\">&nbsp;Confirm Password doesn't match your password</span>";
	}else{
		m1Div.innerHTML = "";
	}
}

function acctTypeOptions(type, acctPaidBy, id){
	var t = type;
	if(t != null && t == acctPaidBy){
		document.getElementById(id).style.display = "block";
	}else{
		document.getElementById(id).style.display = "none";
	}
}

function copyClipBoard(id){
	var textArea = document.getElementById("textArea"+id);
	textArea.select();	
	var version = 0;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version>=5.5) {//NON IE browser will return
		Copied = textArea.createTextRange();
		Copied.execCommand("copy");
	}else{
		alert("Copying from JavaScript is disabled on your Mozilla due to security settings. For more information, read http://www.mozilla.org/editor/midasdemo/securityprefs.html \n Type ctrl + c to copy content.");
		Copied = textArea.createTextRange();
		Copied.execCommand("copy");
	}
}

function AJAXInteraction(url, callback) {
	var req = init();
	req.onreadystatechange = processRequest;
	function init() {
	  if (window.XMLHttpRequest) {
		  return new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		  return new ActiveXObject("Microsoft.XMLHTTP");
	  }else{
		  return new XMLHttpRequest();
	  }
	}    
	function processRequest () {
	  // readyState of 4 signifies request is complete
	  if (req.readyState == 4) {
	  // status of 200 signifies sucessful HTTP call
		if (req.status == 200) {
		  if (callback) {
			  //alert(req.responseXML);
			  callback(req.responseXML)
		  };
		}
	  }
	}
	this.doGet = function() {
	  // make a HTTP GET request to the URL asynchronously
	  try{
	  	req.open("GET", url, true);
	  	req.send(null);
	  }catch(e){
		//alert("Exception "+e);
	  }
	}
}

function getParameter ( parameterName ) {
	queryString = window.top.location.search.substring(1);
	// Add "=" to the parameter name (i.e. parameterName=value)
	parameterName = parameterName + "=";
	if ( queryString.length > 0 ) {
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) {
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
			// Return the string
			return unescape ( queryString.substring ( begin, end ) );
		}
		// Return "null" if no parameter has been found
		return "null";
	}
	return "null";
}

function validateMsgCallback(responseXML){
	var xml = responseXML;
	try{
		if(xml != null && xml.getElementsByTagName("tidbit")!= null && xml.getElementsByTagName("tidbit")[0] != null){
		   var msg = xml.getElementsByTagName("tidbit")[0].firstChild.nodeValue;
		   if (msg != null && msg != "") {
		   		var mdiv = document.getElementById("message");
		       	mdiv.innerHTML = msg;
		   }
	    }
    }catch(e){}
}

function getRandomMessage(url){
	ajax = new AJAXInteraction(url, validateMsgCallback); 
	ajax.doGet();
	wait(10000,url);
}

function getNextMessage(url){
	var code = 'true';
	var qryStr = '&getnext=' + encodeURIComponent(code);
	var nextURL = url + qryStr;
	var ajax = new AJAXInteraction(nextURL, validateMsgCallback); 
	ajax.doGet();
}

function wait(delay ,url){
	string = "getRandomMessage('"+url+"')";
	setTimeout(string,delay);
}

function updateIntStats(statsRefURL){
	var cCode = getParameter('cc');
	var qryStr = '';
	if(cCode != null){
	qryStr = '&cc='+encodeURIComponent(cCode);
	}
	var url = statsRefURL + encodeURIComponent(document.referrer) + qryStr;
	var ajax = new AJAXInteraction(url, validateMsgCallback); 
	ajax.doGet();
}

function createNewFlyer(prodId){
	if(prodId == ''){
		alert('Please select a type to create a flyer');
	}else{
		window.location.href='Editor.jsp?prodId='+prodId+'&state=new';
	}
}

function searchFlyers(keywordTypeStr){
	var keywordObj = document.getElementById("keyword");
	var keyword = '';
	if(keywordObj != null){
		keyword = keywordObj.value;
		var url = 'Flyers.jsp?keyword='+keyword;
		var keywordTypeObj = document.getElementById('keywordType');
		var keywordType = '';
		if(keywordTypeObj != null){
			keywordType = keywordTypeObj.value;
			}else{
			keywordType = keywordType;
		}
		url = url + '&keywordType='+keywordType;
		var sortByObj = document.getElementById('sortBy');
		var sortBy = '';
		if(sortByObj != null){
			sortBy = sortByObj.value;
			url = url + '&sortBy='+sortBy;
		}else{
			sortBy = '';
		}
		var filterByObj = document.getElementById('filterBy');
		var filterBy = '';
		if(filterByObj != null){
			filterBy = filterByObj.value;
			url = url + '&filterBy='+filterBy;
		}else{
			filterBy = '';
		}
		window.location = url;
	}else{
		alert('Please enter a keyword to search');
	}
}

function showInputBox(acctId, flyerId, label, title, titleLimitLength, href){
	var labelValue = '';
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		labelValue = label;
		label = label.replace(/'/g,"\\'");
	}
	if(title != null && title != ''){
		title = title.replace(/"/g,"&quot;");
		title = title.replace(/'/g,"\\'");
	}
	
	var html = '<input type="text" class="inputLong" onkeypress="return entsub(event)" onblur="changeLabel(this.value,\''+acctId+'\',\''+flyerId+'\',\''+title+'\','+titleLimitLength+', \''+href+'\')" value="'+labelValue+'" id="labelfield" />';
	document.getElementById("label"+flyerId).innerHTML = html;
	
	try{
		document.getElementById("labelfield").focus();
		document.getElementById("labelfield").select();
	}catch(e){
		alert(e);
	}
}

function entsub(event) {
	if (event && event.keyCode  == 13){
		return false;	
	}
	return true;
}

function entersubmit(event) {
	if (event && event.keyCode  == 13){
		return true;	
	}
	return false;
}

function changeLabel(value, acctId, flyerId, title, titleLimitLength, href){
	var label = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			label = value;
		}else{
			label = title;
		}
	}else{
		label = title;
	}
	var displayLabel = '';
	if(label != null && label.length > titleLimitLength){
		displayLabel = label.substring(0,titleLimitLength)+"...";
	}else{
		displayLabel = label;
	}
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}
	if(title != null && title != ''){
		title = title.replace(/"/g,"&quot;");
		title = title.replace(/'/g,"\\'");
		title = title.replace(/\\/g,"&#92;");
	}
	
	//document.getElementById("label"+flyerId).innerHTML = '<a href="'+href+'">'+displayLabel+'</a>&nbsp;<img id="edit'+flyerId+'" onclick="javascript:showInputBox(\''+acctId+'\',\''+flyerId+'\',\''+label+'\',\''+title+'\','+titleLimitLength+');" src="images/icons/pencil.gif" alt="Click to modify label" title="Click to modify label"/>';
	document.getElementById("label"+flyerId).innerHTML = displayLabel+'&nbsp;<img id="edit'+flyerId+'" onclick="javascript:showInputBox(\''+acctId+'\',\''+flyerId+'\',\''+label+'\',\''+title+'\','+titleLimitLength+');" src="images/icons/pencil.gif" alt="Click to modify label" title="Click to modify label"/>';
	var url = "/"+mainAppDir+"/action?action=FlyerLabel&acctId="+acctId+"&flyerId="+flyerId+"&label="+encodeURIComponent(value);
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteFlyer(flyerId, title, url){
	var flag = confirm("Do you really want to Delete the flyer '"+title+"'?");
	if(flag){
		window.location=url;
	}
}

function closeEmailHTML() {
	if(window.parent != null){
		window.parent.location = "EmailList.jsp";
	}else{
		window.location = "EmailList.jsp";
	}
}

function closeWidgetHTML(widgetInstId, type, id) {
	if(window.parent != null) {
		window.parent.location = "WidgetEditor.jsp?widgetInstId="+widgetInstId+"&type="+type+"&id="+id;
	}else{
		window.location = "WidgetEditor.jsp?widgetInstId="+widgetInstId+"&type="+type+"&id="+id;
	}
}

function confirmDeleteWidget(widgetInstId){
	var flag = confirm("Do you really want to Delete this widget?");
	if(flag){
		window.location.href = "/"+mainAppDir+"/action?action=Widgets&widgetAction=delete&widgetInstId="+widgetInstId;
	}
}

function copyWidgetToClipboard(id) {
	var textArea = document.getElementById(id);
	textArea.select();	
	var version = 0;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version>=5.5) {//NON IE browser will return
		Copied = textArea.createTextRange();
		Copied.execCommand("copy");
	}else{
		alert("Copying from JavaScript is disabled on your Mozilla due to security settings. For more information, read http://www.mozilla.org/editor/midasdemo/securityprefs.html \n Type ctrl + c to copy content.");
		Copied = textArea.createTextRange();
		Copied.execCommand("copy");
	}
}

function showInputBoxList(acctId, listId, name){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("list"+listId).innerHTML = '<input class="inputLong"  type="text" onkeypress="javascript:return entsubList(event);" onblur="javascript:changeListName(this.value,\''+acctId+'\',\''+listId+'\',\''+name+'\')" value="'+labelValue+'" id="listNamefield"/>';
	try{
		document.getElementById("listNamefield").focus();
		document.getElementById("listNamefield").select();
	}catch(e){
		alert(e);
	}
}

function entsubList(event) {
  if (event && event.keyCode  == 13){
	return false;	
  }
  return true;
}


function changeListName(value, acctId, vlistId, name){
	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}
	
	document.getElementById("list"+vlistId).innerHTML = displayLabel+'&nbsp;<a href="#" onclick="javascript:showInputBoxList(\''+acctId+'\',\''+vlistId+'\',\''+label+'\');return false;" title="Edit Label"><img src="images/icons/pencil.gif" alt="Click to modify label" title="Click to modify label"/></a>';
	var url = "/"+mainAppDir+"/action?action=Lists&currAcctId="+acctId+"&listAction=rename&listName="+encodeURIComponent(value)+"&listId="+vlistId;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteList(acctId, name, listId){
	var flag = confirm("Do you really want to Delete the list '"+name+"'?");
	if(flag){
		window.location.href = "/"+mainAppDir+"/action?action=Lists&currAcctId="+acctId+"&listAction=delete&listId="+listId;
	}
}

function noenter() {
	return !(window.event && window.event.keyCode == 13);
}

function checkDomainAvailability(){
	var domainName = document.getElementById('domainName').value;
	window.location.href = 'CheckAvailability.jsp?domainName='+domainName;
}

function doHelp(x, focus){
	if(x != null && focus){
		x.value = x.value.trim();
		if(x.value.length > 0 && x.value == 'Enter vFlyer Id here.'){
			x.value = '';
		}
	}

	if(!focus){
		if(x.value == ''){
			x.value = 'Enter vFlyer Id here.';
		}
	}
}

function findFlyer(){
	var obj = document.getElementById('fsURL'); 
	if(obj != null){
		obj.value = obj.value.trim();
		if(obj.value.length > 0 && obj.value == 'Enter vFlyer Id here.'){
			return false;
		}else{
			window.location.href = "/"+mainAppDir+"/action?action=FindFlyer&siteURL="+obj.value;
		}
	}
	return true;
}

function checkAvailability(){
	var domainName1 = document.getElementById('domainReqValue').value;
	window.location = 'CheckAvailability.jsp?domainName='+domainName1;
}
	
//function checkForDomainAvailability(reqDomainName){
function checkForDomainAvailability(reqDomainName, domainPrice){
	var domain = reqDomainName;
	if(domain != "" && domain != null){
	  	try{
	  		var url1 = "/"+mainAppDir+"/action?action=CheckAvailability&domainName="+encodeURIComponent(domain);
	  		//var ajax1 = new AJAXInteraction(url1, displayDomainsCallback);
	  		var ajax1 = new AJAXInteraction(url1, function(responseXML) {
	  			displayDomainsCallback(responseXML, domainPrice);
	  		}); 
		    ajax1.doGet();
		}catch(e){
			alert(e);
		} 
	}
}

function updateDomainName(domainName){
	document.getElementById("domainNameTld").value = domainName; 
}

function continuePurchase(){
	var domainName = document.getElementById("domainNameTld").value;
	window.location = 'DomainShopperInformation.jsp?domainName='+domainName;
}

//function displayDomainsCallback(responseXML){
function displayDomainsCallback(responseXML, domainPrice){
	try{
		if(responseXML.getElementsByTagName("check")[0] != null){
			var domains = responseXML.getElementsByTagName("domain");
			if(domains != null){
				var html = '<br /><span>The following matching domain names were found:</span>';
				var available = 0;
				for(var i=0; i<domains.length; i++){
					var domain = domains[i];
					var name = domain.getAttribute('name');
					var avail = domain.getAttribute('avail');
					if(avail == '1'){
						var selectedText = '';
						if(available == 0){
							selectedText = 'checked = "checked"';
							updateDomainName(name);
						}
						available = 1;
						
						html = html + '<br /><input type="radio" class="v2_radio" id="domain'+i+'" onclick="javascript:updateDomainName(\''+name+'\');" name="dNameTld" value="'+name+'" '+selectedText+' />&nbsp;&nbsp;<label for="domain'+i+'"><b>'+name+'</b>&nbsp;&nbsp;' + domainPrice + ' per Year</label>';
					}else{
						html = html + '<br /><b>'+name+'</b>&nbsp;Not Available';
					}
				}
				//html = html +'</table>';
				if(available == 1){
					html = html + '<br /><br /><input type="submit" value="Continue" class="button-save" onclick="javascript:continuePurchase();"/>&nbsp;<input type="button" value="Cancel" class="button-cancel" onclick="javascript:window.location.href=\'Domains.jsp?isCancel=true\';"/>';	  			
				}
				//html = html +'</div>';
				document.getElementById("displayMsg").innerHTML = html;
			}
		}else if(responseXML.getElementsByTagName("error")[0] != null){
			var msg = responseXML.getElementsByTagName("error")[0].firstChild.nodeValue;
			var errorMsg = "<span class=\"red\">"+msg+"</span>";
			document.getElementById("displayMsg").innerHTML = errorMsg;
		}else{
			var msg = "Unable to process your request. Please try again later. If you need more help please contact support.";
			var errorMsg = "<span class=\"red\">"+msg+"</span>";
			document.getElementById("displayMsg").innerHTML = errorMsg;
		}
	}catch(e){
		alert(e);
	}
}

function orderDomain(){
  	try{
  		var url = "/"+mainAppDir+"/action?action=OrderDomain";
  		var ajax = new AJAXInteraction(url, orderDomainCallback); //orderDomainCallback defined in ConfigureDomain.jsp
	    ajax.doGet();
	}catch(e){
		alert(e);
	} 
}
	
function cancelDomain(domainId){
	try{
		var url = "/"+mainAppDir+"/action?action=CancelDomain&domainId="+domainId;
		var ajax = new AJAXInteraction(url, noActionCallBack); 
	    ajax.doGet();
	}catch(e){
		alert(e);
	} 
}
	
function domainConfig(id, config){
		document.getElementById('flyer').style.display = "none";
	document.getElementById('site').style.display = "none";
	if(config == 'show'){
		document.getElementById(id).style.display = "inline";
	}
}
	
function disableDomain(domainName){
	var flag = confirm("Do you really want to delete your domain?");
	if(flag){
		window.location.href = '/'+mainAppDir+'/action?action=ConfigureDomain&domainName='+domainName+'&domainAction=delete';
	}
}
	
function showDomainInstructions(){
	if(document.getElementById("instructions").style.display == 'none'){
		document.getElementById("instructions").style.display = "block";
	}else{
		document.getElementById("instructions").style.display = "none";
	}
}

function showCVC(id, AMEXCardType){
	if(id == AMEXCardType){
		document.getElementById('cvc').innerHTML = '<img src="images/icons/cvc2.gif" style="cursor:help;" alt=" 4 digit, non-embossed number printed above your account number on the face of your card." title="4 digit, non-embossed number printed above your account number on the face of your card."/>';
	}else{
		document.getElementById('cvc').innerHTML = '<img src="images/icons/cvc.gif" style="cursor:help;" alt="Verification Number found on back of the card." title="Verification Number found on back of the card."/>';
	}
}

function showInputBoxTheme(acctId, themeId, name){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("theme"+themeId).innerHTML = '<input class="inputLong" type="text" onkeypress=\"javascript:return entsub(event);\" onblur="javascript:changeThemeName(this.value,\''+acctId+'\',\''+themeId+'\',\''+name+'\');" value="'+labelValue+'" id="themeNamefield"/>';
	try{
		document.getElementById("themeNamefield").focus();
		document.getElementById("themeNamefield").select();
	}catch(e){
		alert(e);
	}
}

function changeThemeName(value, acctId, themeId, name){
	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}
	
	document.getElementById("theme"+themeId).innerHTML = displayLabel+'&nbsp;<img id="edit'+themeId+'" onclick="javascript:showInputBoxTheme(\''+acctId+'\',\''+themeId+'\',\''+label+'\');" src="images/icons/pencil.gif" alt="Click to modify label" title="Click to modify label"/>';
	var url = "/"+mainAppDir+"/action?action=Themes&currAcctId="+acctId+"&themeAction=rename&themeName="+encodeURIComponent(value)+"&themeId="+themeId;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteTheme(acctId, themeId, name){
	var flag = confirm("Do you really want to Delete the theme '"+name+"'");
	if(flag){
		window.location.href = "/"+mainAppDir+"/action?action=Themes&currAcctId="+acctId+"&themeAction=delete&themeId="+themeId;
	}
}

function showInputBoxHeader(acctId, imageId, name){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("image"+imageId).innerHTML = '<input type="text" onkeypress=\"javascript:return entsub(event);\" onblur="javascript:changeHeaderName(this.value,\''+acctId+'\',\''+imageId+'\',\''+name+'\');" value="'+labelValue+'" id="imageNamefield" class="inputMed"/>';
	try{
		document.getElementById("imageNamefield").focus();
		document.getElementById("imageNamefield").select();
	}catch(e){
		alert(e);
	}
}

function changeHeaderName(value, acctId, imageId, name){
	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}

	document.getElementById("image"+imageId).innerHTML = displayLabel+'&nbsp;<img id="edit'+imageId+'" onclick="javascript:showInputBoxHeader(\''+acctId+'\',\''+imageId+'\',\''+label+'\');" src="images/icons/pencil.gif" alt="Click to rename" title="Click to rename"/>';
	var url = "/"+mainAppDir+"/action?action=ThemeHeaders&currAcctId="+acctId+"&imageAction=rename&imageName="+encodeURIComponent(value)+"&imageId="+imageId;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteHeader(acctId, imageId, name){
	var flag = confirm("Do you really want to Delete the header '"+name+"'");
	if(flag){
		window.location="action?action=ThemeHeaders&currAcctId="+acctId+"&imageAction=delete&imageId="+imageId;
	}
}

function showInputBoxBackground(acctId, imageId, name){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("image"+imageId).innerHTML = '<input type="text" class="inputMed" onkeypress=\"javascript:return entsub(event);\" onblur="javascript:changeBackgroundName(this.value,\''+acctId+'\',\''+imageId+'\',\''+name+'\');" value="'+labelValue+'" id="imageNamefield" />';
	try{
		document.getElementById("imageNamefield").focus();
		document.getElementById("imageNamefield").select();
	}catch(e){
		alert(e);
	}
}

function changeBackgroundName(value, acctId, imageId, name){
	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}
	
	document.getElementById("image"+imageId).innerHTML = displayLabel+'<img id="edit'+imageId+'" onclick="javascript:showInputBoxBackground(\''+acctId+'\',\''+imageId+'\',\''+label+'\');" src="images/icons/pencil.gif" alt="Click to rename" title="Click to rename"/>';
	var url = "action?action=ThemeBackgrounds&currAcctId="+acctId+"&imageAction=rename&imageName="+encodeURIComponent(value)+"&imageId="+imageId;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteBackground(acctId, imageId, name){
	var flag = confirm("Do you really want to Delete the background '"+name+"'");
	if(flag){
		window.location="action?action=ThemeBackgrounds&currAcctId="+acctId+"&imageAction=delete&imageId="+imageId;
	}
}

function logBugLog(bugLogURL){
	var qryStr = "&refURL="+encodeURIComponent(document.referrer);
	qryStr = qryStr + "&reqURL="+encodeURIComponent(window.location);
	var url = bugLogURL + qryStr;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function showInputBoxImages(acctId, acctImgId, name, titleLimitLength){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("img"+acctImgId).innerHTML = '<input type="text" class="inputMed" onkeypress=\"return entsub(event)\" onblur="changeImagesName(this.value,\''+acctId+'\',\''+acctImgId+'\',\''+name+'\',\''+titleLimitLength+'\')" value="'+labelValue+'" id="imageNamefield" />';
	try{
		document.getElementById("imageNamefield").focus();
		document.getElementById("imageNamefield").select();
	}catch(e){
		alert(e);
	}
}

function changeImagesName(value, acctId, acctImgId, name, titleLimitLength){
	var titleLimitLength = 23;

	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if (displayLabel.length > titleLimitLength) {
		displayLabel = displayLabel.substring(0, titleLimitLength)+"...";
	}
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}

	document.getElementById("img"+acctImgId).innerHTML = displayLabel+'&nbsp;<img id="edit'+acctImgId+'" onclick="javascript:showInputBoxImages(\''+acctId+'\',\''+acctImgId+'\',\''+label+'\',\''+titleLimitLength+'\');" src="images/icons/pencil.gif" alt="Click to rename" title="Click to rename"/>';
	var url = "action?action=Images&currAcctId="+acctId+"&imageAction=rename&imageName="+encodeURIComponent(value)+"&acctImageId="+acctImgId;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function confirmDeleteImage(acctId, acctImgId, name){
	var flag = confirm("Do you really want to Delete the image '"+name+"'?");
	if(flag){
		window.location="action?action=Images&currAcctId="+acctId+"&imageAction=delete&acctImageId="+acctImgId;
	}
}

function changeBackgroundColor(id, color){
	document.getElementById(id).style.backgroundColor = color;
}

function showTextBox(id, action, prodInstId){
	if(document.getElementById("captionText"+id) != null){
		document.getElementById("captionText"+id).display = 'none';
	}
	var caption = document.getElementById("C"+id).value;
	if(caption == "Add Caption"){
		caption = "";
	}
	else {
		caption = caption.replace(/"/g,"&quot;");
	}
	document.getElementById(id).style.display = 'none';
	document.getElementById(id+"Caption").style.display = 'inline';
	document.getElementById(id+"Caption").innerHTML = "<input class=\"reg-input\" value=\""+caption+"\" type=\"text\" id=\"captionText"+id+"\" onkeypress=\"return entsub(event)\" style=\"line-height:16px; text-align:center; font-size:12px; width:100px;\" onblur=\"showCaption("+id+", '"+action+"', "+prodInstId+")\"/>";
	try{
		document.getElementById("captionText"+id).focus();
		document.getElementById("captionText"+id).select();
	}catch(e){
		alert(e);
	}
}
function showCaption(id, action, prodInstId){
	var caption = document.getElementById("captionText"+id).value;
	document.getElementById("C"+id).value = caption;
	document.getElementById(id+"Caption").style.display = 'none';
	document.getElementById(id).style.display = 'inline';
	if(caption != null && caption != "") {
		
		caption = caption.replace(/^\s+|\s+$/g,""); //TRIM
		//caption = caption.replace(/\\/g,""); //REMOVE BACK SLASHES
		//caption = caption.replace(/</g,"&lt;");
		//caption = caption.replace(/>/g,"&gt;");
		
		var dCaption = "";
		if(caption.length > 17){
			dCaption = caption.substring(0,15)+"...";
		}else{
			dCaption = caption;
		}
		
		if(dCaption != null && dCaption != ''){
			dCaption = dCaption.replace(/"/g,"&quot;");
			dCaption = dCaption.replace(/\\/g,"&#92;");
			dCaption = dCaption.replace(/</g,"&lt;");
			dCaption = dCaption.replace(/>/g,"&gt;");
		}
		
		caption = caption.replace(/\\/g,"&#92;");
		caption = caption.replace(/</g,"&lt;");
		caption = caption.replace(/>/g,"&gt;");
		
		document.getElementById(id).innerHTML = dCaption+" <img style=\"position:relative; top:3px;\" src=\"images/icons/pencil.gif\" />";
	}else{
		document.getElementById(id).innerHTML = "Add Caption <img style=\"position:relative; top:3px;\" src=\"images/icons/pencil.gif\" />";
	}
	
	var url = "action?action=" + action;
	if (prodInstId != null) {
		url += "&prodInstId=" + prodInstId
	}
	url += "&source=CAPTION&imageid=" + id + "&caption=" + encodeURIComponent(caption);
	
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();

}


function showInputBoxWidget(acctId, id, name, titleLimitLength){
	var labelValue = '';
	if(name != null && name != ''){
		name = name.replace(/"/g,"&quot;");
		labelValue = name;
		name = name.replace(/'/g,"\\'");
	}
	document.getElementById("widget"+id).innerHTML = '<input type="text" class="inputMed" onkeypress=\"return entsub(event)\" onblur="changeWidgetName(this.value,\''+acctId+'\',\''+id+'\',\''+name+'\',\''+titleLimitLength+'\')" value="'+labelValue+'" id="widgetNamefield" />';
	try{
		document.getElementById("widgetNamefield").focus();
		document.getElementById("widgetNamefield").select();
	}catch(e){
		alert(e);
	}
}

function changeWidgetName(value, acctId, id, name, titleLimitLength){
	var titleLimitLength = 23;

	var displayLabel = '';
	if(value != null && value != ''){
		value = value.replace(/^\s+|\s+$/g,""); //TRIM
		value = value.replace(/\\/g,""); //REMOVE BACK SLASHES
		if(value != ''){
			displayLabel = value;
		}else{
			displayLabel = name;
		}
	}else{
		displayLabel = name;
	}
	
	var label = displayLabel;
	
	if (displayLabel.length > titleLimitLength) {
		displayLabel = displayLabel.substring(0, titleLimitLength)+"...";
	}
	
	if(displayLabel != null && displayLabel != ''){
		displayLabel = displayLabel.replace(/"/g,"&quot;");
		displayLabel = displayLabel.replace(/\\/g,"&#92;");
		displayLabel = displayLabel.replace(/</g,"&lt;");
		displayLabel = displayLabel.replace(/>/g,"&gt;");
	}		
	if(label != null && label != ''){
		label = label.replace(/"/g,"&quot;");
		label = label.replace(/'/g,"\\'");
		label = label.replace(/\\/g,"&#92;");
	}

	document.getElementById("widget"+id).innerHTML = displayLabel+'&nbsp;<img id="edit'+id+'" onclick="javascript:showInputBoxWidget(\''+acctId+'\',\''+id+'\',\''+label+'\',\''+titleLimitLength+'\');" src="images/icons/pencil.gif" alt="Click to rename" title="Click to rename"/>';
	var url = "action?action=Widgets&currAcctId="+acctId+"&widgetAction=rename&widgetName="+encodeURIComponent(value)+"&widgetInstId="+id;
	var ajax = new AJAXInteraction(url, noActionCallBack); 
	ajax.doGet();
}

function saveFlyer(title, url){
	 if (window.sidebar) {
	  window.sidebar.addPanel(title, url,"");
	 } else if( window.external ) {
	  window.external.AddFavorite( url, title); 
	 } else if(window.opera && window.print) {
	   return true; 
	 }
}
