function dateformat (form){
	var ele1 = form.start;
	if (ele1 == null) ele1 = form.date;
 	var ele2 = form.end;
 	if (ele2 == null) ele2 = form.date1;
 	
	var da = ele1.value;
	var da1 = ele2.value;
	var date = da.split('-');
	var date1 = da1.split('-');
	
	if (da != null && da.length != 0){
		if (date[0].length != 2 || date[1].length != 2 || date[2].length != 4){
			alert ('Please enter date in MM-DD-YYYY format');
			return false;
		}
	}
	if (da1 != null && da1.length != 0){
		if (date1[0].length != 2 || date1[1].length != 2 || date1[2].length != 4){
			alert ('Please enter date in MM-DD-YYYY format');
			return false;
		}
	}
}

function conditionalDateFormat (form, elem){
	var formObj = document.getElementById(elem);
   if (formObj != null && formObj.style.display == "block") {
		
		var da = form.start.value;
		var da1 = form.end.value;
		var date = da.split('-');
		var date1 = da1.split('-');
		
		if (date.length != 3) {
			alert ('Start date in correct format (MM-DD-YYYY) is required');
			return false;
		}
	
		if (da != null && da.length != 0){
			if (date[0].length != 2 || date[1].length != 2 || date[2].length != 4){
				alert ('Please enter the start date in MM-DD-YYYY format');
				return false;
			}
		}
		if (da1 != null && da1.length != 0){
			if (date1.length != 3) {
				alert ('Please enter the end date in MM-DD-YYYY format');
				return false;
			}
			if (date1[0].length != 2 || date1[1].length != 2 || date1[2].length != 4){
				alert ('Please enter the end date in MM-DD-YYYY format');
				return false;
			}
		}
	} else return true;
}

function dateformat1 (form){
	var da = form.date.value;
	var date = form.date.value.split('-');
	if (da != null && da.length != 0){
		if (date[0].length != 2 || date[1].length != 2 || date[2].length != 4){
			alert ('Please enter date in MM-DD-YYYY format');
			return false;
		}
	}
}

function setPage (page){
	document.getElementById("pageName").value = page;
	alert(document.getElementById("pageName").value);
}
		
function textCounter(field, cntfield, maxlimit) {
	// if too long...trim it!
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
		cntfield.value = maxlimit - field.value.length;
}

function checkNull(elements, names) {

	var elementarr = new Array();
	var namesarr = new Array();
    elementarr = elements.split(' ');
	namesarr = names.split(' ');
	
	for (var i = 0; i < elementarr.length; i++) {

		var elementvalue = document.getElementById(elementarr[i]);
		var namevalue = namesarr[i];

		if (elementvalue.value == null || elementvalue.value == "") {
			var displayalert = "Value of '" + namevalue + "' is missing. Enter value";
			alert(displayalert);
			return false;
		}
	}
	return true;
}
// changeTitle 'First Parameter' -- Main Title for the page. 
// changeTitle 'Second Parameter' -- Add (seperated by ^ESCAPE^) as many title entries as it takes to get to the page where
//					 method is called (excluding "Home" since home page is added by default). e.g. for 'Home > Page one > Page two'
//					'Second Parameter' would be 'Page one^ESCAPE^Page two' 
// changeTitle 'Third Parameter' -- Sequentially Add (seperated by ^ESCAPE^) URL's to each title added in the 'Second Parameter'
//					(excluding "Home" since home page is added by default). e.g. for 'Home > Page one > Page two'
//					'Third Parameter' would be 'http://www.pageone.com' ... Notice that, since page two is calling the method and is
//					 landing page, no URL is added (it would be ignored, even if added)

function changeTitle(maintitle, titles, urls) {
	
	var titleelement = document.getElementById('backofficetitle');
	var mapelement = document.getElementById('backofficemapping');
	
	titleelement.innerHTML = maintitle;
	
	var html = "<font size='2em'><a href='/do/backoffice'> Home </a> > ";
	var titlesarr = new Array();
	var urlsarr = new Array();
	titlesarr = titles.split('^ESCAPE^');
	urlsarr = urls.split('^ESCAPE^');
	
	for (var i = 0; i < titlesarr.length - 1; i++) {
		var title = titlesarr[i];
		var link = urlsarr[i];
		
		html += "<a href='" + link + "'>" + title + "</a> > ";
	}
	if (titlesarr.length > 0) html += titlesarr[titlesarr.length - 1];
	mapelement.innerHTML = "</font>" + html;
}


