var defTile = '10 Cent Ads - Audience Analysis Advertising.  Only 10 Cents';
var defBeginTile = '10 Cent Ads';

var browserIsIE = (navigator.userAgent.indexOf("MSIE") != -1);
var PrintLocation = '';
var IsPrint = false;
function setPrintLocation()
{
	if (PrintLocation == '')
		PrintLocation = document.location + '';
}
function PrintContenWindow()
{
	if (PrintLocation == '')
		PrintLocation = document.location;
	
	var pl = (PrintLocation.indexOf('?') != -1) ? PrintLocation + '&IsPrint=1' : PrintLocation + '?IsPrint=1'; 
	popWin = window.open(pl,'popWin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1040,height=600,left=100,top=100');
	popWin.location.href = pl;
	window.popWin.focus();
//	window.popWin.print();
}
function PrintConten()
{
	window.print();
}
addEvent(window, 'load', setPrintLocation);

function setPreview()
{
	gEl('review').className = 'active';
	show('preview');
}


/* CP */
var cpMin = 155;
var cpMax = 172;
var isIE  = (document.all && document.getElementById);
function gEl(id)
{
	return document.getElementById(id);
}
function cpMenuOver(id)
{
	id = 'cp_back_' + id;
	var _ps = "'" + id + "'" + ',' + 172;
	if (gEl(id).className.indexOf('ctive') != -1)
		return;
	gEl(id).setAttribute('cpDir', '1');
	gEl(id).style.display = 'block';
	setTimeout("cpAni(" + _ps + ")", 100);
}
function cpMenuOut(id)
{
	id = 'cp_back_' + id;
	var _ps = "'" + id + "'" + ',' + 155;
	if (gEl(id).className.indexOf('ctive') != -1)
		return;
	
	gEl(id).setAttribute('cpDir', '0');
	setTimeout("cpAni(" + _ps + ")", 100);
}
function cpAni(id, endW)
{
	var el = gEl(id);
	var dir = (el.getAttribute('cpDir') == '1') ? 3 : -3;
	var dirA = (dir > 0) ? 20 : -20;
	if (!isIE)
		dirA = dirA / 120;
	
	endW = (dir > 0) ? cpMax : cpMin; 
	var curW = getElementWidth(el);
	
	var setW = curW + dir;
	var _ps = "'" + id + "'" + ',' + endW;
	if ((dir > 0 && setW < endW) || (dir < 0 && setW >= endW))
	{
		el.style.width = setW + 'px';
		if (isIE)
			el.filters.alpha.opacity += dirA;
		else
		{
			el.style.MozOpacity = el.style.MozOpacity * 1 + dirA;
			el.style.opacity = el.style.opacity * 1 + dirA;
//			gEl('infor').innerHTML = el.style.opacity + '|' + dirA + '|' + dir + '|'+ setW + el.className;
		}
		setTimeout("cpAni(" + _ps + ")", 50);
	}
	else
	{
		if (dir < 0)
		{
			el.style.display = 'none';
			if (isIE)
				el.filters.alpha.opacity = 0;
			else
			{
				el.style.MozOpacity = 0;
				el.style.opacity = 0;
			}
		}
		else
		{
			if (isIE)
				el.filters.alpha.opacity = 100;
			else
			{
				el.style.MozOpacity = 1;
				el.style.opacity = 1;
			}
			el.style.width = endW + 'px';
		}
	}
}
function getElementWidth(target) 
{
	if (target.clientWidth) return target.clientWidth;
	else if (target.innerWidth) return target.innerWidth;
	else if (target.offsetWidth) return target.offsetWidth;
	else return 0;
}
function setField(key,rt)
{
	if (IsPrint) return;
	var _a = gEl(key + '-display');
	var _b = gEl(key + '-edit');
	_a.style.display = 'none';
	_b.style.display = 'block';
	if (rt && gEl(key + '-rt'))
		gEl(key + '-rt').style.display = 'block';
	
}
function changeBg(who)
{
	var inputObj = gEl(who);
	var inputObjValue = inputObj.value;
	var inputColor = inputObj.style.backgroundColor;

	if (inputObjValue == '') 
		inputObj.style.border ='1px solid #f00';
	else 
		inputObj.style.border ='1px solid #C6C6C6';
}
function cleanIt(elm, isDef)
{
	if (elm.value == isDef)
		elm.value = '';
}
function calXOverride(isNone)
{
	if (!isNone)
		isNone = (gEl('tdt').value == '');
	
	if (isNone)
	{
		gEl('date-to-check-no').className = 'checked';
		gEl('date-to-check-yes').className = 'unchecked';
		gEl('tdt').value = '';
		gEl('date-to-none').className = 'date';
		gEl('date-to-client').innerHTML = 'select';
		gEl('date-to-client').className = 'date alt';
	}
	else
	{
		gEl('date-to-check-no').className = 'unchecked';
		gEl('date-to-check-yes').className = 'checked';
		gEl('date-to-none').className = 'date alt';
		
		gEl('date-to-client').className = (gEl('tdt').value == '') ? 'date alt' : 'date';
		gEl('date-to-client').innerHTML = gEl('tdt').value + '&nbsp;';
	}
	
	gEl('date-from-client').className = (gEl('fdt').value == '') ? 'date alt' : 'date';
	gEl('date-from-client').innerHTML = (gEl('fdt').value == '') ? 'select' : gEl('fdt').value;
	
}

