
/*


	.::.  Epicenter JavaScript/Ajax Library   .::.
   	 Copyright (c) 2007 James Flanders Roberts IV

      This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

      This library is available in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

      You should have received a copy of the GNU General Public
   License along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.





*/



//    .::.  Browser check  .::..................................

IS_DOM = (document.getElementById) ? true : false;
IS_IE = (document.all) ? true : false;
IS_IE50 = (navigator.userAgent.indexOf("IE 5.0") != -1);
IS_IE6 = (navigator.userAgent.indexOf("IE 6") != -1);
IS_Mac = (navigator.appVersion.indexOf("Mac") != -1);
IS_IE5Mac = IS_IE && IS_Mac && IS_DOM;
var browserName = navigator.appName;
var lastHash = '';


function getThis(sId){

		var oObject;
		oObject = false;

		if (IS_DOM) {
			if (document.getElementById(sId)) {
				oObject = document.getElementById(sId);
			}
		}
		else if (IS_IE)
		{
			if (document.all[sID]) {
				oObject = document.all[sID];
			}
		}
		return oObject;
};



//    .::.  start content functions  .::................................



function getXHR(t, url, vars, e, m, h, load, nosync){

var syncparam = 'true';

if (nosync && nosync == true)
{
	syncparam = 'false';
}

if(!m) m='get';

if ($(t))
{
	if(!load){
//	$(t).innerHTML = '<div class="ac"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="38" height="50"><param name="movie" value="images/loading.swf" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><embed src="images/loading.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="38" height="50" wmode="transparent"></embed></object></div>';
	$(t).innerHTML = '<div class="ac s10 black">loading<br><img src="images/load/load_squares-24.gif"></div>';

	}
}

var l = $('loading');
//l.style.display='block';

if(typeof(vars) == 'object'){
	var params = vars;
	var vars = null;
}else{
	var url=url+'?'+vars;
	var params = null;
}

new Ajax.Updater(
	t,
	url,
	{
		onComplete:function(){
			titleTxt = vars.replace('act=', '');
			titleTxt = titleTxt.replace('.', '-');
			titleTxt = titleTxt.replace(/&.*?=(.*?)/g, '-$1');

			//l.style.display='none';
			if(e != '0'){
				var run = "new Effect."+e+"('"+t+"');";
				eval(run);
				//alert(run);
			}else{
				$(t).style.display='block';
			}
			if(h) document.title = "DM2 - "+titleTxt;

		},
		onFailure:function(err){
			alert('Error ' + err.status + ' -- ' + err.statusText);
		},
		asynchronous:syncparam,
		evalScripts:true,
		parameters: params,
		method:m
	}
);

};



//------------ write flash for IE
function flashWrite(flashVars, src, width, height, wmode, flash_t, text){

$(flash_t).innerHTML = '<div class="ac"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+width+'" height="'+height+'"><param name="movie" value="'+src+'" /><param name="quality" value="high" /><param name="wmode" value="'+wmode+'" /><param name="FlashVars" value="'+flashVars+'" /><embed src="'+src+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" wmode="'+wmode+'" FlashVars="'+flashVars+'"></embed></object>'+text+'</div>';

};


var loadedobjects="";

function loadobjs(){
if (!document.getElementById) return
for (i=0; i<arguments.length; i++){
	var file=arguments[i];
	var fileref="";
	if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
		if (file.indexOf(".js")!=-1){ //If object is a js file
			fileref=document.createElement('script');
			fileref.setAttribute("type","text/javascript");
			fileref.setAttribute("src", file);
		}
		else if (file.indexOf(".css")!=-1){ //If object is a css file
			fileref=document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", file);
		}
	}
	if (fileref!=""){
		document.getElementsByTagName("head").item(0).appendChild(fileref);
//		loadedobjects+=file+" "; //Remember this object as being already added to page
	}
}
}




//    .::.  end content functions  .::...............................







//      .::.  start menu functions  .::..................................


var currentMenu;
var defaultMenu;
var defaultSubMenu;
var timeout;
var content;

var publicPages = ['content', 'login'];
//var publicSubPages = ['faq'];
//publicSubPages['faq'] = ['basics', 'manager', 'monetize', 'money'];

//      .::.  public menu  .::..................................
function goPublic(page, subpage){
	for(i=0;i<publicPages.length;i++){
		getThis('page-'+publicPages[i]).style.display='none';
	}

	getThis('page-'+page).style.display='block';

	if(subpage){
		for(i=0;i<publicSubPages[page].length;i++){
			getThis('subpage-'+page+'-'+publicSubPages[page][i]).style.display='none';
			getThis('sublink-'+page+'-'+publicSubPages[page][i]).className='submenuOFF';
		}

		getThis('subpage-'+page+'-'+subpage).style.display='block';
		getThis('sublink-'+page+'-'+subpage).className='submenuON';

	}


	getThis('menucell-'+defaultMenu).className='menuOFF';
	getThis('menuimg-'+defaultMenu).src='images/nav/'+defaultMenu+'-Off.gif';
	if(sm=getThis('sm-'+defaultMenu)) sm.style.display="none";

	currentMenu = page;
	defaultMenu = page;

	getThis('menuimg-'+page).src='images/nav/'+page+'-On.gif';
	if(sm=getThis('sm-'+page)){
		sm.style.display="block";
		getThis('menucell-'+page).className='menuON';
	}


}