function displayTest(){
	// URL division
	var urlarr = new Array();
	urlarr = location.href.split('/do/');
	var urlprefix = urlarr[0];
	var urlpath = "/do/" + urlarr[1];
	
	var maintitle = "";
	var titles = "";
	var urls = "";
	
	if (urlpath == "/do/backoffice/customer/manage") {
  	maintitle = 'Customer Management'; 
  	titles = 'Customer Management^ESCAPE^Lookup By Number';
  	urls = "/do/backoffice/customer/manage";
  }
  if (maintitle != "" && titles != "") changeTitle(maintitle, titles, urls);
}
	
//character counter
function countit(what){
	formcontent = what.form.message.value;
	var counter = formcontent.length;
	var tempArray = formcontent.split("");
	
	for (x in tempArray) {
		if (tempArray[x] == "`" || tempArray[x] == ";" || tempArray[x] == ":" || tempArray[x] == "@" || tempArray[x] == "&" || tempArray[x] == "=" || tempArray[x] == "+" || tempArray[x] == "$" || tempArray[x] == "," || tempArray[x] == "/" || tempArray[x] == "?" || tempArray[x] == "%" || tempArray[x] == "#" || tempArray[x] == "[" || tempArray[x] == "]") { counter += 2; }
	}	
	
	what.form.displaycount.value = counter;
	
	if (what.form.displaycount.value > 160) {
		what.form.displaycount.style.backgroundColor="#FFCCCC";
	} else {
		what.form.displaycount.style.backgroundColor="#CCFFCC";
	}
}

function checkRelevantForm(thisForm, relevantFormName){
	var relevantForm = document.getElementById(relevantFormName);
	if (relevantForm.value != null && relevantForm.value != "") thisForm.submit();
}

function toggle(hideDiv,showDiv) {
	var show = document.getElementById(showDiv);
	var hide = document.getElementById(hideDiv);
	
	if(show.style.display == "block") {
		show.style.display = "none";
		hide.style.display = "block";
  	}
	else {
		show.style.display = "block";
		hide.style.display = "none";
	}
}


function disable(elm,bool) {
	var element = document.getElementById(elm);
	element.disabled = bool;
}


function autoFill(txtarea, myValue) {
	  var myField = document.getElementById(txtarea);
	  //var stp = document.getElementById(field);
	  
       //IE support
       if (document.selection) {
         myField.focus();
         sel = document.selection.createRange();
         sel.text = myValue;
       }
       //MOZILLA/NETSCAPE support
       else if (parseInt(myField.selectionStart) != parseInt(myField.selectionEnd) || myField.selectionStart == '0') {
         var startPos = myField.selectionStart;
         var endPos = myField.selectionEnd;
         myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
       } else {
           myField.value += myValue;
       }
     
     
   }

function toggleMap(hideDiv,showDiv,mapDiv) {
	var show = document.getElementById(showDiv);
	var hide = document.getElementById(hideDiv);
	var map = document.getElementById(mapDiv);
	
	if(map.style.display == "block") {
		show.style.display = "none";
		hide.style.display = "block";
		map.style.display = "none";
  	} else {
		show.style.display = "block";
		hide.style.display = "none";
		map.style.display = "block";
	}
}

//Clear Input Field
function clearField(element) {
	var field = document.getElementById(element);
	field.value = "";
}


// Copy to clipboard
function copy_clip(meintext)
{

 if (window.clipboardData) 
   {
   
   // the IE-way
   window.clipboardData.setData("Text", meintext);
   
   // Probabely not the best way to detect netscape/mozilla.
   // I am unsure from what version this is supported
   }
   else if (window.netscape) 
   { 
   
   // This is importent but it's not noted anywhere
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   // create interface to the clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // create a transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specify the data we wish to handle. Plaintext in this case.
   trans.addDataFlavor('text/unicode');
   
   // To get the data from the transferable we need two new objects
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   alert("Following info was copied to your clipboard:\n\n" + meintext);
   return false;
}

//select all text
function fnSelect(objId) {
	fnDeSelect();
	if (document.selection) {
	var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(objId));
	range.select();
	}
	else if (window.getSelection) {
	var range = document.createRange();
	range.selectNode(document.getElementById(objId));
	window.getSelection().addRange(range);
	}
}
	