function detCleanF(el)
{
	if (el.getAttribute('defvalue'))
	{
		if (el.value == el.getAttribute('defvalue'))
			el.value = '';
	}
}
function detCleanB(el)
{
	if (el.value == '')
	{
		var k = el.id.replace('-input','');
		gEl(k + '-display').style.display = 'none';
		gEl(k + '-edit').style.display = 'block';
	}
	if (el.getAttribute('defvalue'))
	{
		if (el.value == '')
			el.value = el.getAttribute('defvalue');
	}
}
function checkLength(Id)
{
	var obj = gEl(Id);
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}
function detInit()
{
	if (gEl('campaign-name-input'))
	{
		gEl('campaign-name-input').onselect = function() { detCleanF(this); }
		gEl('campaign-name-input').onclick = function() { detCleanF(this); }
		detCleanB(gEl('campaign-name-input'));
	}
	if (gEl('headline-input'))
	{
		gEl('headline-input').onkeyup = function(e) { detPreview(); }
		gEl('headline-input').onselect = function() { detCleanF(this); }
		gEl('headline-input').onclick = function() { detCleanF(this); }
		gEl('headline-input').onblur = function()  { detPreview(); }
		detCleanB(gEl('headline-input'));
	}
	if (gEl('body-input'))
	{
		gEl('body-input').onkeyup = function(e) { checkLength('body-input'); detPreview(); }
		gEl('body-input').onselect = function() { detCleanF(this); }
		gEl('body-input').onclick = function() { detCleanF(this); }
		gEl('body-input').onblur = function()  { checkLength('body-input'); detPreview(); }
		detCleanB(gEl('body-input'));
	}
	if (gEl('display-url-input'))
	{
		gEl('display-url-input').onkeyup = function(e) { detPreview(); }
		gEl('display-url-input').onselect = function() { detCleanF(this); }
		gEl('display-url-input').onclick = function() { detCleanF(this); }
		gEl('display-url-input').onblur = function()  { detPreview(); }
		detCleanB(gEl('display-url-input'));
	}
	if (gEl('destination-url-input'))
	{
		gEl('destination-url-input').onkeyup = function(e) { detPreview(); }
		gEl('destination-url-input').onselect = function() { detCleanF(this); }
		gEl('destination-url-input').onclick = function() { detCleanF(this); }
		gEl('destination-url-input').onblur = function()  { detPreview(); }
		detCleanB(gEl('destination-url-input'));
	}
	detPreview();
}

function n_detInit()
{
	if (gEl('headline-input'))
	{
		gEl('headline-input').onkeyup = function(e) { detPreview(); }
		gEl('headline-input').onblur = function()  { detPreview(); }
	}
	if (gEl('body-input'))
	{
		gEl('body-input').onkeyup = function(e) { checkLength('body-input'); detPreview(); }
		gEl('body-input').onblur = function()  { checkLength('body-input'); detPreview(); }
	}
	if (gEl('display-url-input'))
	{
		gEl('display-url-input').onkeyup = function(e) { detPreview(); }
		gEl('display-url-input').onblur = function()  { detPreview(); }
	}
	if (gEl('destination-url-input'))
	{
		gEl('destination-url-input').onkeyup = function(e) { detPreview(); }
		gEl('destination-url-input').onblur = function()  { detPreview(); }
	}
	detPreview();
}

function detPreview()
{
	var keys = new Array('headline','body','display-url','destination-url');
	for (var k in keys)
	{
		if (gEl(keys[k] + '-input'))
		{
			var v = gEl(keys[k] + '-input').value;
			v = v.replace(/</g, '&lt;');
			v = v.replace(/>/g, '&gt;');
			if (gEl(keys[k] + '-preview'))
			{
				if (keys[k] == 'destination-url')
					gEl(keys[k] + '-preview').href = v;
				else if (keys[k] == 'display-url')
					gEl('destination-url-preview').innerHTML = v;
				else
					gEl(keys[k] + '-preview').innerHTML = v;
			}
		}
	}
}

/* demographics */
function dmgInit()
{
	var elms = document.getElementsByTagName('a');
	var keys= new Array();
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('ldmg-') != -1)
		{
//			if (elms[i].id.indexOf('--1') == -1)
//			{
				var key = elms[i].id.replace(/[0-9]/g,'').replace('--', '-');
				
				if (elms[i].className == 'active')
					keys[key] = (keys[key]) ? keys[key]+1 : 1;
				else if (!keys[key])
					keys[key] = 0;

				var _id = elms[i].id.replace('ldmg-', 'dmg-');
				elms[i].className = (gEl(_id).checked) ? 'active' : '';
				elms[i].onclick = function() { 
					this.blur();
					dmgCheck(this);
					return false;
				}
//			}
		}
	}
	for (var i in keys)
	{
		if (keys[i] == 0)
			dmgCheck(gEl(i + '-1'));
	}
}

function dmgCheck(elm)
{
	elm.className = (elm.className == 'active') ? '' : 'active';
	gEl(elm.id.replace('ldmg-', 'dmg-')).checked = (elm.className == 'active');
	
	var key = elm.id.replace(/[0-9]/g,'');
	key = key.replace('--', '-');
	var isAll = (elm.id.replace(key,'') == '-1');
	key = key.replace('ldmg-', '');
	
	if (!isAll)
	{
		gEl('dmg-' + key + '-1').checked = false;
		gEl('ldmg-' + key + '-1').className = '';
	}
	
	
	
	var _ct = 0;
	var _cc = 0;
	
	// find total elms in group
	for (var i = -1; i < 20; i++)
	{
		if (gEl('ldmg-' + key + i))
		{
			_ct++;
			if (gEl('dmg-' + key + i).checked)
				_cc++;
		}
	}
	
	if (isAll || _ct - _cc <= 1 || _cc == 0)
	{
		gEl('dmg-' + key + '-1').checked = true;
		gEl('ldmg-' + key + '-1').className = 'active';
		
		for (var i = 0; i < _ct; i++)
		{
			if (gEl('dmg-' + key + i))
			{
				gEl('dmg-' + key + i).checked = false;
				gEl('ldmg-' + key + i).className = '';
			}
		}
	}
}

