/* global scripts */

// map yui namespace
var YUA = YAHOO.util.Anim;
var YUD = YAHOO.util.Dom;
var YUE = YAHOO.util.Event;
var YUZ = YAHOO.util.Easing;
var YWT = YAHOO.widget.Tooltip;
var $ = YUD.get;

// global init functions
YUE.on(window,'load',setFunnel);
YUE.on(window,'load',setInputHover);
YUE.on(window,'load',setPreviewLinks);

/* home page */

// function to open flash demo
function openDemo(){
	var params = 'scrollbars=no,resizable=no,toolbar=no,status=no,menubar=no,location=no,height=650,width=850'
	var theDemo = window.open('/corp/demo.jsp', 'demo', params);
	if (window.focus) theDemo.focus();
}

// setup preview links
function setPreviewLinks(){
	if (!YUD.getElementsByClassName) return;
	pLinks = YUD.getElementsByClassName('previewLink','a');
	for (i=0; i<pLinks.length; i++){
		pLinks[i].onclick = function(){
			openPreview(this.search);
			return false;
		}
	}
}

// open preview pages
function openPreview(arg){
	movie = (arg.indexOf('adv') != -1) ? 'advPreview' : 'pubPreview';
	var params = 'scrollbars=no,resizable=no,toolbar=no,status=no,menubar=no,location=no,height=590,width=770'
	var thePreview = window.open('/corp/preview.jsp?app='+movie, 'preview', params);
	if (window.focus) thePreview.focus();	
}

// add hover state for IE6 buttons
function setInputHover(){
	btns = YUD.getElementsByClassName('btn', 'input');
	if (document.all) {
		for (i=0; i<btns.length; i++){
			btns[i].onmouseover = function() {
				YUD.addClass(this, 'btnHover');	
			}
			btns[i].onmouseout = function() {
				YUD.removeClass(this, 'btnHover');	
			}
		}
	}
}

// function to set sliding panel toggle
function writeToggleLink(state, link){
	imgTxt = (state == 'dn') ? 'View Chart' : 'Close Chart';
	link.innerHTML = imgTxt;
	link.style.display = 'block';
	newImg = document.createElement('img');
	imgArr = '/corp/images/arrow_orange_'+state+'.gif'
	newImg.setAttribute('src', imgArr);
	newImg.setAttribute('alt',imgTxt);
	newImg.setAttribute('title',imgTxt);
	newImg.setAttribute('height','12');
	newImg.setAttribute('width','11');
	link.appendChild(newImg);
}

// setup drawer open/close animations
function setPanel(el){
	if (!document.getElementById) return;
	var toggle = $('toggle');
	if (toggle) writeToggleLink('dn', toggle);
	//var boxes = YUD.getElementsByClassName(boxClass,'div',mainDiv);
	var panel = $(el);
	// hide box
	//panel.style.display = 'none';
	// find panel toggle and attach action
	panel.toggle = $('toggle');
	// add initial state class == 'shut'
	YUD.addClass(panel.toggle, 'shut');
	panel.toggle.onclick = function (){
		showHidePanel(this, 'check');
		return false;
	}
}

// show/hide panel
function showHidePanel(boxLink, action){
	// get panel
	var panel = $('sbar-chart');
	if (action == 'check'){
		// check whether toggle link has 'open' or 'closed' class
		if (YUD.hasClass(boxLink, 'open')){
			showHidePanel(boxLink, 'shut');
			YUD.replaceClass(boxLink, 'open', 'shut');
		} else if (YUD.hasClass(boxLink, 'shut')){
			showHidePanel(boxLink, 'open');
			YUD.replaceClass(boxLink, 'shut', 'open');
			panel.style.paddingBottom = '10px';
			// load flash movie
			//fo.write("sbar-chart");
			
		}
	// open panel
	} else if (action == 'open'){
		//panel.style.display = 'block';
		YUD.replaceClass(panel, 'displayOff', 'displayOn');
		writeToggleLink('up', boxLink);
	// close box
	} else if (action == 'shut'){
		//panel.style.display = 'none';
		YUD.replaceClass(panel, 'displayOn', 'displayOff');
		writeToggleLink('dn', boxLink);
		YUD.replaceClass(panel.toggle, 'open', 'shut');
	}
	return false;
}