//      .::.  initialize menu  .::..................................
function init(menuitem, submenuitem, target){

	currentMenu = menuitem;
	defaultMenu = menuitem;
	defaultSubMenu = submenuitem;
	getThis('menuimg-'+menuitem).src='images/nav/'+menuitem+'-On.gif';
	if(sm=getThis('sm-'+menuitem)){
		sm.style.display="block";
		getThis('menucell-'+menuitem).className='menuON';
	}
	if(submenuitem) getThis('sublink-'+menuitem+'-'+submenuitem).className='submenuON';


	goContent(menuitem, submenuitem, target);


	goHistory(target, menuitem, submenuitem);


};

//      .::.  go to page  .::..................................

function goTo(menuitem, submenuitem, target, go){

	currentMenu = menuitem;
	defaultMenu = menuitem;
	defaultSubMenu = submenuitem;
	getThis('menuimg-'+menuitem).src='images/nav/'+menuitem+'-On.gif';
	if(sm=getThis('sm-'+menuitem)){
		sm.style.display="block";
		getThis('menucell-'+menuitem).className='menuON';
	}
	if(submenuitem) getThis('sublink-'+menuitem+'-'+submenuitem).className='submenuON';


	if(go == 1) goContent(menuitem, submenuitem, target);


};


//      .::.  button clicked  .::..................................

function goMenu(menuitem, submenuitem, target, go){

//	if(defaultMenu!=menuitem || defaultSubMenu!=submenuitem ){

		if(go != 0){
			go = 1;
			goHistory(target, menuitem, submenuitem);
		}

		getThis('menucell-'+defaultMenu).className='menuOFF';
		getThis('menuimg-'+defaultMenu).src='images/nav/'+defaultMenu+'-Off.gif';
		if(sm=getThis('sm-'+defaultMenu)) sm.style.display="none";

		if(defaultSubMenu) getThis('sublink-'+defaultMenu+'-'+defaultSubMenu).className='submenuOFF';

		goTo(menuitem, submenuitem, target, go);




//	}

};


function goHistory(target, menuitem, submenuitem, extra_vars, histparent){

	var hKey = menuitem;
	if(submenuitem) hKey += '-'+submenuitem;
	if(extra_vars){
//		extra_vars = '&'+extra_vars;
		var extraVars = extra_vars.replace(/&.*?=(.*?)/g, '-$1');
//		extraVars = extraVars.replace(/&/g, '-');
		hKey += extraVars;
	}
	else vars='';
	if (!histparent)
	{
		histparent = 0;

		if (target != 'dm-content' && lasthash != '')
		{
			histparent = lasthash;
		}
	}
	var hData = [
		hKey,
		{
			't':target,
			'loc':menuitem,
			'subloc':submenuitem,
			'vars':extra_vars,
			'parent':histparent
		}
	];
//	alert(hData[1]);
	lasthash = hKey;
	dhtmlHistory.add(hData[0],hData[1]);

	return false;

}

function historyChange(newLocation, hData, nosync) {
//	alert(nosync);
	if(initialized == true){
		var url = 'index.php';
		if(hData){
			var loc = hData['loc'];
			var subloc = hData['subloc'];
			var targ = hData['t'];
			if(hData['vars']){
				var extraVars = hData['vars'];
			}
		}else{
			var loc = 'home';
			var subloc = 'home';
			var targ = 'dm-content';
		}
//		if (targ == "paged-domain-stats") targ = 'dm-content';

		if(targ != 'dm-content' && extraVars){
			var tmp_subloc = subloc.split('-');
			var tmp_vars = '&act='+loc+'.'+tmp_subloc[0]+'&from_history=1&' + extraVars;
		//	tmp_vars = extraVars;
//			alert(targ);
			if(defaultMenu != loc || defaultSubMenu != tmp_subloc[0]) getXHR(targ, url, tmp_vars, '0', 'GET', 1, nosync);
			goMenu(loc, tmp_subloc[0], targ, 0);
		}else{
			goMenu(loc, subloc, targ, 0);
		}
		var vars = '&act='+loc+'.'+subloc;

		if(extraVars) vars += '&'+extraVars;
//		vars += '&from_history=1';
		getXHR(targ, url, vars, '0', 'GET', 1, 1, nosync);
	}
}


