/** Added for fix bug menu in IE 7 - WC - 12/11/2009 **/

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

Array.prototype.unique = function(b) {
	var a = [], i, l = this.length;
	for( i=0; i<l; i++ ){
		if( a.indexOf( this[i], 0, b ) < 0 ){ 
			a.push( this[i] );
		}
	}	
	return a;
};

/**  End Added for fix bug menu in IE 7 **/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var bo_ns_id = 0;

function startIeFix()
{  
	if(isIE())
	{    
		document.write('<div id="bo_ns_id_' + bo_ns_id + '"><!-- ');  
	}
}

function endIeFix()
{  
	if(isIE())
	{    
		document.write('</div>');    
		var theObject = document.getElementById("bo_ns_id_" + bo_ns_id++);
		var theCode = theObject.innerHTML; //.toLowerCase();
		theCode = theCode.substring(4 ,9+theCode.indexOf("</object>"));
		document.write(theCode);  
	}
}

//Polls
var PollID;
var Home;

function validatePoll(form) {
	for (i=0;i<form.option.length;i++)
	{
		if(form.option[i].checked == true)
		 	return form.option[i].value;
	}
	alert("Must select an option !");
	return false;
}

function sendPollRequest(id, type, home, poll)
{

	if(home=='yes'){
		Home = true;
	}else{
		Home = false
	}	
	PollID = poll;

	var option=false;
	if(id==-1)
	{
		alert("You have already voted !");
		return false;
	}
	if(type=='vote'){
		if(Home==true){
			if(!(option=validatePoll(document.HomePoll)))		
			return false;	
		}else{
			if(!(option=validateInnerPoll(PollID)))		
			return false;
		}
	}
			
	url = 'http://www.latv.com/getpoll.asp?pollID=' + id + '&option=' + option + '&type=' + type + '&home=' + home;
	setPollContent('<center><img src="http://www.latv.com/img/siteElements/loadingLATV.gif" /></center>',PollID);
	makeRequest(url , receivePollRequest, false);
}


function receivePollRequest(aXmlRequest)
{
	if(isCompleted(aXmlRequest))
	{
		setPollContent(getResponse(aXmlRequest));
		aXmlRequest = null;
	}
}

function setPollContent(content)
{
	if(Home==true){
		var oDiv = document.getElementById('pollDIV');
	}else{
		var oDiv = document.getElementById('pollDIV_' + PollID);	
	}
	oDiv.innerHTML = content;
}


function isIE()
{	// only for Win IE 6+  
	// But not in Windows 98, Me, NT 4.0, 2000  
	var strBrwsr = navigator.userAgent.toLowerCase();  
	if(strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0)
	{    
		if(parseInt(strBrwsr.charAt(strBrwsr.indexOf("msie")+5)) < 6)
		{      
			return false;    
		}    

		if(	strBrwsr.indexOf("win98") > -1 ||       
			strBrwsr.indexOf("win 9x 4.90") > -1 ||       
			strBrwsr.indexOf("winnt4.0") > -1 ||       
			strBrwsr.indexOf("windows nt 5.0") > -1)    
		{      
			return false;    
		}    
		
		return true;  
	}
	else
	{    
		return false;  
	}
}