// open/close drawer
function doDrawer(boxLink, action){
	// get parent box
	draw = getParentBox(boxLink, 'dBox');
	table = getDownloadTable(draw);
	if (action == 'check'){
		// check whether toggle link has 'open' or 'closed' class
		if (YUD.hasClass(boxLink, 'open')){
			doDrawer(boxLink, 'shut');
			YUD.replaceClass(boxLink, 'open', 'shut');
		} else if (YUD.hasClass(boxLink, 'shut')){
			doDrawer(boxLink, 'open');
			YUD.replaceClass(boxLink, 'shut', 'open');
		}
	// open box
	} else if (action == 'open'){
		table.style.display = 'block';
		//alert('open');
	// close box
	} else if (action == 'shut'){
		table.style.display = 'none';
		//alert('close');
		YUD.replaceClass(draw.toglet, 'open', 'shut');
	}
	return false;
}

	
// set sidebar height to match main if shorter
function setSbarHeight(){
	/*
	if (!$('sbar')) return;
	mainDims = getDims('main');
	mainHeight = mainDims[0];
	//alert('mainHeight: '+ mainHeight);
	sbarDims = getDims('sbar');
	sbarHeight = sbarDims[0];
	//alert('sbarHeight: '+ sbarHeight);
	if (mainHeight > sbarHeight){
		$('sbar').style.height = mainDims[0] + "px";
	}
	*/
}

// get height & width dims of an el using YUD regions
function getDims(id){
	region = YUD.getRegion(id);
	height = region.bottom - region.top;
	width = region.right - region.left;
	regionArr = new Array();
	regionArr[0] = height;
	regionArr[1] = width;
	return regionArr;
}

// function to setup show/hide text blocks
// expects 7 page set variables:

// mRoot: name of div that represents root node of collection
// mParentClass: class of parent element that is clicked to activate show/hide
// mParentShowClass: class name that sets open style of parent element
// mParentHideClass: class name that sets closed style of parent element
// mParentHasRollover: boolean to determine if parents have rollver class
// mParentRolloverClass: class name of rollover parent state
// mChildClass: class of child element that encloses content to show/hide
// mChildShowClass: class name that sets child display:block
// mChildHideClass: class name that sets child display:none;

function setMoreLinks() {
	if (!document.getElementsByTagName) return;
	// set up onclick handlers
	var parents = YUD.getElementsByClassName(mParentClass, '', $(mRoot));
	//alert(parents.length);
	for (i=0; i < parents.length; i++){
		// add onclick handler to parent elements
		parents[i].onclick = function(){
			showHideText(this);
		}
		// if rollover state, add it
		if (mParentHasRollover) {
			parents[i].onmouseover = function(){
				doRollovers(this);
			}
			parents[i].onmouseout = function(){
				doRollovers(this);
			}
		}
		// set parent elements to hidden display state (arrow up) if not explicitly set stay open
		if (mParentHideClass && mParentHideClass != "" && mParentShowClass && !YUD.hasClass(parents[i], mParentShowClass)){
			YUD.addClass(parents[i],mParentHideClass);
		}
	}
	// hide all child content blocks if not explicitly set to stay open
	var children = YUD.getElementsByClassName(mChildClass, '', $(mRoot));
	for (j=0; j < children.length; j++){
		if (!YUD.hasClass(children[j], mChildShowClass)){
			YUD.addClass(children[j],mChildHideClass);
		}
	}
	// display expand all/collapse all link
	YUD.replaceClass($('expander'), 'displayOff', 'displayOn');
	
	// set onclick action for expand all/collapse all button
	//setShowHideAll();
}

// function to do rollovers for parent items
function doRollovers(pObj){
	if (YUD.hasClass(pObj, mParentRolloverClass)){
			YUD.removeClass(pObj, mParentRolloverClass);
	} else {
			YUD.addClass(pObj, mParentRolloverClass);
	}
}

// function to show/hide text blocks
function showHideText(pObj){
	node = pObj;
	foundChild = 0;
	// look through siblings
	while (node.nextSibling){
		node = node.nextSibling;
		if (node.nodeType == 1){
			if (YUD.hasClass(node, mChildClass)){
				foundChild = 1;
				break;
			}
		}
	}
	if (YUD.hasClass(node, mChildHideClass)){
		YUD.replaceClass(node, mChildHideClass, mChildShowClass);
		YUD.replaceClass(pObj, mParentHideClass, mParentShowClass);
	} else {
		YUD.replaceClass(node, mChildShowClass, mChildHideClass);
		YUD.replaceClass(pObj, mParentShowClass, mParentHideClass);
	}
}



/* ----------------------------- */
/*     begin funnel scripts      */
/* ----------------------------- */

