var IE = ( document.all );
var DOM = ( document.getElementById );
var boxResizeObj, boxPositionObj;
var base = location.href.replace(/\?.*/,'').replace(/^http:\/\/(.*)/,'$1').split('/');
var base_url = 'http://' + ( base[1] == 'guia' ? base[0]+'/guia' : base[0] );
var curr_url = location.href.replace(/\?.*/,'');

///////////////////////////////////////////////////////////////////////////////////////
function DoResizeBox( evt, box, opening, b_left, b_top )
{
	evt = evt ? evt : ( window.event ? window.event : null );
	var obj = document.getElementById(box);
	var num_steps = 10;
	var d_width = obj.getAttribute('width');
	var d_height = obj.getAttribute('height');
	//window.status = 'clientX = '+evt.clientX+', clientY = '+evt.clientY+', b_left = '+b_left+', b_top = '+b_top;
	b_left = opening ? ( evt.clientX - 400 ) : ( obj.style.left.replace(/px/,'') - 130 );
	b_top = opening ? ( ( evt.pageY ? evt.pageY : evt.clientY+document.body.scrollTop ) - 15 ) : obj.style.top.replace(/px/,'');

	if( opening )
	{
		obj.style.display = '';
		setTimeout( 'SetBoxSize("'+box+'",true,'+d_width+','+d_height+','+b_left+','+b_top+','+num_steps+')', 10 );
	}
	else
	{
		boxResizeObj = setTimeout( 'SetBoxSize("'+box+'",false,'+d_width+','+d_height+','+b_left+','+b_top+','+num_steps+')', 10 );
	}
}

///////////////////////////////////////////////////////////////////////////////////////
function SetBoxSize( box, opening, box_width, box_height, box_left, box_top, num_steps )
{
	var obj = document.getElementById(box);
	var width_inc = ( box_width / num_steps );
	var height_inc = ( box_height  / num_steps );
	var d_width = Number( obj.style.width.replace(/px/,'') );
	var d_height = Number( obj.style.height.replace(/px/,'') );
	var d_left = box_left+box_width-(d_width/2)+( document.all ? 0 : 40 );
	var d_top = box_top+10;
	if( ( opening && d_width < box_width ) || ( !opening && d_width > 0 ) )
	{
		d_width = opening ? ( d_width+width_inc ) : ( d_width-width_inc );
		d_height = opening ? ( d_height+height_inc ) : ( d_height-height_inc );
		obj.style.width = d_width+'px';
		obj.style.height = d_height+'px';
		obj.style.left = d_left+'px';
		obj.style.top = d_top+'px';
		setTimeout( 'SetBoxSize("'+box+'",'+opening+','+box_width+','+box_height+','+box_left+','+box_top+','+num_steps+')', 10 );
	}
	else if( !opening )
	{
		boxResizeObj = setTimeout( 'document.getElementById("'+box+'").style.display="none"', 10 );
	}
	//window.status = 'width: '+d_width+', height: '+d_height;
}

///////////////////////////////////////////////////////////////////////////////////////
// strip out html that could cause security issues.
function StripHTML( form )
{
	for( i = 0; i < form.length; i++ )
	{
		if( form[i].type.toLowerCase() == 'text' || form[i].type.toLowerCase() == 'textarea' ) 
			form[i].value = form[i].value.replace( /\<[^>]*\>/ig, '' );
	}
}

///////////////////////////////////////////////////////////////////////////////////////
function StripWhiteSpace( text )
{
	text = text.replace(/[\t\n\r]/ig,' ').replace(/\s{2,}/ig,' ');
	text = text.replace(/^ {1,}/ig,'').replace(/ {1,}$/ig,'');
	return text;
}

///////////////////////////////////////////////////////////////////////////////////////
function CleanText( text )
{
	text = text.replace( /\<[^>]*\>/ig, '' );
	text = StripWhiteSpace( text );
	return text;
}