function fnDeSelect() {
	if (document.selection) document.selection.empty(); 
	else if (window.getSelection)
            window.getSelection().removeAllRanges();
}

function grabAndCopy(hide,show){
	
	toggle(hide,show);
	fnSelect(show);
	
}

	// User Management Functions
function checkWebRole(){
	
	var roles = document.userForm.selectedRoles;
	var uncheck = false;
	var showIps = document.getElementById('ipaddress');
	
	for(var i = 0; i < roles.length;i++){
		if(roles[i].value == 'web'){
			if(roles[i].checked){
				uncheck = true;
			}
		}
	}
	
	if(uncheck){
		for(var o = 0; o < roles.length;o++){
			if(roles[o].value != 'web'){
				roles[o].checked = false;
			}
		}
		showIps.style.display = "block";
	}
}

function checkRole(){
	
	var roles = document.userForm.selectedRoles;
	var uncheck = false;
	var showIps = document.getElementById('ipaddress');
	for(var i = 0; i < roles.length;i++){
		if(roles[i].value != 'web'){
			if(roles[i].checked){
				uncheck = true;
			}
		}
	}
	
	if(uncheck){
		for(var o = 0; o < roles.length;o++){
			if(roles[o].value == 'web'){
				roles[o].checked = false;
				showIps.style.display = "none";
			}
		}			
	}
}
	
//User Ip Address Management
function deleteIpAddress(count){
		
	var ipaddress = document.userForm.ips.value;
	var show = document.getElementById("ipaddress");
	var ipArray = ipaddress.split(",");
	var newIps = "";
	var results = "<table><tr><td colspan='2'><table width='200'><tr bgcolor='#E0E0E0'><td>Ip Address Access</td><td>&nbsp;</td></tr>"; 
	ipArray.splice(count,1);	
	
	if(ipArray.length > 0){
		for(var i=0;i<ipArray.length;i++){
			newIps += ipArray[i] + ",";
			results += "<tr><td>" + ipArray[i] + "</td><td><input type='button' name='delete' value='Delete' onclick='deleteIpAddress(" + i + ");'></td></tr>";
		}
	}
	newIps = newIps.substring(0,newIps.length-1);
	results += '<tr bgcolor="#E0E0E0"><td>Add Ip Address: </td><td><input type="button" name="add" value="Add" onclick="TINY.box.show(\'/backoffice/ip.jsp\',1,0,0,1);" /></td></tr></table>';
	show.innerHTML = results;
	document.userForm.ips.value = newIps;
}

function addIpAddress(){
	
	var ipaddress 	= document.userForm.ips.value;
	var show 		= document.getElementById("ipaddress");
	var newIp 		= document.ipForm.ip.value;
	var ipArray = ipaddress.split(",");
	var newIps = "";
	var count = 0;
	var results = "<table><tr><td colspan='2'><table width='200'><tr bgcolor='#E0E0E0'><td>Ip Address Access</td><td>&nbsp;</td></tr>"; 
	
	if(ipArray.length > 0){
		for(var i=0;i<ipArray.length;i++){
			if(ipArray[i] != null && ipArray[i].length > 0){
				newIps += ipArray[i] + ",";
				results += "<tr><td>" + ipArray[i] + "</td><td><input type='button' name='delete' value='Delete' onclick='deleteIpAddress(" + i + ");'></td></tr>";
				count++;
			}				
		}
	}
	
	newIps+= newIp; 
	results += "<tr><td>" + newIp + "</td><td><input type='button' name='delete' value='Delete' onclick='deleteIpAddress(" + count + ");'></td></tr>";
	
	results += '<tr bgcolor="#E0E0E0"><td>Add Ip Address: </td><td><input type="button" name="add" value="Add" onclick="TINY.box.show(\'/backoffice/ip.jsp\',1,0,0,1);" /></td></tr></table>';
	show.innerHTML = results;
	document.userForm.ips.value = newIps;
	TINY.box.hide();
}
	