/* channels */
function chnInit()
{
	var elms = document.getElementsByTagName('span');
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && (elms[i].id.indexOf('dchn-') != -1 || elms[i].id.indexOf('achn-') != -1))
		{
			elms[i].onclick = function() { 
				this.blur();
				chnCheck(this);
				return false;
			}
		}
	}
}
function chnCheck(elm)
{
	var isCheck = (elm.id.indexOf('dchn-') != -1);
	var elms = document.getElementsByTagName('span');
	var _ht = '<div class="title alt">Chosen Channels</div>';
	
	if (!isCheck)
	{
		var _id = elm.id.replace('achn-','dchn-');
		gEl(_id).className = '';
	}
	else
		elm.className = 'active';

	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('dchn-') != -1 && elms[i].className == 'active') 
		{
			var _id = elms[i].id.replace('dchn-','');
			var _cht = elms[i].getAttribute('title');
			elms[i].className = 'active';
			var _fn = (elms[i].getAttribute('zoomimg'))
				? 'onclick="return zoomClick(this)" onmouseover="zoomPreload(this)" dozoom="' + elms[i].getAttribute('zoomimg') + '"'
				: '';
						
			_cht = _cht.replace(/\s+/g,' ');

			_ht = _ht + '<a>' + 
				'<input type="checkbox" name="Channels['+_id+']" value="1" checked />' +
				'<span onclick="chnCheck(this);return false;" id="achn-'+_id+'">remove</span> <div '+_fn+'>' + _cht + '</div></a>';
		}
	}
	gEl('cp-channels-active').innerHTML = _ht;
}

var geoKeys = new Array('', 'country', 'state', 'city', 'zip');
var geoRess = new Array(); // existing results
var geoTActive = new Array(); // all active items in current search // коллекция всех найденых элементов с текущей строкой поиска - из них выборка
var geoActive = new Array(); // all selected items

var geoTExists = new Array(); // temporary array for get existing result if available

var geoA = false;
var geoAKey = '';
var geoAStr = '';
var geoMode = 0;
var geoBase = 0;

function geoInit()
{
	/* init checkboxes */
	var elms = document.getElementsByTagName('div');
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('geo-cb-') != -1)
		{
			elms[i].onclick = function() { 
				this.blur();
				geoCheck(this);
				return false;
			}
		}
	}
	gEl('input-search-country').onkeyup = function(e) { geoSearch(e, 'country', this); }
	gEl('input-search-state').onkeyup = function(e) { geoSearch(e, 'state', this); }
	gEl('input-search-city').onkeyup = function(e) { geoSearch(e, 'city', this); }
	gEl('input-search-zip').onkeyup = function(e) { geoSearch(e, 'zip', this); }
	
	// collect base ids
	var t = 0;
	var elms = document.getElementsByTagName('input');
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].getAttribute('geoinfo'))
		{
			var _t = elms[i].getAttribute('geoinfo').split('_');
			var _g = new geoItem(_t[1],_t[2],_t[0],1);
			var exK = _t[0] + '_' + _t[1];
			geoActive[exK] = _g;
			t++;
		}
	}
	if (t > 0)
		gEl('geo-cb-0').className = 'unchecked'
}




function geoSearch(e, key, elm)
{
	if (geoA) return;
	gEl('geo-search-stop').onclick=function() { 
		geoSearchCollect(); 
		this.style.display = 'none';
		return false; 
	}
	gEl('geo-search-stop').style.display = 'block';
	gEl('geo-search-stop').style.height = document.body.offsetHeight + 'px';
	
	if (elm.value.length < 1)
	{
//		geoSearchStop(key, elm);
		gEl('geo-search-' + key).innerHTML = '';
		gEl('geo-search-' + key).style.display = 'none';
		return;
	}
	
	geoAKey = key;
	geoAStr = elm.value;
	var str = geoAStr;
	
	e = (e) ? e : ((window.event) ? window.event : "");
	var k = e.which ? e.which : (e.keyCode ? e.keyCode : '');
	if (k == 37 || k == 38 || k == 39 || k == 40)
		return;
	else if (k == 27) // esc
	{
		geoSearchStop(key, elm);
		return;
	}
	else if (k == 13) // ent
	{
		geoSearchCollect(key, elm);
		return;
	}
	
	// now check for existing results
	if (geoIsExists(key,str))
		geoDisplay();
	else
	{
		geoA = true;
		var req = 'area=' + key + '&str=' + str + '&base=1';
		AjaxAfterLoadCall = 'geoSearchCB';
		// [show preloader]
		AjaxRequestPost('/ajax/geo/', req, 'null');
	}
}

function geoSearchCB()
{
	// [hide preloader]
	resToArray(AjaxRequestResult,geoAKey); // now all results in geoTActive 
//	alert(AjaxRequestResult)
//	alert(geoAKey + '|' + geoTActive.length);
	if (!geoTExists[geoAKey])
		geoTExists[geoAKey] = new Array();
	geoTExists[geoAKey][geoAStr] = geoTActive;
	
	geoDisplay();
	geoA = false;
}