///////////////////////////////////////////////////////////////////////////////////////
// return bool - email address validation
function ValidEmail( email )
{
	if( !email ) return false;
	if( email.indexOf('@') == -1 || email.indexOf('.') == -1 ||
			email.indexOf('@') != email.lastIndexOf('@') ||
			email.indexOf('@') == 0 || email.indexOf('@') == email.length ||
			email.indexOf('.') == 0 || email.indexOf('.') == email.length ||
			email.lastIndexOf('.') == email.indexOf('@')+1 ||
			email.indexOf('@') > email.lastIndexOf('.') )
	{
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////
if( window.ActiveXObject && !window.XMLHttpRequest )
{
	window.XMLHttpRequest = function()
	{
		progIds=new Array("Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP");
		for( var i in progIds )
		{ 
			try{ return new ActiveXObject(progIds[i]); }
			catch(ex){}
		}
		return null;
	};
}

///////////////////////////////////////////////////////////////////////////////////////
function GetHTTPObj()
{
	var http;
	try { http = new XMLHttpRequest(); } 
	catch(e) 
	{
		try { http = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch(E) 
		{ 
			try { http = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(ERR) { http = null; }
		}
	}
	return http;
}

var global_http;
///////////////////////////////////////////////////////////////////////////////////////
// This is the actual request using AJAX
function BGReq( action, parms, destObj, callbackFunction )
{
	var http = GetHTTPObj();
	if( http )
	{
		var url = base_url+'/bg';
		http.open( 'POST', url );
		http.onreadystatechange = function()
		{
			if( http.readyState == 4 ) 
			{
				try
				{
					var val = http.responseText.replace(/[\t\n\s]/ig,' ').replace(/"/g,'\"').replace(/'/g,'\'');
					if( destObj && document.getElementById(destObj) && val.length > 0 ) document.getElementById(destObj).innerHTML = http.responseText;
					if( document.getElementById('background_results') ) document.getElementById('background_results').innerHTML = http.responseText;
					if( callbackFunction ) eval( callbackFunction+'("' + val + '")' );
				}
				catch(e){}
			}
		}
		http.setRequestHeader( 'POST', url + ' HTTP/1.1' );
		http.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
		http.send( 'bg_a='+action+'&'+parms.join('&') );
		global_http = http;
		//setTimeout('if( global_http && global_http.readyState != 4 ) alert("background process is taking too long!"); global_http.abort();', 10000 );
	}
	else
		alert( 'Could not get a background process' );
}

///////////////////////////////////////////////////////////////////////////////////////
// This function will set a "session" value dynamically without reloading the page
function SetSessionValue( daName, daValue )
{
	var parms = Array( 'bg_var='+daName, 'bg_val='+daValue );
	BGReq( 'set_val', parms, 'results' );
}

///////////////////////////////////////////////////////////////////////////////////////
function CheckResponse( response )
{
	alert( response );
}

///////////////////////////////////////////////////////////////////////////////////////
// pop open a window. this window can be posted to
function popupWin( url, width, height, useExisting )
{
	var frm;
	var win_opts = 'width='+width+',height='+height+',toolbar=no,location=no,directories=no,menubar=no,status=no,resizable=yes,scrollbars=yes';
	pop_win = window.open( '', '_info', win_opts );
	if( pop_win )
	{
		pop_win.document.write('<font face="tahoma,arial,helvetica" size="4" color="maroon">Loading...</font>');
		window.status = 'Loading...';
	
		if( ! document.getElementById('_generated_form') && ! useExisting ) 
		{
			frm = document.body.appendChild( document.createElement('form') );
			frm.name = '_generated_form';
			frm.id = '_generated_form';
		}
		frm = useExisting ? document.forms[0] : document.getElementById('_generated_form');
		frm.target = '_info';
		frm.action = url;
		frm.submit();
	
		if( !pop_win.closed ) pop_win.focus();
	}
}

///////////////////////////////////////////////////////////////////////////////////////
function EstaraCall( evt, phone, adid )
{
	evt = evt ? evt : ( window.event ? window.event : null );
	var b_top = !evt ? 100 : ( evt.pageY ? evt.pageY : (evt.clientY+document.body.scrollTop) )-200;
	var url = webVoicePop('var1=lapalmainteractivo','var2=','var3=','var4=','var5=adtext','var6=','dtmf_leg2=','AuthorizeURL='+base_url+'/phone?p='+phone+'&a='+adid,'Template=42500'); // 101981

	var callFrameDiv = document.getElementById('_phonecall_frame_div').cloneNode(true);
	if( document.getElementById('_temp_frame_div') ) document.body.replaceChild( callFrameDiv, document.getElementById('_temp_frame_div') );
	callFrameDiv.id = '_temp_frame_div';
	callFrame = callFrameDiv.getElementsByTagName('iframe')[0];
	document.body.appendChild(callFrameDiv);
	var callFrameDoc = callFrame.contentWindow ? callFrame.contentWindow.document : callFrame.contentDocument;
	callFrameDoc.location.href = url;
	callFrameDiv.style.display = '';
	callFrameDiv.style.top = b_top;
	if( ! document.all ) 
	{
		callFrameDiv.style.border = '1px solid #666'
		callFrameDiv.style.borderWidth = '0px 3px 3px 0px'
	}
}
