function pop_window(url, inwidth, inheight) {
	var options = 'scrollbars,resizable,width=' + inwidth + ',height=' + inheight;
	var popit = window.open(url, 'console', options);
}
function setLocation(url) {
	window.top.location.href = url;
	return false;
}
function byId(id) {
	return document.getElementById(id);
}
function addLoadEvent(func) {
	var oldOnload = window.onload; 
	if (typeof window.onload != "function") { 
    	window.onload = func; 
  	} else { 
    	window.onload = function() { 
      		if (oldOnload) { 
        		oldOnload(); 
      		} 
      		func(); 
      	}
    } 
}

function isNumeric(strString)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789";
	var strChar;
	
	if (strString.length == 0) 
	{
		return false;
	}
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length; i++)	
	{
	   strChar = strString.charAt(i);
	   if (strValidChars.indexOf(strChar) == -1)
	   {
	      return false;
	   }
	}
	
	return true;
}

		
function leadToWater(i) {
	switch(Number(i)){
		case 0: // STEP_1
			new Effect.Opacity('create_sub', {duration:0.5,from:0.999999, to:0.999999});
			new Effect.Opacity('create_audio', {duration:0.5,from:0.999999, to:0.5});
			new Effect.Opacity('create_call', {duration:0.5,from:0.999999, to:0.5});
		break;
		case 1: // STEP_2
			new Effect.Opacity('create_sub', {duration:0.5,from:0.999999, to:0.5});
			new Effect.Opacity('create_audio', {duration:0.5,from:0.999999, to:0.999999});
			new Effect.Opacity('create_call', {duration:0.5,from:0.999999, to:0.5});
		break;
		case 2: // STEP_3
			new Effect.Opacity('create_sub', {duration:0.5,from:0.999999, to:0.5});
			new Effect.Opacity('create_audio', {duration:0.5,from:0.999999, to:0.5});
			new Effect.Opacity('create_call', {duration:0.5,from:0.999999, to:0.999999});
		break;
//		case 3: // COMPLETE
//			new Effect.Opacity('sub', {duration:0.5,from:0.999999, to:0.5});
//			new Effect.Opacity('audio', {duration:0.5,from:0.999999, to:0.5});
//			new Effect.Opacity('call', {duration:0.5,from:0.999999, to:0.5});
//		break;
//		case 4:
//			new Effect.Opacity('sub', {duration:0.5,from:0.999999, to:0.999999});
//			new Effect.Opacity('audio', {duration:0.5,from:0.999999, to:0.999999});
//			new Effect.Opacity('call', {duration:0.5,from:0.999999, to:0.999999});
//		break;
//		case 99:
//		break;
		default:
		break;				
	}
}

function closeModal(i){
	switch(Number(i)){
		case 0:	//do nothing
			break;
		case 1:
			parent.parent.GB_hide();  //close modal
			break;
		default:
			break;
		}
}

function refreshScreen(checkbox){document.forms['body:form'].submit();}

function dis(a){
a.disabled = "disabled";
}