function geoDisplay()
{
	var t = geoOutActive(geoTActive,geoAKey);
	var ht = '';
	
	gEl('geo-search-' + geoAKey).innerHTML = ht;
	gEl('geo-search-' + geoAKey).style.display = 'none';
		
	if (t.length > 0)
	{
		// 1 display active
		// hide already selected states 
		if (geoMode != 3 && geoMode != 31)
		{
			var gt = 0;
			for (var i in geoActive)
			{
				if (geoActive[i].key == geoAKey)
				{
					gt++;
					ht += geoHtRes(geoActive[i], 1);
				}
			}
			// 2 display separator
			if (gt > 0)
				ht += '<div class="sp"></div>';
		}
				
		for (var i = 0; i < t.length; i++)
			ht += geoHtRes(t[i]);
	}
	// 3 display results
	if (ht.length > 0)
	{
		gEl('geo-search-' + geoAKey).innerHTML = ht;
		gEl('geo-search-' + geoAKey).style.display = 'block';
	}
	
}

function geoHtRes(el, a, s)
{
	a = (a) ? 'class="active"' : '';
	s = (s) ? '<span>-</span>' : '';
	var ht = '<a href="" ' + a + ' onclick="geoElO(\''+el.key+'\','+el.id+',this);return false;">' + s + el.name + '</a>';
	return ht;
}
function geoHtResA(el)
{
	var ht = '<a href="" class="active" onclick="geoElO(\''+el.key+'\','+el.id+',this,1);return false;"><input type="checkbox" name="'+el.key+'['+el.id+']" value="1" checked /><span>-</span>' + el.name + '</a>';
	return ht;
}

function geoIsExists(key,str)
{
	if (!geoTExists[key])
	{
		geoTExists[key] = new Array();
		return false;
	}
	if (geoTExists[key][str])
	{
		geoTActive = geoTExists[key][str];
		return true;
	}
	// not found - check previous key, if found then check count, if = 0 then set same to 0 and return true
	var pStr = str.substring(0, str.length - 1);
	if (geoTExists[key][pStr] && geoTExists[key][pStr].length == 0)
	{
		geoTExists[key][str] = new Array();
		geoTActive = geoTExists[key][str];
		return true;
	}
	return false;
}


function geoSearchCollect(key, elm)
{
	key = (key) ? key : geoAKey;
	elm = (elm) ? elm : gEl('input-search-' + key);
	
	if (geoMode == 3)
	{
//		geoSearchStop(key, elm);
		gEl('geo-search-' + key).innerHTML = '';
		gEl('geo-search-' + key).style.display = 'none';
		return;
	}
	
	var ht = '';
	
	var elTo = gEl('geo-active-' + key);
	for (var i in geoActive)
	{
		if (geoActive[i].key == geoAKey)
			ht += geoHtResA(geoActive[i]);
	}
	
	if (ht.length > 0)
	{
		gEl('geo-active-' + geoAKey).innerHTML = ht;
//		gEl('geo-active-' + geoAKey).style.display = 'block';
	}
	
	// get all selected values after enter but before clear content
	geoSearchStop(key, elm);
}

function geoSearchStop(key, elm)
{
	key = (key) ? key : geoAKey;
	if (key && key != '')
	{
		gEl('geo-search-' + key).innerHTML = '';
		gEl('geo-search-' + key).style.display = 'none';
	}
	
	gEl('geo-active-citystate').innerHTML = '';
	gEl('geo-active-citystate').style.display = 'none';
	gEl('geo-active-state').style.display = 'block';
	
	geoCheckClean(1);
	
	geoA = false;
	geoAKey = '';
	geoMode = 0;
	geoAStr = '';
	geoTActive = new Array();
}

// geo checkboxes works 
function geoCheck(elm)
{
	if (geoA) return;
	
	if (elm.className == 'checked')
		return;
	
	var _id = elm.id.replace('geo-cb-','') * 1;
	
	geoCheckClean();
	
	if (_id == 0)
	{
		if (geoAKey != '')
			geoSearchStop();
		geoActive = new Array();
		for (var i = 1; i < 5;i++)
			gEl('geo-active-' + geoKeys[i]).innerHTML = '';
	}
	else
	{
		gEl('geo-cb-0').className = 'unchecked';
		for (var i = 1; i < 5;i++)
			geoHideInput(i);
		
		geoMode = _id;
		gEl('geo-cb-' + _id).className = 'checked';
		if (geoMode == 3)
		{
			geoSetInput((_id-1));
			gEl('geo-city-help').style.visibility = 'visible';
		}
		else
		{
			gEl('geo-city-help').style.visibility = 'hidden';
			geoSetInput(_id);
		}
	}
}

function geoSetInput(id)
{
	if (geoKeys[id] && geoKeys[id] != '')
	{
		gEl('geo-input-' + geoKeys[id]).style.visibility = 'visible';
		gEl('input-search-' + geoKeys[id]).focus();
	}
}
function geoHideInput(id)
{
	if (geoKeys[id] && geoKeys[id] != '')
	{
		gEl('geo-input-' + geoKeys[id]).value = '';
		gEl('geo-input-' + geoKeys[id]).style.visibility = 'hidden';
	}
}
function geoCheckClean(all)
{
	for (var i = 1; i < 5;i++)
	{
		gEl('geo-cb-' + i).className = 'unchecked';
		gEl('input-search-' + geoKeys[i]).value = '';
		gEl('geo-input-' + geoKeys[i]).style.visibility = 'hidden';
		gEl('geo-search-' + geoKeys[i]).innerHTML = '';
		gEl('geo-search-' + geoKeys[i]).style.display = 'none';
	}
	
	gEl('geo-cb-0').className = (all) ? 'unchecked' : 'checked';
}