function popup(url, width, height)
{
	 window.open(url,'','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
}

function popupNoScroll(url, width, height)
{
	 window.open(url,'','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,width='+width+',height='+height);
}

function openPhotoMax(photo)
{
	var width = screen.availWidth;
	var height = screen.availHeight;

	window.open(
'photomax.asp?p=' + photo,
'',
'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,scrollbars=1,x=0,y=0,width=' + width + ',height=' + height);
}

function contactForm(address, customurl)
{
	if(customurl)
		popupNoScroll(customurl + 'contact-form/contact-form.asp?ad=' + address, 400, 500);
	else
		popupNoScroll('contact-form/contact-form.asp?ad=' + address, 400, 500);
}

function contactFormJob(address, customurl)
{
	if(customurl)
		popupNoScroll(customurl + 'contact-form/contact-form-job.asp?ad=' + address, 400, 500);
	else
		popupNoScroll('contact-form/contact-form-job.asp?ad=' + address, 400, 500);
}

function updateDaysSelect(day, month, year)
{
	var aMonths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if(isLeapYear(year.value))
		aMonths[1] = 29;

	var iDaySelected = parseInt(day.value);
	var iMonthSelected = parseInt(month.value);

	if(iMonthSelected)
	{
		while(day.length)
			day.remove(0);

		var tmp = document.createElement('option');

		tmp.value = '';
		tmp.text = '---';

		try
		{
			day.add(tmp, null);
		}
		catch(e)
		{
			day.add(tmp);
		}

		for(var i = 1; i <= aMonths[iMonthSelected - 1]; i++)
		{
			tmp = document.createElement('option');

			tmp.value = i;
			tmp.text = i;

			try
			{
				day.add(tmp, null);
			}
			catch(e)
			{
				day.add(tmp);
			}
		}

		if(iDaySelected && iDaySelected <= aMonths[iMonthSelected - 1])
			day.value = iDaySelected;
		else
			day.value = '';

	}
}

function isLeapYear(year)
{
	year = parseInt(year);

	if(year % 4 == 0)
	{
		if(year % 100 != 0)
			return true;
		else
		{
			if(year % 400 == 0)
				return true;
			else
				return false;
		}
	}

	return false;
}

function CreateElement(obj, code)
{
	var d = document.getElementById(obj);
	d.innerHTML = code;
}

//	search controls

function checkSearchValue(){
	searchValue = document.getElementById('search').value;
	if(trim(searchValue) == ''){
		alert("Debes ingresar un termino a buscar !");
		return false;
	}
	return true;
}

// some controls and strings functions

		function validateEmail(val){
			var reg=/(^[a-zA-Z0-9._-]{1,30})@([a-zA-Z0-9.-]{1,30}$)/;
			if(reg.test(val)){
				return true;
			}else{
				return false;
			}
		}
		
		function validateName(val){
			if(val == '' || !val){
				return false;
			}else{
				return true;
			}
		}
		
		function validateComment(comment, enabledNull, minLong, maxLong){
			var cantCar = comment.length;
			if(comment == "" || comment == "Dejanos tu comentario!"){
				if(enabledNull){
					return true;
				}else{
					return false;
				}
			}else{
				if(cantCar >= minLong && cantCar <= maxLong) return true;
				else return false;
			}
		}
			
		function trim(s){
			s = s.replace(/\s+/gi,' '); //sacar espacios repetidos dejando solo uno
			s = s.replace(/^\s+|\s+$/gi,''); //sacar espacios blanco principio y final				
			return s;
		}
		
		
// share functions


	
	function shareContent(){
		var nameTo = trim(document.getElementById("nameTo").value);
		var nameFrom = trim(document.getElementById("nameFrom").value);
		var emailTo = trim(document.getElementById("emailTo").value);
		var message =  trim(document.getElementById("message").value);
		var linkToShare = document.getElementById("linkToShare").innerHTML;		// this value is taking from span id="linkToShare" in share.asp
		
		if(!validateName(nameFrom)){
			alert('Debes ingresar tu nombre.');
			document.getElementById('shareDataForm').nameFrom.focus();
			return false;
		}
		
		if(!validateName(nameTo)){
			alert('Debes ingresar el nombre del destino.');
			document.getElementById('shareDataForm').nameTo.focus();
			return false;
		}
		
		if(!validateEmail(emailTo)){
			alert('Debes ingresar el email del destino.');
			document.getElementById('shareDataForm').emailTo.focus();
			return false;
		}
		
		if(!validateComment(message, false, 10, 100)){
			alert('Debes ingresar tu comentario; al menos ' + 10 + ' caracteres.');
			document.getElementById('shareDataForm').message.focus();
			return false;
		}
						
						
		var urlToLoad = 'http://www.latv.com/actions/shares_ops.php?nameTo=' + escape(nameTo) + '&emailTo=' + emailTo + '&nameFrom=' + escape(nameFrom) + '&message=' + escape(message) + '&linkToShare=' + escape(linkToShare);
		
		//alert(urlToLoad);
		
		loadcontent(urlToLoad, 'resultShare');
		document.getElementById('shareForm').style.display = 'none';
		return false;
	}
	
	
	
	
	//		comments functions
	
	function setTextarea(obj, opt){
		if(opt){
			if(trim(obj.value) == "Dejanos tu comentario!"){
				obj.value = '';
			}
		}else{
			if(trim(obj.value) == ''){
				obj.value = 'Dejanos tu comentario!';
			}
		}
	}
		
		
	function responseDivEnabled(val){
		if(val){
			document.getElementById('responseMeDiv').style.visibility = 'visible';
		}else{
			document.getElementById('responseMeDiv').style.visibility = 'hidden';
		}
	}
	
	function setResponseTo(commentID, commentName, commentContent){
		document.getElementById('responseToComment').value = commentID;
		document.getElementById('commentDataForm').subjectsComment[1].checked = true;
		responseDivEnabled(false);
		document.getElementById('responseMessage').style.display = 'block';
		document.getElementById('responseMessage').style.visibility = 'visible';
		
		document.getElementById('responseMessageNameTo').innerHTML = commentName;
		document.getElementById('responseMessageComment').innerHTML = commentContent;
		
		allocatePage('responseMessage', 'onclick');		
		document.getElementById('commentDataForm').commentFrom.focus();
		return false;
	}
		
	function allocatePage(divToAllocate, event){
		var Xnew;
		var Ynew;
		
		if(event == 'onclick'){
			if (document.getElementById) { 
				Xnew = document.getElementById(divToAllocate).offsetLeft;
				Ynew = document.getElementById(divToAllocate).offsetTop;
			} else if (document.all) {
				Xnew = document.all.getElementById(divToAllocate).offsetLeft;
				Ynew = document.all.getElementById(divToAllocate).offsetTop;
			} else if (document.layers) { 
				Xnew = document.layers[divToAllocate].pageX;
				Ynew = document.layers[divToAllocate].pageY;
			} else 
				alert('Su navegador no está soportado.');
		}else{
			//event = 'onload'
			var divToAllocateCoords = document.getElementById(divToAllocate);
			Xnew = getDimensions(divToAllocateCoords).x;
			Ynew = getDimensions(divToAllocateCoords).y;
		}
		/*
		var yTmp = -2;
		while (yTmp < Ynew){
			setTimeout("window.scrollTo(" + Xnew + ", " + yTmp + ")", 100);
		}
		*/
		window.scrollTo(Xnew, Ynew-2);		// padding-top:2px;
	}
	
	function viewResponses(opt, commentID){
		if(opt){
			document.getElementById('responseHide_' + commentID).style.visibility = 'visible';
			document.getElementById('responseHide_' + commentID).style.display = 'block';
			
			document.getElementById('responseView_' + commentID).style.visibility = 'hidden';
			document.getElementById('responseView_' + commentID).style.display = 'none';
			
			document.getElementById('responsesTable_' + commentID).style.visibility = 'visible';
			document.getElementById('responsesTable_' + commentID).style.display = 'block';
			
		}else{
			document.getElementById('responseView_' + commentID).style.visibility = 'visible';
			document.getElementById('responseView_' + commentID).style.display = 'block';
			
			document.getElementById('responseHide_' + commentID).style.visibility = 'hidden';
			document.getElementById('responseHide_' + commentID).style.display = 'none';
			
			document.getElementById('responsesTable_' + commentID).style.visibility = 'hidden';
			document.getElementById('responsesTable_' + commentID).style.display = 'none';
		}
	}
		
	function saveComment(){
		var commentFrom = trim(document.getElementById("commentFrom").value);
		var commentEmail = trim(document.getElementById("commentEmail").value);
		var comment = trim(document.getElementById("comment").value);
		
		var captcha = trim(document.getElementById("captcha").value);
		var captchaQuestion = trim(document.getElementById("captchaQuestion").value);
		var captchaSecureData = trim(document.getElementById("captchaSecureData").value);
		
		var responseMe = 0;	//	aviso de respuesta OFF

		var element_TYPE = trim(document.getElementById("element_TYPE").value);	// artist, show, foto
		var element_ID = trim(document.getElementById("element_ID").value);	// id del artist, show, foto
		var element_NAME = trim(document.getElementById("element_NAME").value);	// nombre/title del artist, show, foto
		
		var MailTo = trim(document.getElementById("MailTo").value);
		var MailSubject = trim(document.getElementById("MailSubject").value);
		
		var responseToComment = document.getElementById("responseToComment").value;
		
		for (i=0;i<document.getElementById('commentDataForm').subjectsComment.length;i++) {
			if (document.getElementById('commentDataForm').subjectsComment[i].checked) {
				subjectComment = document.getElementById('commentDataForm').subjectsComment[i].value;
			}
		}
		
		if(responseToComment > 0){
			subjectComment = "response";
		}
		
		if(subjectComment == 'comment'){
			if (document.getElementById('commentDataForm').responseMe.checked) {
				responseMe = 1;	//	aviso de respuesta ON
			}
		}

		if(!validateName(commentFrom)){
			alert('Debes ingresar tu nombre.');
			document.getElementById('commentDataForm').commentFrom.focus();
			return false;
		}
		
		if(!validateEmail(commentEmail)){
			alert('Debes ingresar tu email.');
			document.getElementById('commentDataForm').commentEmail.focus();
			return false;
		}
		
		if(!validateComment(comment, false, 10, 100)){
			alert('Debes ingresar tu comentario; al menos ' + 10 + ' caracteres.');
			document.getElementById('commentDataForm').comment.focus();
			return false;
		}		
		
		if(captchaQuestion == '' || !captchaQuestion){
			alert('Debes contestar la pregunta.');
			document.getElementById('commentDataForm').captchaQuestion.focus();
			return false;
		}
				
		var urlToLoad = 'http://www.latv.com/actions/comments_ops.php?submit=true';
		
		urlToLoad = addParamether(urlToLoad, 'commentFrom', escape(commentFrom));
		urlToLoad = addParamether(urlToLoad, 'commentEmail', commentEmail);
		urlToLoad = addParamether(urlToLoad, 'comment', escape(comment));
		urlToLoad = addParamether(urlToLoad, 'subjectComment', subjectComment);
		urlToLoad = addParamether(urlToLoad, 'responseMe', responseMe);
		urlToLoad = addParamether(urlToLoad, 'element_TYPE', element_TYPE);
		urlToLoad = addParamether(urlToLoad, 'element_ID', element_ID);
		urlToLoad = addParamether(urlToLoad, 'element_NAME', element_NAME);
		urlToLoad = addParamether(urlToLoad, 'MailTo', MailTo);
		urlToLoad = addParamether(urlToLoad, 'MailSubject', MailSubject);
		urlToLoad = addParamether(urlToLoad, 'responseToComment', responseToComment);
		urlToLoad = addParamether(urlToLoad, 'captchaQuestion', captchaQuestion);
		urlToLoad = addParamether(urlToLoad, 'captchaSecureData', captchaSecureData);

		// clean form
		document.getElementById("commentFrom").value = '';	
		document.getElementById("commentEmail").value = '';
		document.getElementById("comment").value = 'Dejanos tu comentario!';
		document.getElementById("captchaQuestion").value = '';
		
		//alert(urlToLoad);
		if(captcha != ''){
			alert('ERROR. Algo raro hay con tu envio. Bye!');
		}else{
			loadcontent(urlToLoad, 'resultComment');
			document.getElementById('commentForm').style.display = 'none';
			document.getElementById('resultComment').style.display = 'block';			
		}
		
		return false;
	}
	
	function addParamether(urlToAdd, paramNameToAdd, paramValueToAdd){
		var isFirstParam = true;
		var newURL = '';
		
		for(i=0;i<urlToAdd.length;i++){
			if(urlToAdd.charAt(i)=="?") 
				isFirstParam = false;
		}
		
		newURL = urlToAdd + ((isFirstParam) ? '?' : '&') + paramNameToAdd + '=' + paramValueToAdd;
		return newURL;
	}
	
	getDimensions = function(oElement) {
		var x, y, w, h;
		x = y = w = h = 0;
		if (document.getBoxObjectFor) { // Mozilla
			var oBox = document.getBoxObjectFor(oElement);
			x = oBox.x-1;
			w = oBox.width;
			y = oBox.y-1;
			h = oBox.height;
		}else if (oElement.getBoundingClientRect) { // IE
			var oRect = oElement.getBoundingClientRect();
			x = oRect.left-2;
			w = oElement.clientWidth;
			y = oRect.top-2;
			h = oElement.clientHeight;
		}
		return {x: x, y: y, w: w, h: h};
	}