var CallWizard = {
    usageType: {
		currentId: 'sidebar_information',
	    instructionMap: {     
			1: 'sidebar_information',
			2: 'sidebar_information',
			3: 'sidebar_sales_b2c',
			4: 'sidebar_sales_b2b', 
			5: 'sidebar_donation_b2c', 
			6: 'sidebar_donation_b2b'
		}
	},
	
	audio_files: new Hash(),

	toggleSection: function (checkbox, content) {
		if ( checkbox.checked == true && ! $(content).visible() ){new Effect.BlindDown($(content), {duration:.1, fps:40});}
		else if ( checkbox.checked == false && $(content).visible() ){new Effect.BlindUp($(content), {duration:.1, fps:40});}
	},
	
	formatTime: function (theDate) {
		  thishour = theDate.getHours();
		  thisminute = theDate.getMinutes();
		  if (thishour > 11) {
		    thishalf="PM";
		  } else {
		    thishalf="AM";
		  }
		  if (thishour > 12) {
		    thishour -= 12;
		  }
		  if (thisminute < 10) {
		    thisminute = "0"+thisminute;
		  }
		  timeformatted = thishour + ":" + thisminute + " " + thishalf;
		  return timeformatted;
	},
		
	toggleStopTime: function (checkbox) {
		var content = "vmessage-stoptime-group";
		var container = document.getElementsByName("body:form:createcallwizardstepthreeView_sendOption");
		var scheduledDate = document.getElementById("body:form:createcallwizardstepthreeView_callRequest_timeScheduled");
		var now = new Date();
		var x;
		var tempTime = $("body:form:time_scheduled_time_input").value;
		if(container.length==3){//ie
			if( container[2].checked == true )
			 x = new Date( scheduledDate.value);
			else
			{
			 x = now;
			 tempTime = CallWizard.formatTime(now);		 
			 }
		}
		if(container.length==2){//firefox
			if( container[1].checked == true )
			 x = new Date( scheduledDate.value);
			else
			{
			 x = now;
			 tempTime = CallWizard.formatTime(now);		 
			 }
		}

		x.setDate(x.getDate()+1);
		var tempMonth = x.getMonth()+1;
		if(tempMonth<10)
		   tempMonth = "0"+tempMonth;
		var temp = tempMonth+"/"+x.getDate()+"/"+x.getFullYear();
		
		if ( checkbox.checked == true && ! $(content).visible() )	{
			new Effect.BlindDown($(content), {duration:.1, fps:40});
			$("body:form:createcallwizardstepthreeView_callRequest_timeStop").value = temp;	
			$("body:form:time_stop_time_input").value = tempTime;	
		}
		else if ( checkbox.checked == false && $(content).visible() ){
			new Effect.BlindUp($(content), {duration:.1, fps:40});
		}
	},	
	
	msgLookupChange: function () {
			var sel = $F('body:form:createcallwizardsteponeView_callRequest_messageLookup');
			var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playaudio" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
			$('playaudio').replace(newLink);		
	},
	
	introLookupChange: function () {
		var sel = $F('body:form:createcallwizardsteponeView_callRequest_introLookup');
		var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playintro" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
		var playintro = $('playintro');
		if(playintro!=null) $('playintro').replace(newLink);		
	},
	
	exitLookupChange: function () {
		var sel = $F('body:form:createcallwizardsteponeView_callRequest_exitLookup');
		var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playexit" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
		var playexit = $('playexit');
		if(playexit!=null)
			$('playexit').replace(newLink);		
	},
	
	machLookupChange: function () {
			if($("playmach")){
		  		var sel = $F('body:form:createcallwizardsteponeView_callRequest_machineLookup');
				var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playmach" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
				var playmach = $('playmach');
				if(playmach!=null)
					$('playmach').replace(newLink);
			}
	},
	
	xferLookupChange: function () {
			var sel = $F('body:form:createcallwizardsteponeView_callRequest_transferLookup');
			var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playxfer" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
			var playxfer = $('playxfer');
			if(playxfer!=null)
				$('playxfer').replace(newLink);		
	},
	
	optinLookupChange: function () {
		var sel = $F('body:form:createcallwizardsteponeView_callRequest_optinLookup');
		var newLink = '<a title="Play Audio" href="'+CallWizard.audio_files.get(sel) +'" id="playoptin" target="_blank"><img alt="Play Audio" class="play-audio" src="/account/images/sound_none.png"/></a>';
		var playoptin = $('playoptin');
		if(playoptin!=null)
			$('playoptin').replace(newLink);		
	},
	
	
	toggleIntro: function ( checkbox ) {
		if ($(checkbox) == null) return;
		CallWizard.toggleSection($(checkbox), "vmessage-intro-group");
		$("body:form:createcallwizardsteponeView_callRequest_useIntroMessage").checked = $(checkbox).checked;
	},
	
	toggleExit: function (checkbox){
		if ($(checkbox) == null) return;
		CallWizard.toggleSection( $(checkbox), "vmessage-exit-group" );
	},
	
	toggleMachine: function (checkbox) {
		if ($(checkbox) == null) return;
		CallWizard.toggleSection($(checkbox), "vmessage-machine-group");
		$("body:form:createcallwizardsteponeView_callRequest_playOnVoicemail").checked = $(checkbox).checked;
	},
	
	toggleMachineContent: function (checkbox) {
		if ($(checkbox) == null) return;
		CallWizard.toggleSection($(checkbox), "vmessage-machine-alt");
		$("body:form:createcallwizardsteponeView_callRequest_useMachineMessage").checked = $(checkbox).checked;
	},
	

	toggleTransfer: function (checkbox) {
		if ($(checkbox) == null) return;
		// optin and transfer are mutually exclusive
		CallWizard.toggleSection($(checkbox), "vmessage-transfer-group");
		var optin = $('body:form:createcallwizardsteponeView_callRequest_useOptinMessage');
		if (optin == null) return;
		
		if ( $(checkbox).checked ) {
			
			optin.checked = false;
			optin.disabled = true;
			CallWizard.toggleSection( optin, 'vmessage-optin-group' );
		}
		else {
			optin.disabled = false;
		}
	},	
	
	toggleOptin: function (checkbox) {
		if ($(checkbox) == null) return;
		// optin and transfer are mutually exclusive
		CallWizard.toggleSection($(checkbox), "vmessage-optin-group");
		var transfer = $('body:form:createcallwizardsteponeView_callRequest_enableTransfer');
		if (transfer == null) return;
		
		if ( $(checkbox).checked ) { 
			
			transfer.checked = false;
			transfer.disabled = true;
			CallWizard.toggleSection( transfer, 'vmessage-transfer-group' );
		}
		else {
			transfer.disabled = false;
		}
		
	},
	
	toggleScheduledTime: function (checkbox) {
		var content = "vmessage-scheduledtime-group";
		var container = document.getElementsByName("body:form:createcallwizardstepthreeView_sendOption");
		if(container.length==3){//ie
			if( container[2].checked == true && ! $(content).visible())	{new Effect.BlindDown($(content), {duration:.1, fps:40});
			}else if ( container[2].checked == false && $(content).visible() ){
				new Effect.BlindUp($(content), {duration:.1, fps:40});
			}
		}
		if(container.length==2){//firefox
			if( container[1].checked == true && ! $(content).visible()){
				new Effect.BlindDown($(content), {duration:.1, fps:40});
			}else if ( container[1].checked == false && $(content).visible() ){
				new Effect.BlindUp($(content), {duration:.1, fps:40});
			}
		}
	},
	
	sendConfirm: function(str) {
		var container = document.getElementsByName("body:form:messageupdateView_sendOption");
		var sendDate = document.getElementById("body:form:messageupdateView_callRequest_timeScheduled");
		var timeStr;
		if(container.length==2){//firefox
			if(container[1].checked == true)
				timeStr = "The VMessage is scheduled on " + sendDate.value +". "+ str + "?";
			else
				timeStr = str + " immediately?";
		}
		if(container.length==3){//ie
			if(container[2].checked == true)
				timeStr = "The VMessage is scheduled on " + sendDate.value +". "+ str + "?";
			else
				timeStr = str + " immediately?";
		}
		if (!confirm(timeStr)){
			return false;
		}else{
			return true;	
		}	
	},
		
	stepOneSetup: function () {

		var el = $('body:form:createcallwizardsteponeView_callRequest_messageLookup');
		if ( el ) { new Form.Element.EventObserver( el, CallWizard.msgLookupChange ); }
		
		el = $('body:form:createcallwizardsteponeView_callRequest_introLookup');
		if ( el ) { new Form.Element.EventObserver( el, CallWizard.introLookupChange ); }
		
		el = $('body:form:createcallwizardsteponeView_callRequest_exitLookup');
		if  (el ) { new Form.Element.EventObserver( el, CallWizard.exitLookupChange ); }
		
		el = $('body:form:createcallwizardsteponeView_callRequest_machineLookup');
		if ( el ) { new Form.Element.EventObserver( el, CallWizard.machLookupChange ); }
		
		el = $('body:form:createcallwizardsteponeView_callRequest_transferLookup');
		if ( el ) { new Form.Element.EventObserver( el, CallWizard.xferLookupChange ); }
		
		el = $('body:form:createcallwizardsteponeView_callRequest_optinLookup');
		if ( el ) { new Form.Element.EventObserver( el, CallWizard.optinLookupChange ); }
		
		CallWizard.toggleIntro('body:form:createcallwizardsteponeView_callRequest_useIntroMessage');
		CallWizard.toggleMachine( 'body:form:createcallwizardsteponeView_callRequest_playOnVoicemail' );
		CallWizard.toggleExit( 'body:form:createcallwizardsteponeView_callRequest_useExitMessage' );
		CallWizard.toggleMachineContent( 'body:form:createcallwizardsteponeView_callRequest_useMachineMessage' );
		CallWizard.toggleTransfer( 'body:form:createcallwizardsteponeView_callRequest_enableTransfer' );
		CallWizard.toggleOptin('body:form:createcallwizardsteponeView_callRequest_useOptinMessage');
		CallWizard.updateSidebar();
	},
	
	stepTwoSetup: function () {
		new Effect.Highlight('body:form:addRecipient_link', {startcolor:'#ffff99', endcolor:'#ffffff',duration: 2.0});
		new Effect.Highlight('body:form:addList_link', {startcolor:'#ffff99', endcolor:'#ffffff',duration: 2.0});
	},
	
	
	
	scheduleCallSetup: function () {
		if ($("body:form:createcallwizardstepthreeView_callRequest_useStopTime").checked == true){
			$("vmessage-stoptime-group").show();
		}else{
			$("vmessage-stoptime-group").hide();
		}		
		var container = document.getElementsByName("body:form:createcallwizardstepthreeView_sendOption");
		if(container.length==2)	{//firefox
			if(container[1].checked == true){
				$("vmessage-scheduledtime-group").show();
			}else{
				$("vmessage-scheduledtime-group").hide();
			}
		}
		if(container.length==3)	{//ie
			if(container[2].checked == true){
				$("vmessage-scheduledtime-group").show();
			}else{
				$("vmessage-scheduledtime-group").hide();
			}
		}
	},
		
	updateSidebar: function() {
		var callType = $("body:form:createcallwizardsteponeView_callRequest_usageType");
		var instr = CallWizard.usageType.instructionMap[callType.value];
		$(CallWizard.usageType.currentId).hide();
		$(instr).show();
		CallWizard.usageType.currentId = instr;
	},
	
	reviewSetup: function () { new Effect.Highlight('body:form:createcallwizardstepfourView_send', {startcolor:'#ffff99', endcolor:'#ffffff',duration: 2.0}); }
}