// set funnel vars
var usLeadState = 0;
var intlLeadState = 0;
var usFormHeight = '44'; //adjust if form length is changed
var intlFormHeight = '50'; //adjust if form length is changed

// funnel init setup
function setFunnel() {
	if (!YUD.inDocument($('sbarFunnel'))) return;
	resetForms();
	setInputHover();
	
	// get accurate height of lead forms for slide in/out
	//usFormHeight = ((getDims('usLead')[0])/10) + 2;
	//intlFormHeight = ((getDims('intlLead')[0])/10) + 2;
	//alert(usFormHeight + ' / ' + intlFormHeight);
	// hide forms
	//YUD.addClass($('formInset'),'displayOff');
	YUD.addClass($('usLead'),'hidden');
	YUD.addClass($('intlLead'),'hidden');
	
	// prep country select radio buttons
	csTab = $('countrySelect');
	var csBtns = csTab.getElementsByTagName('input');
	for (i=0; i<csBtns.length; i++){
		csBtns[i].checked = false;
		csBtns[i].onclick = function(){
			countryFork(this);
		}
	}
	
	// add update form handlers
	var elms = $('usLeadForm').elements;
	for (i=0; i<elms.length; i++){
		// add onclick handler for radio & checkbox fields other than us/int form toggle
		if ((elms[i].type == 'radio' && elms[i].name != 'northAmMarket') || elms[i].type == 'checkbox'){
			elms[i].onclick = function (){
				updateField(this);
			}
		// add onchange handler for selects
		} else if (elms[i].tagName.toLowerCase() == 'select'){
			elms[i].onchange = function (){
				updateField(this);
			}
		} else if (elms[i].type == 'text'){
			elms[i].onblur = function (){
				updateField(this);
			}
		}
	}
}



// swap the advertiser / publisher tabs
var swapTab = function(e) {
	YUE.preventDefault(e);
	tabClass = '';
	advClass = 'advertiserTop';
	pubClass = 'publisherTop';
	tabObj = $('roleSelectTab');
	// determine which link was clicked & strip alternative class
	if (this.id == 'advTab') {
		tabClass = advClass;
		YUD.removeClass(tabObj, pubClass);
		showForm('adv');
	} else if (this.id == 'pubTab') {
		tabClass = pubClass;
		YUD.removeClass(tabObj, advClass);
		showForm('pub');
		resetForms();
		// set lead form states
		displayModule('usLead','close', '');
		displayModule('intlLead','close', '');
		usLeadState = 0;
		intlLeadState = 0;
	}
	// add appropriate class
	YUD.addClass(tabObj, tabClass);
}

// return to start of advertiser form choice
var leadFormRestart = function(e) { 
	YUE.preventDefault(e);
	if (this.id == 'btnUSStartOver'){
		displayModule('usLead','close',usFormHeight);
	} else if (this.id = 'btnIntlStartOver'){
		displayModule('intlLead','close',intlFormHeight);
	}
	usLeadState = 0;
	intlLeadState = 0;
	$('countrySelector').reset();
	resetForms();
	resetTargetDivs();
	YUD.replaceClass($('thanksTxt'),'displayOn','displayOff');
	location.href = "#sbarFunnel";
}

var targetDivs = ['managedAcct', 'notManagedAcct', 'ineligibleAcct'];
// close all funnel target divs
function resetTargetDivs(){
	for (j=0; j<targetDivs.length; j++){
		if ($(targetDivs[j])) YUD.replaceClass($(targetDivs[j]),'displayOn','displayOff');
	}
}

