/*if(window.location.hostname == 'bo'){
	var SiteUrl = '/bo/';
}else{
	var SiteUrl = '/';
}*/

var SiteUrl = '/';

document.getElementById('Te');

function procAjaxResponse(t){
	try {
		eval(t.responseText);
		
	}catch (err) {
		//alert('Error in evaled code:' + err + "\n" + t.responseText);
		window_r(err, 100, 'Error in Evaled Code:');
	}
}

function procAxajEx(t){
	//alert('Exception:' + t);
	window_r(t, 100, 'Exception:');
}

function procAjaxFail(t){
	//alert('Failure:' + t);
	window_r(t, 100, 'Failure:');
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
  			if (oldonload) {
    			oldonload();
  			}
  			func();
		}
	}
}

function new_freecap()
{
	if(document.getElementById)
	{
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
	}
}



/*
	Functions for the write a comment script
*/

//This actually shows the write comment form on each resources page
function write(){
	if(ddms_ok) {
		obj_div = document.getElementById('write_form');
		obj_div.style.display='';
		document.input_comment.wComment.focus();
		window.scroll(0,50000);
	}else{
		alert('Please wait a few seconds for the form to load and try again');
	}
}

function get_ddms(){
	var url = SiteUrl + 'ajax.php?w=get_country_ddm_write_comment';
	var params = '&pCountry=' + pCountry + '&pState=' + pState;
    var obj_ajax = new Ajax.Request(url, {method: 'post', parameters: params, onSuccess: procAjaxResponse, onException: procAxajEx, onFailure: procAjaxFail});	
}

function textcounter(obj,m,update) {
	var max		= m;
	var base	= obj;
	var cur		= base.value.length;
	var cnt		= document.getElementById(update);

	if (cur > max){
		//base.body.value = base.body.value.substring(0, max);
		base.value = base.value.substring(0, max);
		//base.scrollTo(max);
					
	}else{
		cnt.innerHTML = max - cur
	}
}

function checkState(){
	var obj = document.getElementById('wCountry');
	if(obj.value == 'US'){
		document.getElementById('statesAustralia').style.display = 'none';
		document.getElementById('statesUnitedStates').style.display = '';
	}else if(obj.value == 'AU'){
		document.getElementById('statesUnitedStates').style.display = 'none';
		document.getElementById('statesAustralia').style.display = '';
	}else{
		document.getElementById('statesUnitedStates').style.display = 'none';
		document.getElementById('statesAustralia').style.display = 'none';
	}
}

function send() {
	var btn=document.getElementById('btn');
	btn.value='Pls Wait';
	btn.disabled=true;
	
	var url = SiteUrl + 'ajax.php?w=check_write_review';
	var params = $('frmComment').serialize(true);
	var obj_ajax = new Ajax.Request(url, {method: 'post', parameters: params, onSuccess: procAjaxResponse, onException: procAxajEx, onFailure: procAjaxFail});	
	
	wait_time(5000, "try {document.getElementById('btn').value = 'Re-Submit'; document.getElementById('btn').disabled = false;} catch(err) {}");
}

var start_timer;

function wait_time(t, cmd){
	start_timer = setTimeout(cmd, t); 
}

/*
	Finish functions for write a comment
*/

function vote(id,val){
	document.getElementById('yes_button_' + id).disabled=true; document.getElementById('no_button_' + id).disabled=true;
	var res_type = document.input_comment.wResourceType.value;
	var res_id = document.input_comment.wResourceId.value;
	
	var url = SiteUrl + 'ajax.php?w=check_vote_on_comment';
	var params = "&id=" + id + "&val=" + val + "&res_id=" + res_id + "&res_type=" + res_type;
    var obj_ajax = new Ajax.Request(url, {method: 'post', parameters: params, onSuccess: procAjaxResponse, onException: procAxajEx, onFailure: procAjaxFail});	
}

//For the Left subnav Newsletter
var checking_first_form = false;
function checkFirstForm(form) {
    if(!checking_first_form){
        checking_first_form = true;
        var url = SiteUrl + 'ajax.php?w=check_newsletter_email';
        var params = $('frmNewsetter').serialize(true);
        var obj_ajax = new Ajax.Request(url, {method: 'post', parameters: params, onSuccess: procAjaxResponse, onException: procAxajEx, onFailure: procAjaxFail});
    }
}

function tell_a_friend(){
	if(ddms_ok){
		document.getElementById('span_tell_a_friend').style.display='';
		//document.getElementById('Tell_Friend_2').focus();
	} else{
		alert('Please wait a second for the form to load and try again');
	}
}



function debugJS() {
	
	/**
	 *	tabulation
	 */
	this.tabul			= '    ';
	
	/**
	 * fonctions
	 */
	this.dumpJS			= dumpJS;
	
	
	/**
	 *	@desc	decompose récursivement un element
	 *	@param	mixed	elt		element a decomposer
	 *	@param	int		max		nombre maxi de recurances
	 *	@param	string	S_tab	suivi des tabulations
	 *	@param	int		rec		suivi de reccuression
	 */
	function dumpJS(elt, max, S_tab, rec) {
		if (max == undefined) {
			max = 2;	
			
		}
		rec++;
		var S_result	= '';
		if (elt == 'undefined') {
			return "undefined";
			
		}
		switch (typeof elt) {
			case 'object' : 
				S_result	+= "object {\n";
				if (rec < max) {
					for (myI in elt) {
						try {
							S_result += S_tab + this.tabul + '[' + myI + '] => ' 
							S_result += this.dumpJS(elt[myI], max, S_tab + this.tabul, rec);
							
						} catch (e) {
							S_result += S_tab + this.tabul + '[' + myI + '] => ' + "** ERROR **\n";
							
						}
						
					}
					
				} else {
					S_result += S_tab + this.tabul + "** MAX RECURSION **\n";	
					
				}
				S_result	+= S_tab + "}\n";
				break;
				
			case 'string' :
				S_result	+= typeof elt + ' "' + elt + "\"\n";
				break;
 
			default :
				S_result	+= typeof elt + '(' + elt + ")\n";
				break;
				
		}
		return S_result;
		
	}
	
}
 
___O_debugJS	= new debugJS();
/**
 *	@desc	decompose récursivement un element et affiche une alerte
 *	@param	mixed	elt		element a decomposer
 *	@param	int		max		nombre maxi de recurances
 */
function alert_r(elt, max) {
	alert(___O_debugJS.dumpJS(elt, max, "", 0));	
	
}
 
/**
 *	@desc	decompose récursivement un element et affiche dans le body
 *	@param	mixed	elt		element a decomposer
 *	@param	int		max		nombre maxi de recurances
 */
function document_r(elt, max) {
	document.write('<pre>');	
	document.write(___O_debugJS.dumpJS(elt, max, "", 0));	
	document.write('</pre>');	
	
}
 
 
/**
 *	@desc	decompose récursivement un element et affiche une nouvelle fenetre
 *	@param	mixed	elt		element a decomposer
 *	@param	int		max		nombre maxi de recurances
 */
function window_r(elt, max, msg) {
	win = window.open('', 'format','width=400,height=300,left=50,top=50,status,menubar,scrollbars,resizable');
	win.document.write('<pre>' + '<b>' + msg + '</b><br />' + ___O_debugJS.dumpJS(elt, max, "", 0) + '</pre>');
	win.focus();	
	
}

function restore_rn(o){
	o.value = o.value.replace(/rnrn/g, "\r\n");
}