//Subscriptions Overview
function allTimeHistory(url,affiliateFilter,extRefFilter,levelFilter,carrierFilter,referrerFilter,timeFilter,monthFilter,yearFilter,startTimeFilter,stopTimeFilter){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	
	if(affiliateFilter == ''){
		affiliateFilter = 'nothing';
	}
	
	if(extRefFilter == ''){
		extRefFilter = 'nothing';
	}
	
	if(levelFilter == ''){
		levelFilter = 'nothing';
	}
	
	if(carrierFilter == ''){
		carrierFilter = 'nothing';
	}
	
	if(referrerFilter == ''){
		referrerFilter = 'nothing';
	}
	
	if(monthFilter == ''){
		monthFilter = '00';
	}
	
	if(yearFilter == ''){
		yearFilter = '0000';
	}
	
	if(timeFilter == ''){
		timeFilter = 'my';
	}
	
	if(startTimeFilter == ''){
		startTimeFilter = 'nothing';
	}
	
	if(stopTimeFilter == ''){
		stopTimeFilter = 'nothing';
	}
	
	//alert("AF: " + affiliateFilter + " ERF: "+extRefFilter+" LF: "+levelFilter+" CF: "+carrierFilter+" RF: " +referrerFilter+" MF: "+monthFilter+" YF: "+yearFilter+" TF: "+timeFilter+" STF: "+startTimeFilter+" SPF: "+stopTimeFilter);
	url=url+"?af="+affiliateFilter+"&erf="+extRefFilter+"&lf="+levelFilter+"&cf="+carrierFilter+"&r="+referrerFilter+"&mf="+monthFilter+"&yf="+yearFilter+"&tf="+timeFilter+"&stf="+startTimeFilter+"&spf="+stopTimeFilter;
	xmlHttp.onreadystatechange=process;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function process() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var show = document.getElementById("allTime");
		var showGraph = document.getElementById("allTimePieGraph");
		show.innerHTML = xmlHttp.responseText;
		show.style.display = "block";		
		showGraph.style.display = "block";
	}
}

function checkPriceforRecurring(price){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 	
	var type = "new";
	
	if(price == null)
		price = 0;
	else
		type = "old";
	
	url="/do/backoffice/content/program/price/recurring/check?price="+price+"&type="+type;
	xmlHttp.onreadystatechange=returnRecurringAvailability;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
	
}

function returnRecurringAvailability() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var recurringField = document.getElementById("recurring");
		var confirmField = document.getElementById("confirm");
		var notifyField = document.getElementById("notify");
		var reminderField = document.getElementById("reminder");
		var result = xmlHttp.responseText;
		var splitter = result.split("|");
		
		var price = splitter[0];
		var type = splitter[1];
		
		//if(type == "old"){
			if(price > 0){
				
				//Paying programs
				recurringField.disabled = false;
				confirmField.disabled = true;
				confirmField.checked = true;
				notifyField.disabled = true;
				notifyField.checked = true;
				
				
				if(reminder.checked)
					recurring.checked = true;
				
				reminderField.disabled = true;
				reminderField.checked = false;
				
			}else{
				//Disable b/c there are no recurring charges for free programs
				if(recurring.checked)
					reminderField.checked = true;
				
				recurringField.disabled = true;
				recurringField.checked = false;
				
				confirmField.disabled = false;
				notifyField.disabled = false;
				reminderField.disabled = false;	
			}
		//}
		
		repaintMessageFlow();
	}
}