// convert string result to array
function resToArray(res,key)
{
	geoTActive = new Array();
	var b = res.split('||');
	
	if (b.length > 0)
	{
		for (var i = 0; i < b.length; i++)
		{
			var c = b[i].split('|');
			if (c.length == 2)
				geoTActive[geoTActive.length] = new geoItem(c[0],c[1],key);
		}
	}
	return geoTActive;
}

// remove from results already activated elements - it is before display for client
function geoOutActive(res,key)
{
	var t = new Array();
	for (var i = 0; i < res.length; i++)
	{
		var exK = key + '_' + res[i].id;
		if (!geoActive[exK])
			t[t.length] = res[i];
	}
	return t;	
}

// add or remove active elements
function geoElO(key, id, el, rm)
{
	el.className = (el.className == '') ? 'active' : '';
	if (geoMode == 3)
	{
		gEl('geo-active-citystate').innerHTML = '<a>' + el.innerHTML + '</a>';
		gEl('geo-active-citystate').style.display = 'block';
		gEl('geo-active-state').style.display = 'none';
		
		gEl('geo-search-state').innerHTML = '';
		gEl('geo-search-state').style.display = 'none';
		
		gEl('input-search-state').value = '';
		gEl('geo-input-' + geoKeys[2]).style.visibility = 'hidden';
		gEl('geo-input-' + geoKeys[3]).style.visibility = 'visible';
		gEl('input-search-' + geoKeys[3]).focus();
		
		geoMode = 31;
		geoAKey = 'city';
		geoAStr = '';
		
//		geoA = true;
//		var req = 'area=city&str=&base=' + id;
//		AjaxAfterLoadCall = 'geoSearchCB';
		// [show preloader]
//		AjaxRequestPost('/ajax/geo/', req, 'null');
		return;
	}
	
	var exK = key + '_' + id;
	if (el.className == '') // remove active el
	{
		if (geoActive[exK])
		{
			var t = geoActive;
			geoActive = new Array();
			for (var i in t)
			{
				if (i != exK)
					geoActive[i] = t[i];
			}
		}
		if (rm)
		{
			el.innerHTML = '';
			el.style.display = 'none';
		}
	}
	else // add active el
	{
		for (var i = 0; i < geoTActive.length; i++)
		{
			if (geoTActive[i].id == id)
			{
				geoActive[exK] = geoTActive[i];
				break;
			}
		}
	}
}

function geoItem(id, name, key, act)
{
	this.id = id;
	this.name = name;
	this.key = key;
	this.active = (act) ? act : 0;
	return this;
}

/* */
function cpLocateAd(uri, AdId)
{
	var a = (AdId && AdId > 0) ? '?AdId=' + AdId : '';
	document.location.href=uri + a;
}

/* buy */
function buyInit()
{
	gEl('input-quanity').onkeyup = function() { buyTotal(); }
	gEl('input-quanity').onblur = function() { buyTotal(); }

}

function buyTotal() 
{
	var qty = gEl('input-quanity').value;
	qty = qty.replace(/\,/, '.') * 1;
	var cpt = gEl('input-cpt').value * 1;
	
	var num = qty * cpt;
	if (!isNaN(num))
	{
		gEl('total-cost').innerHTML = '$' + addCommas(num.toFixed(0));
		if (gEl('total-cost-charge'))
			gEl('total-cost-charge').innerHTML = '$' + (num / 10).toFixed(0);
	}
	else
	{
		gEl('total-cost').innerHTML = '$0';
		if (gEl('total-cost-charge'))
			gEl('total-cost-charge').innerHTML = '$0';
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	return x1 + x2;
}

/* regstration */
function formDef(el)
{
	if (el.getAttribute('defvalue'))
	{
		if (el.value == el.getAttribute('defvalue'))
		{
			el.value = '';
			if (el.getAttribute('defcolor'))
				el.style.color = '#' + el.getAttribute('defcolor');
		}
	}
}
function cardInit()
{
	var elms = document.getElementsByTagName('div');
	var def = gEl('CCTypeId').value;
//	def = (def == '') ? '1' : def;
	var keys= new Array();
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('c-card') != -1)
		{
			var t = elms[i].id.replace('c-card','');
			elms[i].className = (t == def) ? 'card active' : 'card';
			elms[i].onclick = function() { cardSet(this); };
		}
	}
}
function cardSet(el)
{
	var def = el.id.replace('c-card','');
	var elms = document.getElementsByTagName('div');
	var keys= new Array();
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('c-card') != -1)
		{
			var t = elms[i].id.replace('c-card','');
			elms[i].className = (t == def) ? 'card active' : 'card';
		}
	}
	gEl('CCTypeId').value = def;
}
function billSet()
{
	gEl('billing-area').style.display = 'block';
	gEl('billingDisplay').value = '1';
}




/* process page */
var processT = new Array();
processT[1] = 'Easiest <span class="underline">Most</span> Detailed Ad Buy on the web';
processT[2] = 'Create your ad and upload your presentation';
processT[3] = 'Select your audience behavior preferences';
processT[4] = 'Choose your audience and location preferences';
processT[5] = 'Buy some ads and start selling <span class="underline">your</span> audience';
processT[6] = 'Unmatched audience, campaign and ratings analysis ';
processT[7] = 'Hone your presentation for unmatched results';