// redirect us lead form to appropriate take off point
var forkLeadForm = function(e){
	YUE.preventDefault(e);
	
	resetTargetDivs();
	// set defaults
	targetDiv = targetDivs[1]; // not managed
	
	// values to test
	advCatgMenu = $('advCategory');
	advCatgValu = advCatgMenu.options[advCatgMenu.selectedIndex].value;
	advBudgMenu = $('advBudget');
	advBudgValu = advBudgMenu.options[advBudgMenu.selectedIndex].value;
	isAgency		= $('HF_isAgency').value;
	otherChkBox	= $('HF_viaOther').value;
	otherTxtBox = $('HF_viaOtherTxt').value;
	//alert('Agency: '+isAgency+', Category: '+advCatgValu+', Budget: '+advBudgValu);
	
	// check fields
	hideForm = 0;
	errCount = 0;
	errMsg = '';	
	if (isAgency == ''){
		errCount++;
		errMsg += '- Please tell us whether you are an agency or not\n';
	}
	if (advCatgValu == ''){
		errCount++;
		errMsg += '- Please tell us your advertiser category\n';
	}
	if (advBudgValu == ''){
		errCount++;
		errMsg += '- Please tell us your monthly ad budget\n';
	}
	if (otherChkBox == 'yes' && otherTxtBox == ''){
		errCount++;
		errMsg += '- Please tell us your "Other" success location\n';
	}
	if (errCount > 0){
		alert(errMsg);
		//hideForm = 0;
		
		//targetDiv = targetDivs[3]; // not enough info
		//divHeight = 8;
		
	
	// if no errors
	} else {
	
		// form redirect logic
		if (advCatgValu == 'Adult' || advCatgValu == 'Gambling'){
			targetDiv = targetDivs[2]; // ineligible
			divHeight = 12;
			//hideForm = 1;
		} else if (isAgency == 'yes'){
			targetDiv = targetDivs[0]; // yes managed
			divHeight = 22;
			//hideForm = 1;
		} else if (isAgency == 'no' && (advBudgValu == '25K - 50K' || advBudgValu == '50K - 75K' || advBudgValu == '75K+')){
			targetDiv = targetDivs[0]; // yes managed
			//hideForm = 1;
			divHeight = 22;
		} else if (isAgency == 'no'){
			targetDiv = targetDivs[1]; // not managed
			divHeight = 20;
			//hideForm = 1;
		}
		//alert(targetDiv.id);
		//hideForm = 0;
		
		// do we hide the form?
		//if (hideForm > 0){
			// hide form divs...
			//YUD.replaceClass($('roleSelectTab'),'displayOn','displayOff');
			//YUD.replaceClass($('countrySelect'),'displayOn','displayOff');
			//YUD.replaceClass($('usLead'),'displayOn','displayOff');
			//YUD.replaceClass($('funnelIntro'),'displayOn','displayOff');
			// show inset top and target div
			YUD.replaceClass($('btnUSLeadNext'),'displayOn','displayOff');
			YUD.replaceClass($('thanksTxt'),'displayOff','displayOn');
			YUD.replaceClass($('funnelIntro'),'displayOn','displayOff');
			location.href = '#usLeadForm';
			setTimeout("displayModule(targetDiv, 'open', divHeight)",500);
		//}
	}

}

// setup tab swap links
YUE.addListener('advTab', 'click', swapTab);
YUE.addListener('pubTab', 'click', swapTab);

// setup form button actions
YUE.addListener('btnUSStartOver', 'click', leadFormRestart);
YUE.addListener('btnIntlStartOver', 'click', leadFormRestart);
YUE.addListener('btnUSLeadNext', 'click', forkLeadForm);
YUE.addListener('btnStartCampaign', 'click', createAccount);

// function to redirect to account setup
function createAccount(){
	location.href = '/signup/advertiser.htm';	
}


// reset the form elements (when swapping out forms)
hFields = ['HF_isAgency', 'HF_advCategory', 'HF_advBudget', 'HF_viaSearch', 'HF_viaAffiliateNets', 'HF_viaExchanges', 'HF_viaPortals', 'HF_viaAdNetworks', 'HF_viaOther', 'HF_viaOtherTxt']
function resetForms(){
	if (!$('sbarFunnel')) return;
	for (i=0; i< hFields.length; i++){
		$(hFields[i]).value = '';
	}
	YUD.replaceClass($('thanksTxt'),'displayOn','displayOff');
	YUD.replaceClass($('btnUSLeadNext'),'displayOff','displayOn');
	if ($('usLeadForm')) $('usLeadForm').reset();
	if ($('intlLeadForm')) $('intlLeadForm').reset();
	if ($('countrySelector')) $('countrySelector').reset();
}

// setup div arrays for initial hide/display
var advInitSects = new Array('formInset', 'countrySelect');
var advHideSects = new Array('formInset', 'countrySelect', 'usLead', 'intlLead', 'managedAcct', 'notManagedAcct', 'ineligibleAcct', 'notEnoughInfo');
var pubInitSects = new Array('formInset', 'publisherApply');
var pubHideSects = new Array('formInset', 'publisherApply');