var ImportWizard = {
	nextConfirm: function (str) {
		var sendEmail = document.getElementById("body:form:subscriberimportwizardstepthreeView_email");
		var emailStr = sendEmail.value;
		var messageStr;
		if(emailStr.length==0){
				messageStr =  str;
			if (!confirm(messageStr)){
				return false;
			}else{
				return true;	
			}	
		}
		else
		{
		return true;
		}
	}
}
var OptinEditor = {
	containerStyleId: 'template-container',
//	radioGroupName: 'renderTypeSelect',
	modes: {
		'TOP_TO_BOTTOM': { style: 'topdown', width: 800, height: 100 },
		'SIDE_BY_SIDE': { style: 'twocol', width: 170, height: 400 }
	},
	selectedRadio: null,
//	selectedMode: this.modes['SIDE_BY_SIDE'],
	toggleTemplate: function (form) {
	    var radios = $(form).getInputs('radio' );
	    var checked = radios.find(function(radio) { 
	    	return radio.name.lastIndexOf('renderTypeSelect') != -1 && radio.checked; 
	    });
	    if ( checked == null || checked == this.selectedRadio ) return;
	    var modeValue = checked.value.split('|')[1];
	    if ( this.modes[modeValue] == null ) return;
	    
	    this.selectedRadio = checked;
	    this.selectedMode = this.modes[modeValue];
	
	    this.updateView();
	    /*
	    Effect.Fade(this.containerStyleId, { 
	        duration: 0.2, 
	        queue: { position:'end', scope: 'editorscope' }, 
	        afterFinish: function() { OptinEditor.updateView() } 
	    });
	    
	    Effect.Appear(this.containerStyleId, {
	        duration: .2, 
	        queue: { position:'end', scope: 'editorscope' }
	    });
	    */
	  },
	  
	  updateView: function()
	  {
		  var m = this.selectedMode;
		  $(this.containerStyleId).className = 'template-editor ' + m['style'];
		  tinyMCE.activeEditor.theme.resizeTo( m['width'], m['height'] );
	  }   
};