var processAuto = false;
var processAutoTime = 5000;

var processId = 1;
var processSrc = '';
var pPreloadActive = false;
var processFade = 1;
var processTm = null;

function processAutoSet()
{
	if (!gEl('process-auto-nav'))
		return;
	processAuto = (processAuto) ? false : true;
	processTm = setTimeout("processNext()", 1000);
	gEl('process-auto-nav').innerHTML = (processAuto) ? 'ON' : 'OFF';
	
}
function processNext(id, force)
{
	if (pPreloadActive || (!processAuto && !force))
		return;
	
	if (processTm)
		clearTimeout(processTm);
	
	if (id)
		processId = id;
	else
		processId = (++processId > 7) ? 1 : processId ;
	for (var i = 1; i < 8; i++)
	{
		if (gEl('process-n' + i))
			gEl('process-n' + i).className = '';
	}
	processSrc = '/images/process_au'+processId+'.png';
	processPreload();
	if (gEl('show-it-title'))
		gEl('show-it-title').innerHTML = processT[processId];
	if (gEl('process-n' + processId))
		gEl('process-n' + processId).className = 'active';
}


function processPreload() {

	pPreloadActive = true;
	var imgPreload = new Image();
	imgPreload.onload = function() {
		processOut(); //
	}
	imgPreload.src = processSrc;
}


function processOut()
{
	if (!gEl('process-show'))
		return;
	processFade -= 0.1;
	if (processFade < 0)
	{
		gEl('process-show').src = processSrc;
		setTimeout("processIn()", 50);
		return;
	}
	if (browserIsIE) 
		gEl('process-show').style.filter = "alpha(opacity=" + (processFade * 100) + ")";
	else
		gEl('process-show').style.opacity = processFade;
	setTimeout("processOut()", 50);
}

function processIn()
{
	if (!gEl('process-show'))
		return;
	processFade += 0.1;
	if (processFade > 1)
	{
		pPreloadActive = false;
		if (processAuto)
			processTm = setTimeout("processNext()", processAutoTime);
		return;
	}
	if (browserIsIE) 
		gEl('process-show').style.filter = "alpha(opacity=" + (processFade * 100) + ")";
	else
	{
		gEl('process-show').style.opacity = processFade;
	}
	setTimeout("processIn()", 50);
}

/* snapshot graphs */
function pseudoCheck(id, isSnapshot)
{
	if (IsPrint) return;
	var doCh = (gEl(id).className == 'legend-checkbox-off') ? '0' : '1';
	doCh = (doCh == '1') ? '0' : '1';
	gEl(id).setAttribute('pseudoChecked', doCh);
	gEl(id).className = (doCh == '1') ? 'legend-checkbox-on' : 'legend-checkbox-off';
	if (gEl('input-' + id))
		gEl('input-' + id).checked = (gEl(id).className == 'legend-checkbox-on');
	gEl(id).blur();
	
	if (isSnapshot)
		snapRefresh();
}

var snapRefreshing = false;
function snapRefresh()
{
	if (IsPrint) return;
	if (snapRefreshing)
		return;
	snapRefreshing = true;
	
	var _p = 'AdId=' + gEl('hfAdId').value;
	_p += (gEl('input-lcb-ad-displays').checked) ? '&Displays=1' : '';
	_p += (gEl('input-lcb-present').checked) ? '&Presentations=1' : '';
	_p += (gEl('input-lcb-free-present').checked) ? '&FreePresentations=1' : '';
	_p += (gEl('input-lcb-ratings').checked) ? '&Ratings=1' : '';
	_p += (gEl('input-lcb-clicks').checked) ? '&Clicks=1' : '';
	
	
	// can be replaced with ajax
	gEl('snapshotForm').submit();
}


/* landing flash */
var ldFlashAuto = true;
var ldFlashAutoTime = 5000;
var ldFlashTm = null;
var ldFlashId = 1;
var ldFlashSrc = '';
var ldFlashPActive = false;
var ldFlashFade = 1;

function ldFlashStart() 
{ 
	ldFlashPActive = false;
	ldFlashId = 1;
	gEl('landing-flash-back').onclick = function() { ldFlashPrew(); }
	gEl('landing-flash-forward').onclick = function() { ldFlashNext(); }
	ldFlashTm = setTimeout("ldFlashNext()", ldFlashAutoTime);
}
function ldFlashPrew() 
{ 
	if (ldFlashPActive)
		return;
	var ld = ldFlashId - 1; 
	ld = (ld < 1) ? 1 : ld; 
	if (ldFlashTm)
		clearTimeout(ldFlashTm);
	ldFlashNext(ld); 
}
function ldFlashNext(id)
{
//	alert(ldFlashPActive + '|' + ldFlashId + '==' + id)
	if (ldFlashPActive)
		return;
	
	if (ldFlashTm)
		clearTimeout(ldFlashTm);
	
	if (id)
		ldFlashId = id;
	else
	{
		ldFlashId++;
		if (ldFlashId > 8)
		{
			if (gEl('landing-flash-back'))
				hide('landing-flash-back');
			if (gEl('landing-flash-forward'))
				hide('landing-flash-forward');
			return;
		}
	}
	ldFlashSrc = '/images/land_flash'+ldFlashId+'.jpg';
	ldFlashPreload();
}