//      .::.  get content  .::..................................

function goContent(loc, subloc, t){

	var url = 'index.php';
	var vars = 'act='+loc+'.'+subloc;
	getXHR(t, url, vars, '0', 'get', '1');

};


//      .::.  button mouseover  .::..................................

function showMenu(mID){

	if(currentMenu == mID){

		clearTimeout(timeout);

	}else{

		clearTimeout(timeout);
		hide(currentMenu);



		if(sm=getThis('sm-'+defaultMenu)) sm.style.display="none";
		getThis('menucell-'+defaultMenu).className='menuOFF';
		getThis('menucell-'+mID).className='menuON';
		getThis('menuimg-'+mID).src='images/nav/'+mID+'-Over.gif';
		getThis('sm-'+mID).style.display="block";
		currentMenu = mID;
	}
};


//      .::.  button mouseout  .::..................................

function hideMenu(mID){

//		if(getThis('sm-'+mID)) hide(mID);
		if(getThis('sm-'+mID)) timeout = setTimeout("hide('"+mID+"')",100);

};



//      .::.  menus back to default  .::..................................

function hide(mID){

	getThis('menucell-'+mID).className='menuOFF';
	getThis('menuimg-'+mID).src='images/nav/'+mID+'-Off.gif';
	if(sm=getThis('sm-'+mID)) sm.style.display="none";

	if(sm=getThis('sm-'+defaultMenu)) getThis('menucell-'+defaultMenu).className='menuON';
	getThis('menuimg-'+defaultMenu).src='images/nav/'+defaultMenu+'-On.gif';
	if(sm=getThis('sm-'+defaultMenu)) sm.style.display="block";
	currentMenu = defaultMenu;
};


//    .::.  end menu functions  .::...............................








//    .::.  BEGIN misc content retrieval functions  .::...............................

function sendForm(loc, subloc, f, t, h){

	var form = $(f);
	var url='index.php';
	var vars='act='+loc+'.'+subloc;
	var extra_vars=null;

	for(var i=0;i<form.length;i++){
		if(form[i].name != 'content_area'){
			vars += "&"+Form.Element.serialize(form[i]);
		}else{
			extra_vars=form.serialize(true);
		}
	}

	if(h) goHistory(t, loc, subloc, vars);

	url += '?'+vars;

	getXHR(t, url, extra_vars, '0', 'post', h, 1);

	return false;

};

 function link(loc, subloc){
	goMenu(loc, subloc, 'dm-content');
 };

  function notify(vars){
  	var vp_so = document.viewport.getScrollOffsets();
	$('notify').style.top = (vp_so[1]+10)+'px';
 	var url='index.php';
 	getXHR('notify-content', url, vars, 'Appear', 'get');
 	new Effect.Appear('notify');
 	window.setTimeout("new Effect.Fade(\'notify\')", 3000);
 };

function notifyStatic(msg){
  	var vp_so = document.viewport.getScrollOffsets();
	$('notify').style.top = (vp_so[1]+10)+'px';
	$('notify-content').innerHTML=msg;
	new Effect.Appear('notify');
	window.setTimeout("new Effect.Fade(\'notify\')", 5000);
};

function getHelp(loc, page, el){

	var element = $('help-'+el);
	var box = $('helper');
    var options = {
      margin: "0px",
	  padding: "5px",
	  backgroundColor: "#d6d6fc",
	  delta_x: 10,
	  delta_y: -5,
      zindex: 1000
    };
	
	// get Element offset
	var el_xy = element.cumulativeOffset();
	var window_dim = document.viewport.getDimensions();	
	
	// decide if wee need to switch sides for the hoverBox
	var dimensions = Element.getDimensions( box );
	var element_width = dimensions.width;
	var element_height = dimensions.height;
	
	if ( (element_width + el_xy[0]) >= ( window_dim[0] - options.delta_x) ){ // too big for X
		_x = (el_xy[0]+4) - element_width;
		// apply delta to make sure that the mouse is not on the tool-tip
		_x = (el_xy[0]+4) - options.delta_x;
	} else {
		_x = (el_xy[0]+4) + options.delta_x;
	}
	
	if ( (element_height + el_xy[1]) >= ( window_dim[1] - options.delta_y) ){ // too big for Y
		_y = (el_xy[1]+10) - element_height;
	    // apply delta to make sure that the mouse is not on the tool-tip
		_y = (el_xy[1]+10) - options.delta_y;
	} else {
		_y = (el_xy[1]+10) + options.delta_y;
	} 
	

	// now set the right styles
	box.style.left = _x+'px';
	box.style.top = _y+'px';
	

	var url = 'index.php';
	var vars = 'act=help.'+loc+'-'+page+'&helper_id='+el;
	getXHR('helper-content', url, vars, '0', 'get');
	
	// finally show the hoverBox
	new Effect.Appear(box, {duration:0.2});

};