// show/hide initial role sections
function showForm(role){
	if (role == 'adv'){
		hideArr = pubHideSects;
		showArr = advInitSects;
	} else if (role == 'pub'){
		hideArr = advHideSects;
		showArr = pubInitSects;
	}
	for (i=0; i<hideArr.length; i++){
		YUD.replaceClass($(hideArr[i]),'displayOn','displayOff');
	}
	for (i=0; i<showArr.length; i++){
		YUD.replaceClass($(showArr[i]),'displayOff','displayOn');
	}
}

// function to fork between US & Intl forms
function countryFork(btn){
	if (btn.value == 'yes'){
		if (usLeadState < 1){
			$('intlLeadForm').reset();
			YUD.addClass($('intlLead'),'displayOff');
			intlLeadState = 0;
			displayModule('usLead','open', usFormHeight);
			usLeadState = 1;
		}
	} else if (btn.value == 'no'){
		if (intlLeadState == 0){
			$('usLeadForm').reset();
			YUD.addClass($('usLead'),'displayOff');
			YUD.replaceClass($('thanksTxt'),'displayOn','displayOff');
			YUD.replaceClass($('btnUSLeadNext'),'displayOff','displayOn');
			resetTargetDivs();
			usLeadState = 0;
			displayModule('intlLead','open', intlFormHeight);
			intlLeadState = 1;
		}
	}
}

// animate open/close of form modules
function displayModule(div,dir,len){
	layer = $(div);
	layerAnim = new YUA(layer);
	if (dir == 'open'){
		YUD.removeClass(layer,'displayOff');
		//if (!document.all) layerAnim.attributes.opacity = { from: 0, to: 1 };
		layerAnim.attributes.height = { from: 0, to: len, unit: 'em' };
	} else {
		layerAnim.attributes.height = { from: len, to: 0, unit: 'em' };
	}
	layerAnim.duration = 0.7;
	layerAnim.method = YUZ.easeOut;
	layerAnim.animate();
}

// function to update form hidden fields
function updateField(sourceField){
	sourceValue = '';
	// if input field...
	if (sourceField.tagName.toLowerCase() == 'input'){
		fieldType = sourceField.type;
		if (fieldType != 'checkbox'){
			sourceValue = sourceField.value;
		} else {
			if (sourceField.checked){
				sourceValue = sourceField.value;	
			} else {
				sourceValue = '';
			}
		}
	// if select field...
	} else if (sourceField.tagName.toLowerCase() == 'select'){
		sourceValue = sourceField.options[sourceField.selectedIndex].value;
	}
	// if corresponding hidden field exists, update it
	targetName = 'HF_' + sourceField.getAttribute('name');
	if (targetField = $(targetName)){
		targetField.value = sourceValue;
		//alert(targetField.getAttribute('id')+': '+targetField.value);
	}
}

// validate ad lead form
function checkIntLeadForm(theForm){
	
	// values to check
	notifyValu  = getRadioValue(theForm, 'notifyMe');
	countryValu = $('00N300000021z4a').options[$('00N300000021z4a').selectedIndex].value;
	isAgency		= getRadioValue(theForm, '00N300000021yJK');
	
	errCount = 0;
	errMsg = '';
	
	// check for notification
	if (notifyValu == ''){
		errCount++;
		errMsg += '- Please tell us if you would like to be notified\n';
	}
	// check for country
	if (countryValu == ''){
		errCount++;
		errMsg += '- Please select a country\n';
	}
	// check for agency
	if (isAgency == ''){
		errCount++;
		errMsg += '- Please tell us if you are an agency\n';
	}
	// check for name
	if (theForm.fullname.value == ''){
		errCount++;
		errMsg += '- Please tell us your company name\n';
	}
	// check for company
	if (theForm.company.value == ''){
		errCount++;
		errMsg += '- Please tell us your name\n';
	}
	// check for email
	if (theForm.email.value == '' || !checkEmail(theForm.email.value)){
		errCount++;
		errMsg += '- Please enter a valid email address\n';
	}

	// check for errors
	// if 3 errors are found, redirect to contact us page
	if (errCount > 0){
		alert(errMsg);
		return false;
	} else {
		return true;
	}
}

// get radio field values
function getRadioValue(theForm, radioName) {
	radioVal = '';
	for (i=0; i<theForm.elements[radioName].length; i++){
	  if (theForm.elements[radioName][i].checked){
	  	radioVal = theForm.elements[radioName][i].value;
	  }
	}
	return radioVal;
}

/* ------------------------------- */
/* Contact & non-Funnel Lead Forms */
/* ------------------------------- */

// function to validate emailAddress
function checkEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str)){
		return true;
	} else {
		return false;
	}
}