function ldFlashPreload() 
{

	ldFlashPActive = true;
	var imgPreload = new Image();
	
	imgPreload.onload = function() {
		ldFlashOut(); //
	}
	imgPreload.src = ldFlashSrc;
}


function ldFlashOut()
{
	if (!gEl('landing-show'))
		return;
	ldFlashFade -= 0.1;
	if (ldFlashFade < 0)
	{
		gEl('landing-show').src = ldFlashSrc;
		setTimeout("ldFlashIn()", 150);
		return;
	}
	if (browserIsIE) 
		gEl('landing-show').style.filter = "alpha(opacity=" + (ldFlashFade * 100) + ")";
	else
		gEl('landing-show').style.opacity = ldFlashFade;
	setTimeout("ldFlashOut()", 50);
}

function ldFlashIn()
{
	if (!gEl('landing-show'))
		return;
	ldFlashFade += 0.1;
	if (ldFlashFade > 1)
	{
		ldFlashPActive = false;
		if (ldFlashAuto)
			ldFlashTm = setTimeout("ldFlashNext()", ldFlashAutoTime);
		return;
	}
	if (browserIsIE) 
		gEl('landing-show').style.filter = "alpha(opacity=" + (ldFlashFade * 100) + ")";
	else
	{
		gEl('landing-show').style.opacity = ldFlashFade;
	}
	setTimeout("ldFlashIn()", 50);
}


/* true cost calc */
function trueCostCalcInit()
{
	gEl('calc-cpr').onkeyup = function(e) { trueCostCalc(); }
	gEl('calc-rate').onkeyup = function(e) { trueCostCalc(); }
	gEl('calc-pp').onkeyup = function(e) { trueCostCalc(); }

	trueCostCalc();
}

function trueCostCalc()
{
	var cpr = gEl('calc-cpr').value;
	var rate = gEl('calc-rate').value;
	var pp = gEl('calc-pp').value;
	
	var crate = (rate != 0) ? (1 / rate * 100) : 0;
	var value = pp - (crate*cpr);
	var cV = value + '';
	value = (cV == 'NaN' || cV == '-Infinity') ? 0.00 : value;
	
	if (value >= 0)
	{
		gEl('loss-gain').className = 'gain';
		gEl('loss-gain').innerHTML = 'gain';
	}
	else
	{
		gEl('loss-gain').className = 'loss';
		gEl('loss-gain').innerHTML = 'loss';
	}
	var tV = value;
	value = addCommas(value.toFixed(2));
	
	gEl('calc-res-show').innerHTML = (tV < 0) ? '$' + value.replace('-','') : '$' + value;
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


/* login box */
var LoginErrorMessage = '';
function showLoginBox()
{
	if (LoginErrorMessage.length > 0)
	{
		alert(LoginErrorMessage);
		LoginErrorMessage = '';
	}
	gEl('overlay4').style.height = document.body.offsetHeight + 'px';
	show('overlay4');
	show('loginparent');
	if (gEl('emailLogin'))
		gEl('emailLogin').focus();
	return false;
}

function hideLoginBox()
{
	hide('loginparent');
	hide('overlay4');
	gEl('a-signin').className = '';
	return false;
}
function getPassword()
{
	var l = gEl('emailLogin').value;
	var q = gEl('LoginSecurityQuestion').value;
	var a = gEl('answerLogin').value;
	
	var req = 'l=' + l + '&q=' + q + '&a=' + a;
	gEl('displaypass').innerHTML = '<div style="height: 19px; background: url(/images/loading2.gif) top left no-repeat"></div>';
	AjaxAfterLoadCall = 'getPasswordRs';
	AjaxRequestPost('/ajax/restore_password/', req, 'null');
}

function getPasswordRs()
{
	if (AjaxRequestResult != '' && AjaxRequestResult != 'Try Again')
	{
		gEl('displaypass').innerHTML = AjaxRequestResult;
		gEl('password2').value = AjaxRequestResult;
	}
	else
		gEl('displaypass').innerHTML = 'Try Again';
}

function show(element) { gEl(element).style.display = 'block'; }
function hide(element) { gEl(element).style.display = 'none'; }


var dcTitle = '';
var dcActive = '';
var dcId = -1;
var dcLoading = 0;

/* guest nav */
function setTopNav()
{
	var elms = document.getElementsByTagName('a');
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('tn') != -1 && elms[i].href)
			elms[i].onclick = function() { return setDCnt(this); }
	}
}
function setDCnt(elT)
{
	elT.blur();
	if (dcActive == elT.href || dcLoading)
		return false;
	if (!gEl('dina-content'))
		return true;
	
	var elms = document.getElementsByTagName('a');
	for (var i = 0; i < elms.length; i++)
	{
		if (elms[i].id && elms[i].id.indexOf('tn') != -1 && elms[i].href)
			elms[i].className = '';
	}
	dcId = elT.id.replace('tn','') * 1;
	
	elT.className = 'active';
	
	dcActive = elT.href;
	if (elT.getAttribute('title'))
		document.title = defTile + ' - ' + elT.getAttribute('title');
	else
		document.title = defTile;
	
	var action = elT.href;
	var request = 'IsAjax=1';
	
	AjaxAfterLoadCall = 'setDCntCb';
	AjaxRequestPost(action, request, 'dina-content');
	
	return false;
}


