function changePayPer(){

	if($('payPerCpm').checked==true){
		$('payFor').innerHTML = 'CPM';
		$('tooltipCpc').style.display='none';
		$('tooltipCpm').style.display='';
		$('cpc_bid').style.display='none';
		$('cpm_bid').style.display='';
	}else{
		$('payFor').innerHTML = 'CPC';
		$('tooltipCpc').style.display='';
		$('tooltipCpm').style.display='none';
		$('cpm_bid').style.display='none';
		$('cpc_bid').style.display='';
	}
}
function dateError(error){
	var div = $('dateError');
	div.innerHTML = error;
}

function newError(err,t){
	$('errorCont').style.display='';
	new Insertion.Bottom('errorList',"<li>"+err+"</li>");
}
function clearErrors(){
	$('errorList').innerHTML='';
}
function checkForm(){
	clearErrors();
	var error = false; 
	if($('cpm').value<0.05 || $('cpc').value<.10){
		if($('cpm').value<.10)
			newError('Minimum CPM is $0.10');
		else if($('cpc').value<.10)
				newError('Minimum CPC is $0.10');
		error = true; 
	}
	if($('daily_budget').value<5){
		newError('Minimum daily budget is $5');
		error = true;
	}
	if($('budget').value<50){
		newError('Minimum total budget is $50');
		error = true;
	}
	if(error==false){
		if(typeof document.forms[0].submit=="function")
			document.forms[0].submit();
		else {
			document.forms[0].appendChild(document.createElement("submit")).click();
			//document.forms[0].submit.click();
		}
			
	}else
		return false;
}
function changeEcpm(el){
	var cpm = $('minEcpm');
	var val = el.options[el.selectedIndex].value;
	cpm.innerHTML = cat[val];
	selectedCat = val; 
}

function parseId(id){
	id = id.split('_');
	return id[1];
}
function toggleP(name){
	
	var divs = ['Keywords','Profile','Category'];
	
	divs.each(function(item) {
		
	//	var toggle = $('toggle'+item);
		var extend = $('extend'+item);
		
		if(item==name){
			//toggle.className='toggleOpen';
			extend.show();
		}else{
			//toggle.className='toggle';
			extend.hide();
		}
	});
}
function changeBehavTo(behav){
	var aud = $('audience');
	var id = "cont_"+behav;
	$('lnk_'+behav).addClassName('hoverLink');
	var conts = aud.childElements();
	var selected = 0;
	
	for(i=0;i<conts.length;i++){
		cont = conts[i];
		if(cont.id==id){
			selected = cont;
			cont.style.display='';
			$('behavior').value = behav;
			$('title').innerHTML = behav.replace(/-/g," ");
		}else{
			cont.style.display='none';
			be = parseId(cont.id);
			$('lnk_'+be).removeClassName('hoverLink');
		}
	}
}
function addCounties(action,what){
	
	var countries =  $('countries');
	var countriesNot =  $('countriesNot');
	
	var countryList = countries.childElements();
	var countryListNot = countriesNot.childElements();
	
	if(what=='all'){
		if(action=='add'){
			for(i=0;i<countryListNot.length;i++){
				var not = $(countryListNot[i]);		
				countriesNot.removeChild(not);
				countries.appendChild(not);
			}
		}else{
			for(i=0;i<countryList.length;i++){
				var not = $(countryList[i]);	
				countries.removeChild(not);
				countriesNot.appendChild(not);
			}
		}
	}else{
		if(action=='add'){
			for(i=0;i<countryListNot.length;i++){
				var not = $(countryListNot[i]);	
				if(not.selected==true){
					countriesNot.removeChild(not);
					countries.appendChild(not);
				}
			}
		}else{
			for(i=0;i<countryList.length;i++){
				var not = $(countryList[i]);	
				if(not.selected==true){	
					countries.removeChild(not);
					countriesNot.appendChild(not);
				}
			}
		}
	}
										
	return true;
}

function submitbutton(save, form){
	var form = form ? form : $('f');
	if(save)
		$('save').value = save;
	
	if($('countries')){
		var countries =  $('countries');
		var countryList = countries.childElements();
		for(i=0;i<countryList.length;i++){
			var not = $(countryList[i]);	
			not.selected=true;	
		}
	}
	form.submit();
}

function changeBehav(action){
	
	var aud = $('audience');
	var conts = aud.childElements();
	var selected = 0;
	for(i=0;i<conts.length;i++){
		cont = conts[i];
		if(cont.style.display=='')
			selected = cont;
	}
	if(action=='prev'){
		var prev = selected.previousSiblings();
       	if(prev.length>0){
			var id = prev[0];
		}else{
			var id = conts.last();
		}
	}else{
		var next = selected.nextSiblings();
       	if(next.length>0){
			var id = next[0];
		}else{
			var id = conts.first();
		}
	}
	//alert(id.id);
	var be = parseId(id.id); // new selected behavior
	var li = parseId(selected.id); // old selected behavior
	$('behavior').value = be;
	$('lnk_'+be).addClassName('hoverLink');
	/*Event.observe(link,'mouseout',function(){link.className='hover';});*/
	
	/*var obj = {
  
	  fx: function(event) {
	  		/*alert(event);
	  	alert(link.id);* /
	  	  link.className = 'hover';
	  },
	};
	
	obj.bfx = obj.fx.bindAsEventListener(obj);

	Event.observe(link, 'mouseout', obj.bfx);*/
	
	//Event.stopObserving(link, 'mouseout', obj.bfx);


	
	/*Event.stopObserving*/
	$('lnk_'+li).removeClassName('hoverLink');
	$('title').innerHTML = be.replace(/-/g," ");
	id.show();
	selected.hide();
}

function updateMM(){
	$('adPreview').show();
	
	var title = encodeURIComponent($('title').value);
	var description1 = encodeURIComponent($('description1').value);
	var description2 = encodeURIComponent($('description2').value);
	var dest_url = encodeURIComponent($('dest_url').value); //.replace(/ /gi, '&nbsp;'));
	var frame = $('frame-preview');
	var frameSrc = '/create-ad/ods.php?';
	frameSrc +='title='+title;
	frameSrc +='&desc='+description1+"<br/>"+description2;
	frameSrc +='&dest_url='+dest_url;
	frame.src = frameSrc;
}