function refreshView(str)
{
	if(str == "topdown")
	{
		 tinyMCE.activeEditor.theme.resizeTo( 800, 100);
	}	
	else if(str == "twocol")
	{
		 tinyMCE.activeEditor.theme.resizeTo( 170, 400 );
	}
}

var OptinEditorNew = {
		containerStyleId: 'template-container',
		modes: {
			'TOP_TO_BOTTOM': { style: 'topdown', width: 800, height: 100 },
			'SIDE_BY_SIDE': { style: 'twocol', width: 170, height: 400 }
		},
		selectedRadio: null,
		toggleTemplate: function (form) {
		    var radios = $(form).getInputs('radio' );
		    var checked = radios.find(function(radio) { 
		    	return radio.name.lastIndexOf('accountoptinView_renderType') != -1 && radio.checked; 
		    });
		    if ( checked == null || checked == this.selectedRadio ) return;
		    var modeValue = checked.value;
		    if ( this.modes[modeValue] == null ) return;
		    
		    this.selectedRadio = checked;
		    this.selectedMode = this.modes[modeValue];
		
		    this.updateViewNew();		    
		  },
		  
		  updateViewNew: function()
		  {
			  var m = this.selectedMode;
			  $(this.containerStyleId).className = 'template-editor ' + m['style'];
			  tinyMCE.activeEditor.theme.resizeTo( m['width'], m['height'] );
		  }   
	};