function repaintMessageFlow(){
	
	//Value varibles
	var confirmValue,notifyValue,dupeNotifyValue,reminderValue,recurringValue = "";
	var affiliateValue,companyValue,shortcodeValue,keywordValue,sourceValue,languageValue,confirmType = "";
	var typeValue,subtypeValue,typeStringValue,frequencyValue,prodDescValue,attProdDescValue,vznProdDescValue = "";
	var priceValue,contentTypeValue,supportEmailValue,supportPhoneValue,valueFloatValue = "";
	
	//Get the form variable values
	var confirm 		= document.getElementById("confirm");
	var notify			= document.getElementById("notify");
	var dupeNotify		= document.getElementById("dupeNotify");
	var reminder 		= document.getElementById("reminder");
	var recurring		= document.getElementById("recurring");
	var valueFloat		= document.getElementById("valueFloat");
	var affiliate		= document.getElementById("affiliate");
	var companyOwned	= document.getElementById("company");
	var shortcode		= document.getElementById("shortcode");
	var keyword			= document.getElementById("keyword");
	var source			= document.getElementById("sourceString");
	var language		= document.getElementById("language");
	var confirmType		= document.getElementById("confirmType");
	var type			= document.getElementById("type");
	var subtype			= document.getElementById("subtype");
	var typeString 		= document.getElementById("typeString");
	var frequency 		= document.getElementById("frequency");
	var prodDesc		= document.getElementById("prodDesc");
	var attProdDesc 	= document.getElementById("attProdDesc");
	var vznProdDesc		= document.getElementById("vznProdDesc");
	var contentType		= document.getElementById("contentType");
	var supportEmail	= document.getElementById("supportEmail");
	var supportPhone	= document.getElementById("supportPhone");
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
			
	if(affiliate != null)
		affiliateValue = affiliate.value;
	else
		affiliateValue = "";
	
	if(companyOwned != null)
		companyOwnedValue = companyOwned.value;
	else
		companyOwnedValue = "";
	
	if(shortcode != null)
		shortcodeValue = shortcode.value;
	else 
		shortcodeValue = "";
	
	if(keyword != "")
		keywordValue = keyword.value;
	else 
		keywordValue = "";
	
	if(source != null)
		sourceValue = source.value;
	else 
		sourceValue = "";
	
	if(language != null)
		languageValue = language.value;
	else
		languageValue = "";
	
	if(confirmType != null)
		confirmTypeValue = confirmType.value;
	else 
		confirmTypeValue = "";
	
	if(type != null)
		typeValue = type.value;
	else
		typeValue = "";
	
	if(subtype != null)
		subtypeValue = subtype.value;
	else
		subtypeValue = "";
	
	if(typeString != null)
		typeStringValue = typeString.value;
	else
		typeStringValue = "";
	
	if(frequency != null)
		frequencyValue = frequency.value;
	else
		frequencyValue = "";
	
	if(prodDesc != null)
		prodDescValue = prodDesc.value;
	else
		prodDescValue = "";
	
	if(attProdDesc != null)
		attProdDescValue = attProdDesc.value;
	else
		attProdDescValue = "";
	
	if(vznProdDesc != null)
		vznProdDescValue = vznProdDesc.value;
	else
		vznProdDescValue = "";
			
	if(contentType != null)
		contentTypeValue = contentType.value;
	else
		contentTypeValue = "";
	
	if(supportEmail != null)
		supportEmailValue = supportEmail.value;
	else
		supportEmailValue = "";
	
	if(supportPhone != null)
		supportPhoneValue = supportPhone.value;
	else
		supportPhoneValue = "";
	
	if(confirm != null){
		if(confirm.checked){
			confirmValue = "y";
			confirmType.disabled = false;
		} else {
			confirmValue = "n";
			confirmType.disabled = true;
		}
	} else {
		confirmValue = "n";
		confirmType.disabled = true;
	}
	
	if(notify != null){
		if(notify.checked)
			notifyValue = "y";
		else
			notifyValue = "n";
	}else
		notifyValue = "n";
	
	if(dupeNotify != null){
		if(dupeNotify.checked)
			dupeNotifyValue = "y";
		else
			dupeNotifyValue = "n";
	}else
		dupeNotifyValue = "n";
	
	if(reminder != null){
		if(reminder.checked)
			reminderValue = "y";
		else 
			reminderValue = "n";
		
	}else
		reminderValue = "n";
	
	if(recurring != null){
		if(recurring.checked)
			recurringValue = "y";
		else
			recurringValue = "n";
	}else
		recurringValue = "n";
	
	if(valueFloat == null)
		url="/do/backoffice/content/program/paint/message/flow?confirm="+confirmValue+"&notify="+notifyValue+"&dupeNotify="+dupeNotifyValue+"&reminder="+reminderValue+"&recurring="+recurringValue+"&valueFloat=0&affiliate="+affiliateValue+"&company="+companyOwnedValue+"&shortcode="+shortcodeValue+"&keyword="+keywordValue+"&source="+sourceValue+"&typeString="+typeStringValue+"&language="+languageValue+"&confirmType="+confirmTypeValue+"&type="+typeValue+"&subtype="+subtypeValue+"&frequency="+frequencyValue+"&productDesc="+prodDescValue+"&attProdDesc="+attProdDescValue+"&vznProdDesc="+vznProdDescValue+"&price="+priceValue+"&contentType="+contentTypeValue+"&supportEmail="+supportEmailValue+"&supportPhone="+supportPhoneValue;
	else{
		valueFloatValue = valueFloat.value;
		url="/do/backoffice/content/program/paint/message/flow?confirm="+confirmValue+"&notify="+notifyValue+"&dupeNotify="+dupeNotifyValue+"&reminder="+reminderValue+"&recurring="+recurringValue+"&valueFloat="+valueFloatValue+"&affiliate="+affiliateValue+"&company="+companyOwnedValue+"&shortcode="+shortcodeValue+"&keyword="+keywordValue+"&source="+sourceValue+"&typeString="+typeStringValue+"&language="+languageValue+"&confirmType="+confirmTypeValue+"&type="+typeValue+"&subtype="+subtypeValue+"&frequency="+frequencyValue+"&productDesc="+prodDescValue+"&attProdDesc="+attProdDescValue+"&vznProdDesc="+vznProdDescValue+"&price="+priceValue+"&contentType="+contentTypeValue+"&supportEmail="+supportEmailValue+"&supportPhone="+supportPhoneValue;
	}
	//alert("confirm="+confirmValue+"&notify="+notifyValue+"&dupeNotify="+dupeNotifyValue+"&reminder="+reminderValue+"&recurring="+recurringValue+"&keyword="+keyword);
	
	xmlHttp.onreadystatechange=returnMessageFlow;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function returnMessageFlow() 
{ 
	if (xmlHttp.readyState==4)
	{
		//Set the Custom Message Flow
		var messageFlowDiv 	= document.getElementById("messageFlow");
		var iphoneTitleTime	= document.getElementById("time");
		var messageFlowCopy = document.getElementById("messageFlowCopy");
		var result			= xmlHttp.responseText;
		var splitter		= result.split("|");
		var messageFlow 	= splitter[0];
		var iphoneTime 		= splitter[1];
		var messageFlowC	= splitter[2];
		messageFlowDiv.innerHTML = messageFlow;
		iphoneTitleTime.innerHTML = iphoneTime;
		messageFlowCopy.innerHTML = messageFlowC;
		//The message flow passed success fully lets populate the iphone
		getIphoneMessageFlow();
	}
}

function getIphoneMessageFlow(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 	
	
	url="/do/backoffice/content/program/paint/iphone/message/flow";
	xmlHttp.onreadystatechange=returnIphoneMessageFlow;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
	
}

function returnIphoneMessageFlow() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var showIphoneMessageFlow = window.frames[0].document.getElementById("iphoneMessageFlow");
		showIphoneMessageFlow.innerHTML = xmlHttp.responseText;			                                        
	}
}