function getGraph(target, type, height, width, color){
	new Effect.Appear(target);
	$(target+'-content').innerHTML='Rendering Graph....';
	var url = 'index.php';
	var vars = 'act=graphs.'+type+'&height='+height+'&width='+width+'&color='+color;
	getXHR(target+'-content', url, vars, '0', 'get');
};

function goPage(target, loc, subloc, extra_vars){
	goHistory(target, loc, subloc, extra_vars);
	var url = 'index.php';
	var vars = 'act='+loc+'.'+subloc+'&'+extra_vars;
	getXHR(target, url, vars, '0', 'get', 1);
}


function showBox(subloc, domain, func){

	var url = 'index.php';
	var vars = 'act=manage.'+subloc+'&func='+func+'&domain='+domain;
	getXHR('box-content', url, vars, '0', 'get');
	new Effect.Appear('box');
};




//    .::.  END misc content retrieval functions  .::...............................




//    .::.  BEGIN misc functions  .::...............................



function toggleDisplay(sDivID,sNum,pFix){

	clearLayerDisplay(sNum,pFix);

	var targetLayer = getThis(pFix+sDivID);

	if(targetLayer.style.display == 'none'){
		targetLayer.style.display = 'block';
	}else{
		targetLayer.style.display = 'none';
	}

};

function clearLayerDisplay(sNum,pFix){

	for(i=1; i<=sNum; i++)
	{

		var targetLayer = getThis(pFix+i);
		targetLayer.style.display = "none";

	}

};

function toggleImages(sDivID, sNum, pFix, iOff, iOn){

	for(i=1; i<=sNum; i++)
	{

		var t = getThis(pFix+i);
		t.src = iOff;

	}

	var tLyr = getThis(pFix+sDivID);

	tLyr.src=iOn;

};

function toggleImg(t, i1src, i2src){
	if(typeof(t)!='object') var tImg = getThis(t);
	else var tImg = t;
	var iSrc = tImg.src;
	if(iSrc.indexOf(i1src) != -1){
		tImg.src = i2src;
	}else{
		tImg.src = i1src;
	}

};

function toggleLayer(sDivID){

	var ndTargetLayer = getThis(sDivID);

	if(ndTargetLayer.style.display == 'none'){
		ndTargetLayer.style.display = 'block';
	}else{
		ndTargetLayer.style.display = 'none';
	}

};

function toggleRow(sDivID){

	var ndTargetLayer = getThis(sDivID+'-row');

	if(ndTargetLayer.style.display == 'none'){
		if(IS_IE) ndTargetLayer.style.display = 'block';
		else ndTargetLayer.style.display = 'table-row';
		Effect.SlideDown(sDivID);
	}else{
		Effect.SlideUp(sDivID);
		Effect.SlideUp(sDivID+'-row');

	}

};


function toggleValue(sDivID, val1, val2){

	var tField = getThis(sDivID);
	if(tField.value == val1){
		tField.value=val2;
	}else{
		tField.value=val1;
	}

};

function changeClass(t, needle, string){
	if(typeof(t)!='object') t = getThis(t);
	t.className = t.className.replace(needle, string);
};

function changeClassSet(t, sNum, pFix, on, off){

	for(i=1; i<=sNum; i++)
	{
		var targ = getThis(pFix+i);
		targ.className = targ.className.replace(on, off);
	}

	t = getThis(pFix+t)
	t.className = t.className.replace(off, on);
};

function toggleClass(t, s1, s2){
	if(typeof(t)!='object') t = getThis(t);
	if(t.className.indexOf(s1)) t.className = t.className.replace(s1, s2);
	else t.className = t.className.replace(s2, s1);
};


function swapImg(lyrName, iSrc)
{
	var tLyr = getThis(lyrName);
	tLyr.src = iSrc;
};



function isNumeric(sText)
{
   var ValidChars = '0123456789';
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
	  {
	  Char = sText.charAt(i);
	  if (ValidChars.indexOf(Char) == -1)
		 {
		 IsNumber = false;
		 }
	  }
   return IsNumber;

};

function isValidEmail(text)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(text)) return true;
	else return false;
};

// auto tab restricted length fields
function autoTab(form, from, to, length) {
	var fromField = from
	var toField = to
	var fieldLength = eval('document.' + form + '.' + fromField + '.value.length');
	if (fieldLength == length) {
		eval('document.' + form + '.' + toField + '.focus();');
	}
};

 function valRadio(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
};

function toggleCollapsable(t){
	new Effect.toggle(t, 'slide');
	toggleImg(t+'-tri', 'images/accents/tri-right.gif', 'images/accents/tri-down.gif');
};


//    .::.  END misc functions  .::...............................