// validate contact us form
function checkContactForm(theForm){
	errCount = 0;
	errMsg = '';

	// check for first name
	if (theForm.first_name.value == ''){
		errCount++;
		errMsg += '- You must enter a first name\n';
	}
	// check for last name
	if (theForm.last_name.value == ''){
		errCount++;
		errMsg += '- You must enter a last name\n';
	}
	// check for email
	if (theForm.email.value == '' || !checkEmail(theForm.email.value)){
		errCount++;
		errMsg += '- You must enter a valid email address\n';
	}
	// check for company
	if (theForm.company.value == ''){
		errCount++;
		errMsg += '- You must enter a company name\n';
	}
	
//	if (theForm.captcha.value == ''){
//		errCount++;
//		errMsg += '- You must enter the characters at the bottom of the page\n';
//	}

	// check for errors
	if (errCount > 0){
		alert(errMsg);
		return false;
	} else {
		return true;
	}
}

// swap necessary form fields and values
function showHideAdServers(e) {
	adServerMenu = $('adServerMenu');
	if (this.id == 'adServerYes'){
		YUD.replaceClass(adServerMenu,'displayOff','displayOn');
	} else {
		YUD.replaceClass(adServerMenu,'displayOn','displayOff');
	}
}

// set selected state for passed menu values
function setMenuValues(theMenu, theValue){
	opts = theMenu.options;
	for (i=0; i<opts.length; i++){
		theValue = convert_entities(theValue);
		if (opts[i].value == theValue){
			theMenu.selectedIndex = i;
			break;
		}
	}
}

// convert typical html entities
function convert_entities(str) {
	str = str.replace(/&amp;/g, '&');
	str = str.replace(/&lt;/g, '<');
	str = str.replace(/&gt;/g, '>');
  //alert(str);
  return str;
}

/* ----------------------- */
/* pricing model functions */
/* ----------------------- */

// function to show recommended pricing model
function showPricingModel(modID, modelBoxes){
	// if no modID, exit function
	if (modID == '') return;
	
	// get ref to all pricing model boxes and (re)display them
	modelBoxes = YUD.getElementsByClassName('priceBox', 'div', $('oModel'));
	for (i=0; i<modelBoxes.length; i++){
		YUD.removeClass(modelBoxes[i], 'displayOff');	
	}
	// get ref to recommeded model box, and make a copu
	modelBox = $(modID);
	copyBox = modelBox.cloneNode(true);
	recBox = $('rBox');
	// remove existing children and then append recommended box
	if (recBox.hasChildNodes()) recBox.removeChild(recBox.childNodes[0]);
	recBox.appendChild(copyBox);
	YUD.replaceClass($('rModel'), 'displayOff', 'displayOn');
	// hide model from other options
	YUD.addClass(modelBox, 'displayOff');
	location.href = '#rModel';
	
}

// function to choose pricing model
function chooseModel(){
	modID = '';
	// do error checking
	errCount = 0;
	errMsg = '';
	theForm = $('pricingAdvisor');
	audienceValu = getRadioValue(theForm, 'audience');
	goalValu = getRadioValue(theForm, 'goal');
	beaconValu = getRadioValue(theForm, 'beacon');
	if (audienceValu == ''){
		errCount++;
		errMsg += 'Please choose a campaign audience.\n';
	}
	if (goalValu == ''){
		errCount++;
		errMsg += 'Please choose a primary advertising goal.\n';
	}
	if (beaconValu == ''){
		errCount++;
		errMsg += 'Please choose a beacon experience level.\n';
	}
	if (errCount > 0){
		alert(errMsg);
	} else {
		//alert('Audience: '+audiVal+'\nGoal: '+goalVal+'\nBeacon: '+beacVal);
		
		// if no errors, apply rules for model choice
		if (goalValu == 'reach'){
			modID = 'cpm';
		} else if (goalValu == 'traffic'){
			if (audienceValu == 'local'){
				modID = 'cpm-cpc';
			} else {
				modID = 'cpc';
			}
		} else if (goalValu == 'actions'){
			if (audienceValu != 'local' && beaconValu == 'high'){
				modID = 'cpm-cpa';
			} else {
				modID = 'cpm-cpc';
			}
		}
	}
	return modID;
}

// function to reset pricing form
function resetPricingForm(){
	$('pricingAdvisor').reset();
	YUD.replaceClass($('rModel'), 'displayOn', 'displayOff');
	YUD.replaceClass($('oModel'), 'displayOn', 'displayOff');
}