function setCustomMessage()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var filter = document.getElementById("filter");
	var message = document.getElementById("message");
	
	url="/do/backoffice/content/program/custom/message/flow?filter="+filter.value+"&message="+message.value;
	xmlHttp.onreadystatechange=returnCustomMessageFlow;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function returnCustomMessageFlow() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var confirm 	= parent.document.getElementById("confirm");
		var notify		= parent.document.getElementById("notify");
		var dupeNotify	= parent.document.getElementById("dupeNotify");
		var reminder 	= parent.document.getElementById("reminder");
		var recurring	= parent.document.getElementById("recurring");
		var valueFloat	= parent.document.getElementById("valueFloat");
		var confirmValue,notifyValue,dupeNotifyValue,reminderValue,recurringValue = "";
		
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		if(confirm.checked)
			confirmValue = "y";
		else
			confirmValue = "n";
		
		if(notify.checked)
			notifyValue = "y";
		else
			notifyValue = "n";
		
		if(dupeNotify.checked)
			dupeNotifyValue = "y";
		else
			dupeNotifyValue = "n";
		
		if(reminder.checked)
			reminderValue = "y";
		else 
			reminderValue = "n";
		
		if(recurring.checked)
			recurringValue = "y";
		else
			recurringValue = "n";
		
		//alert("confirm="+confirmValue+"&notify="+notifyValue+"&dupeNotify="+dupeNotifyValue+"&reminder="+reminderValue+"&recurring="+recurringValue);
		
		url="/do/backoffice/content/program/paint/message/flow?confirm="+confirmValue+"&notify="+notifyValue+"&dupeNotify="+dupeNotifyValue+"&reminder="+reminderValue+"&recurring="+recurringValue+"&valueFloat="+valueFloat.value;
		xmlHttp.onreadystatechange=returnMessageFlow;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		TINY.box.hide();
		repaintMessageFlow();
	}
}