function setDCntCb()
{
	setupZoom();
	// about
	document.body.className = (dcId == 1) ? 'free-alt' : '';
	// landing
	if (dcId == 0)
	{
		var embedTC = AC_FL_RunContentIn(
		'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
		'width', '970',
		'height', '400',
		'src', '/media/10cents',
		'movie', '/media/10cents',
		'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
		'allowScriptAccess', 'sameDomain',
		'allowFullScreen', 'false',
		'wmode', 'transparent',
		'quality', 'high',
		'scale', 'noScale',
		'id', '10cents',
		'bgcolor', '#ffffff',
		'name', '10cents',
		'menu', 'false',
		'align', 'middle',
		'salign','TL'
	); //end AC code
	  gEl('landing-flash-content').innerHTML = embedTC;
		
		//ldFlashStart();
	}
	// true_cost
	else if (dcId == 2)
	{
		trueCostCalcInit();
	}
	// the_process
	else if (dcId == 3)
	{
		processAuto = false;
		pPreloadActive = false;
		processId = 1;
		setTimeout("processAutoSet()", 2000);
	}
	// register
	else if (dcId == 4)
	{
		LDScript('/javascript/validate.js');
		cardInit();
		fixIeGpSet();
	}
}

function LDScript(sr)
{
	var _h = document.getElementsByTagName("head")[0];
	var _e = document.createElement("script");
	_e.charset="UTF-8";
	_e.src=sr;
	_h.appendChild(_e);

}

function fixIeGpSet()
{
	if (!browserIsIE)
		return;
	window.onscroll = function() { fixIeGp() }
	
}
function fixIeGp()
{
	if (!gEl('registration'))
	{
		window.onscroll = function() { return true; }
		return;
	}
	var _b = (gEl('form-border1').style.borderLeft == '#ececec 1px solid') ? '#ededed 1px solid' : '#ececec 1px solid';
	gEl('form-border1').style.borderLeft = _b;
	gEl('form-border2').style.borderLeft = _b;
	gEl('form-border3').style.borderLeft = _b;
	gEl('form-border4').style.borderLeft = _b;
	gEl('form-border1').style.borderRight = _b;
	gEl('form-border2').style.borderRight = _b;
	gEl('form-border3').style.borderRight = _b;
	gEl('form-border4').style.borderRight = _b;
}

var sc_Int = null;
var sc_Sp = 10;
var sc_Top = 0;
var sc_M = 0;
function sc_Gy(e)
{
	var y = e.offsetTop
	if (e.offsetParent) while (e = e.offsetParent) y += e.offsetTop
	return y
}
function sc_Gs()
{
	body=document.body
	var d = document.documentElement
	if (body && body.scrollTop) return body.scrollTop
	if (d && d.scrollTop) return d.scrollTop
	if (window.pageYOffset) return window.pageYOffset
	return 0;
}
function sc_End(e)
{
	if (window.event) 
	{
		window.event.cancelBubble = true
		window.event.returnValue = false
      		return;
    	}
	if (e.preventDefault && e.stopPropagation) 
	{
		e.preventDefault()
		e.stopPropagation()
	}
}

function sc_Run(d)
{
	var i = window.innerHeight || document.documentElement.clientHeight;
	var h = document.body.scrollHeight;
	var a = sc_Gs();
//	alert(d + '|' + a + '|' + h)
	if(d>a)
		if(h-d>i)
			a+=Math.ceil((d-a)/sc_Sp)
		else
			a+=Math.ceil((d-a-(h-d))/sc_Sp)
	else
		a = a+(d-a)/sc_Sp;
	window.scrollTo(0,a)
	if(a==d || sc_Top == a)
	{
		clearInterval(sc_Int)
		sc_Mapped();
	}
	sc_Top = a
}

function sc_Mapped(h)
{
	if (h)
	{
		if (h.indexOf('?map=6') != -1 && gEl('process-auto-nav'))
		{
			processAuto = false;
			gEl('process-auto-nav').innerHTML = 'OFF';
			if (processId != 2)
				processNext(2,1);
		}
		else if (h.indexOf('?map=9') != -1 && gEl('process-auto-nav'))
		{
			processAuto = false;
			gEl('process-auto-nav').innerHTML = 'OFF';
			if (processId != 5)
				processNext(5,1);
		}
		else if (h.indexOf('?map=7') != -1 && gEl('rzoom7'))
			sc_M = 7;
		else if (h.indexOf('?map=8') != -1 && gEl('rzoom8'))
			sc_M = 8;
	}
	else if (sc_M > 0)
	{
		if (sc_M == 7 || sc_M == 8 || sc_M == 9)
		{
			zoomPreload(gEl('rzoom' + sc_M));
			zoomClick(gEl('rzoom' + sc_M), null, true);
		}
		sc_M = 0;
	}
}
function sc_Init()
{
	var a = document.getElementsByTagName('a');
	sc_End(this);
	window.onscroll
	for (i=0;i<a.length;i++) 
	{
		var l = a[i];
		if(l.href && l.href.indexOf('#') != -1 && ((l.pathname == location.pathname) || ('/'+l.pathname == location.pathname)) )
		{
			a[i].onclick = function()
			{
				sc_End(this);
				var ll = this.hash.substr(1);
				var al = document.getElementsByTagName('a');
				for (var il=0;il<al.length;il++) 
				{
				     	if(al[il].name == ll)
				     	{
				     		clearInterval(sc_Int);
				     		sc_Mapped(this.href);
				     		sc_Int = setInterval('sc_Run('+sc_Gy(al[il])+')',10);
				     		return false;
					}
				}
			}
		}
	}
}

addEvent(window, 'load', fixIeGpSet);
addEvent(window, 'load', sc_Init);