function postRequest(url) {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
	alert ("Your browser does not support AJAX!");
		return;
	}
  
        xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange=function() {
      if(xmlHttp.readyState==4) {
    	
    	var result = xmlHttp.responseText;
    	repaintMessageFlow();
    	var keyword = document.getElementById('keyword');
        //document.getElementById("dup_result").innerHTML = result;
        
        //alert(result);
       
        
        if(result.trim() == "NOT AVAILABLE"){
        	keyword.style.background='#fe9191';
        }else if(result.trim() == "SHORTCODE AND KEYWORD COMBINATION AVAILABLE"){
        	keyword.style.background='#b9fd87';
        }else if(result.trim() == "SAME PROGRAM"){
        	keyword.style.background='#fffca6';
        }
       
        var text = keyword.value;
        keyword.value = text.toUpperCase();
      }
    }
    xmlHttp.send(null);
}

function dupCheck(){
  var shortcode_keyword = "";
  var selObj = document.getElementById('shortcode');
  var i;
  for (i = 0; i < selObj.options.length; i++) {
    if (selObj.options[i].selected) {
      shortcode_keyword += selObj.options[i].value;
    }
  }
  
  var key = document.getElementById('keyword');
    if (key.value != null && key != "") {
    	var text = key.value;
    	shortcode_keyword += text.toLowerCase();
    	var url = "/do/backoffice/content/keywordcheck?dup_check=true";
    	url += "&code=" + shortcode_keyword;
    	postRequest(url);	        	
    }
}

function copy()
{
	var copy = document.createElement("input");
	copy.setAttribute("type", "hidden");
	copy.setAttribute("name","copy");
	copy.setAttribute("id", "copy");
	copy.setAttribute("value", "copy");
	
	//var action = document.getElementById("action");
	//action.setAttribute("value", "copy");
	
	document.sweepstakesForm.appendChild(copy);
	
	document.sweepstakesForm.submit();
	
}

function applySave()
{
	var apply = document.createElement("input");
	apply.setAttribute("type", "hidden");
	apply.setAttribute("name","apply");
	apply.setAttribute("id", "apply");
	apply.setAttribute("value", "apply");
	
	document.sweepstakesForm.appendChild(apply);
	
	document.sweepstakesForm.submit();
	
}


function sendRedemptionWinnerEmail(count,type,swi)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var div = document.getElementById("winner"+count);
	var loading = document.getElementById("loading"+count);
	var ccEmailFilter = document.getElementById("ccEmailFilter");
	var ccEmail = document.getElementById("ccEmail");
	var cc = "";
	
	div.style.display = "none";
	loading.style.display = "block";
	
	if(ccEmailFilter.checked){
		if(ccEmail.value != ""){
			cc = ccEmail.value;
		}
	}
	
	
	url="/do/backoffice/sweepstakes/winner/email?count=" + count + "&cc=" + cc + "&type=" + type + "&sweepWinnerId=" + swi;
	xmlHttp.onreadystatechange=returnEmailResult;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function returnEmailResult() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		var results = xmlHttp.responseText;
		var splitter = results.split("|");
		var div = document.getElementById("winner" + parseInt(splitter[1].trim()));
		var loading = document.getElementById("loading" + parseInt(splitter[1].trim()));
		var tempHTML = "";
		var status = splitter[0];
		
		
		
		if(status == "error"){
			tempHTML += div.innerHTML;
			div.innerHTML = "Email did not go out<br>";
		}else if(status == "success"){
			tempHTML += div.innerHTML;
			div.innerHTML = "Email sent<br>";
		}
		
		loading.style.display = "none";
		div.style.display = "block";
	}
}

function confirmSweepstakesDelete(sweepstakesId,page,size){
	
	var answer = confirm("Are you sure you want to delete this sweepstakes?");
	
	if(answer)
		window.location='/do/backoffice/sweepstakes/manager?action=delete&id=' + sweepstakesId + '&page=' + page + '&size=' + size;
	
}
		
//AJAX Component
//Generic XML HTTP Object
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

