var globalUserView;
var globalLimit=0;

var domain='';
if (document.getElementById('domain')) {
    domain=document.getElementById('domain').value;
}
//Funcion para obtener comentarios de playboy
//
//UserView Id de usuario que se requiere los comentario
//limit cantidad de comentarios
function getPlayboyProfileComments(UserView,limit){
	globalUserView = UserView;
	globalLimit = limit;
	var sURL='/playboy2009/PlayboyComments.php?id=';
	if (UserView != null){sURL=sURL+UserView;}
	if (limit != null){sURL=sURL+"&limit="+limit;}
	$.ajax({type: "GET",url: sURL,data:'',success:getPCommentsSuccess});
}
//Accion despues de solicitud de pcomments
function getPCommentsSuccess(postData){
	comment = document.getElementById('comments');
	comment.innerHTML = postData;
	var itableComment = document.getElementById('comments');
	var arrImages = itableComment.getElementsByTagName("img");
	$(arrImages).each(function() {
		$(this).load(function() {
			if( this.width > maxWidthUnit ){
				var Imgsrc = this.src;
				this.style.cursor="pointer";
				assignOnClick(this,this.width,this.height);
				this.title="Zoom";
				this.height=this.height-(((this.width-maxWidthUnit)/this.width)*this.height);
				this.width = maxWidthUnit;
			}
		});
	});
}
//Funcion para borrar un comentario
//
//commentid  Id del comentario a borrar
//UserView   Id del usuario que solicita borrado
//limit      cantidad envia a p coments
function deleteComment(commentid,UserView,limit){
	var sURL='/user/actions/delcomments.php';
	$.ajax({type: "GET",url: sURL,data:'commid='+commentid+'&user='+UserView,success:successDeletePlayboyComments});
}
//Accion posterior al borrado de un comentario
function successDeletePlayboyComments(postData){
	getPlayboyProfileComments(globalUserView,globalLimit);
}
//Funcion para los blinks
function showBlinks() {
	window.open("/blinks/","blinks", "scrollbars=yes,width=700px,height=450px");
}

function getNewImage(ImageId, numPageId, type){
    var idAlbum = document.getElementById('idAlbum').value;
    var idUser = document.getElementById('idUser').value;

    document.getElementById('BigImage').src= domain+'/images/playboylatino2009/loader.gif';
    document.getElementById('BigImage').style.border= '0px';
    var sFile = '';
    if(type=='contestan'){
        sFile = 'contestProfile.php?photoId=';
    }else{
        sFile = 'finalistProfile.php?photoId=';
    }
    trackAjax(numPageId, '/playboy2009/' + sFile + ImageId );
    $.ajax({type: "GET",url: '/playboy2009/actions/getImagePath.php',data:'userId=' +
       idUser + '&albumId=' + idAlbum + '&imageId=' + ImageId, success:changeImagePath});
}
function changeImagePath(postData){
    var imageSrc = eval(postData);
    document.getElementById('BigImage').src= imageSrc;
    document.getElementById('BigImage').style.border= '7px solid #0D0D0D';
}
//Funciones para votar

function showRateIt() {
	$('#rating').hide();
	$('#rate').show();

}

function hideRateIt() {
		restoreRateMsg();
		$('#rate').hide();
		$('#rating').show();
}

function rateIt(rate) {
	var sURL = '/playboy2009/actions/rateFinalist.php';
	var conId = document.getElementById('conId').value;
	var data = 'rate='+rate+'&conId='+conId;
	var sHTML = $.ajax({type: "GET", url: sURL, data: data, async: false}).responseText;
	var result = eval('(' + sHTML + ')');
	if(result.error == 0) {
		$('#starText').hide();
		$('#successMsg').show();
		document.getElementById('rate').onmouseout=null;
		document.getElementById('rating').onmouseover=null;
		$('#rate').css('cursor','default');
		disableRating();
		/*showStars(result.average);*/
		var numTotal = parseInt(document.getElementById('totalRates').value,10);
		document.getElementById('totalRates').value = numTotal + 1
		document.getElementById('viewTotalRates').innerHTML = numTotal + 1;
	}else{
	   $('#error_' + result.error ).show();
	   disableRating();
	   hideRateIt();
	   document.getElementById('rate').onmouseout=null;
	   document.getElementById('rating').onmouseover=null;
	}    
}
function showStars(numStars) {
	setTimeout('clearStars()','100');
	setTimeout('fillStars('+numStars+')', '100');

}
function fillStars(numStars) {
	for(var i=1; i <=numStars; i++) {
		$('#star-'+i).attr('src', imgSrcFull);
	}
	changeText(numStars-1);
}

function restoreRateMsg() {
	$('#starText').hide();
}

function changeText(index) {
	$('#starText').text(messages[index]);
	$('#starText').show();
}

function clearStars() {
	for(var i=1; i <= 5; i++) {
		$('#star-'+i).attr('src', imgSrcEmpty);
	}
}

function disableRating() {
	for(var i=1; i <= 5; i++) {
		document.getElementById('star-'+i).onmouseover=null;
		document.getElementById('star-'+i).onclick=null;
	}
}
function sendToLogin(urlLogin) {
	window.location.href = urlLogin;
}

function loginToRate() {
	$('#nologed').show();
	hideRateIt();
	document.getElementById('rate').onmouseout=null;
	document.getElementById('rating').onmouseover=null;
}

function resizeProfileImg() {
	var newdimension = 200;
	var profileimage=$('#imgprofile');
	if(profileimage.width()!=null){
		if(isTaller(profileimage.width(),profileimage.height())){if(profileimage.height()>=newdimension)$(profileimage).css('height',newdimension)} else {if(profileimage.width()>=newdimension)$(profileimage).css('width',newdimension)}profileimage.show();
	}
	$('#imgprofile').load(function() {
		if( isTaller(this.width,this.height) ){if(($this).height()>=newdimension)$(this).css('height',newdimension)} else {if($(this).width()>=newdimension)$(this).css('width',newdimension)}$(this).show();
	});
	var profileimage=$('#imgprofile2');
	if(profileimage.width()!=null){
		if(isTaller(profileimage.width(),profileimage.height())){if(profileimage.height()>=newdimension)$(profileimage).css('height',newdimension)} else {if(profileimage.width()>=newdimension)$(profileimage).css('width',newdimension)}profileimage.show();
	}
	$('#imgprofile').load(function() {
		if( isTaller(this.width,this.height) ){if(($this).height()>=newdimension)$(this).css('height',newdimension)} else {if($(this).width()>=newdimension)$(this).css('width',newdimension)}$(this).show();
	});
}
function isTaller(width,height) {
	if(height>width){ return true;}
	else {return false};
}
//Validaciones del register
function validate(){
    var pass = document.getElementById('usr_pass');
    var mail = document.getElementById('txtEmail');
    var firstname = 		document.getElementById('txtFirstName');
    var lastname = 			document.getElementById('txtLastName');
    var country = 			document.getElementById('DisplayContries');
    var state = 			document.getElementById('DisplayStates');
    var city = 				document.getElementById('DisplayCities');
    var telephonehome = 	document.getElementById('txtPhone');
    var height = 			document.getElementById('numHeight');
    var weight = 			document.getElementById('numWeight');
    var foto1 =			document.getElementById('textfield1');
    var foto2 =			document.getElementById('textfield2');
    var foto3 =			document.getElementById('textfield3');
    var foto4 =			document.getElementById('textfield4');
    var foto5 =			document.getElementById('textfield5');
    var userlogged = document.getElementById('numUserId');    
    var filter=/^\w+([\.-]?\w+)*@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    var regTxt = /^([a-zA-Z\sáéíóúñÁÉÍÓÚÑÄËÏÖÜäëïöü])*$/;
    var regphone = /^([0-9\ \(\)\-])*$/;
    var regPassword = /^([A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\-\_\=\\\+\.\,\/\;\:\'\?\|\"\}\{\]\[])*$/;
    var regusername =/^([A-Za-z0-9\-\_\.])*$/;
    var reguser =/^([\-\_\.])*$/;

    var send = true;

    document.getElementById('FirstName').style.display='none';
    document.getElementById('LastName').style.display='none';
    document.getElementById('Country').style.display='none';
    document.getElementById('Region').style.display='none';
    document.getElementById('CityError').style.display='none';
    document.getElementById('Phone').style.display='none';
    document.getElementById('Height').style.display='none';
    document.getElementById('Weight').style.display='none';
    document.getElementById('wrongfile1').style.display='none';
    document.getElementById('wrongfile2').style.display='none';
    document.getElementById('wrongfile3').style.display='none';
    document.getElementById('wrongfile4').style.display='none';
    document.getElementById('wrongfile5').style.display='none';
    document.getElementById('reglas').style.display='none';
    if(userlogged.value==0){
        /**Comprobacion de correo**/
        var filter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
        document.getElementById('correo').style.display='none';
        if (mail.value==''){
            document.getElementById('correo').style.display='';
            send = false;
        }
        if (mail.value.length > 125 ){
            document.getElementById('correo').style.display='';
            send = false;
        }
        if ((mail.value.indexOf ('@',0)==-1) || (mail.value.indexOf ('.',0)==-1) || (mail.value.length <5)) {
            document.getElementById('correo').style.display='';
            send = false;
        }
        if (mail.value.indexOf ('@',0)==(mail.value.indexOf ('.',0)-1)){
            document.getElementById('correo').style.display='';
            send = false;
        }
        if(!filter.test(mail.value)){
            document.getElementById('correo').style.display='';
            send = false;
        }
        //Password
        var regPassword = /^([A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\-\_\=\\\+\.\,\/\;\:\'\?\|\"\}\{\]\[])*$/;
        document.getElementById('pass').style.display='none';
		document.getElementById('pass1').style.display='none';
        if(!regPassword.test(pass.value) ){
			document.getElementById('pass').style.display='';
			document.getElementById('pass1').style.display='none';

			pass.focus();
			send = false;
		}
        if(pass.value.length < 6 || pass.value.length > 25 )
		{
			document.getElementById('pass').style.display='';
			document.getElementById('pass1').style.display='none';

			pass.focus();
			send = false;
		}
		var numMonth = document.getElementById('numMonth').value;
		var numDay = document.getElementById('numDay').value;
		var numYear = document.getElementById('numYear').value;
		document.getElementById('birthdate').style.display='none';
		var invalidDate = document.getElementById('invalidDate');
		if(invalidDate != null){
		    invalidDate.style.display='none';
		}
		if ((numMonth=="--") || (numDay=="--") || (numYear=="--")){
            document.getElementById('birthdate').style.display='';
    		send = false;
    	}
    }
    //Nombre
    if (firstname.value==''){
        firstname.focus();
        document.getElementById('FirstName').style.display='';
        send = false;
    }
    if (!regTxt.test(firstname.value)){
        firstname.focus();
        document.getElementById('invalidName').style.display='';
        send = false;
    }
    //Apellido
    if (lastname.value==''){
        lastname.focus();
        document.getElementById('LastName').style.display='';
        send = false;
    }
    if (!regTxt.test(lastname.value)){
        lastname.focus();
        document.getElementById('invalidLastName').style.display='';
        send = false;
    }
    //Pais
    if (country.value==""){
        country.focus();
        document.getElementById('Country').style.display='';
        send = false;
    }
    //Telefonos
    if (telephonehome.value=='' ){
        telephonehome.focus();
        document.getElementById('Phone').style.display='';
        send = false;
    }    
    if (telephonehome.value!=''){
        if (!regphone.test(telephonehome.value) || telephonehome.value.length < 5){
            telephonehome.focus();
            document.getElementById('Phone').style.display='';
            send = false;
        }
    }
    //Estatura
    if (height.value==""){
    height.focus();
    document.getElementById('Height').style.display='';
    send = false;
    }

    //peso
    if (weight.value==""){
        weight.focus();
        document.getElementById('Weight').style.display='';
        send = false;
    }

    //fotos
    if (foto1.value=="" ){
        document.getElementById('wrongfile1').style.display='';
        send = false;
    } else{
        foto= foto1.value.toString().toLowerCase();
        foto = foto.substring(foto.length-4,foto.length);
        if( foto != '.jpg' && foto != 'jpeg' && foto != '.gif' && foto != '.png' &&
            foto != '.bmp'){
                document.getElementById('nofile1').style.display='';
                send = false;
        }
    }

    if (foto2.value==""){
        document.getElementById('wrongfile2').style.display='';
        send = false;
    } else{
        foto= foto2.value.toString().toLowerCase();
        foto = foto.substring(foto.length-4,foto.length);
        if( foto != '.jpg' && foto != 'jpeg' && foto != '.gif' && foto != '.png' &&
            foto != '.bmp') {
            document.getElementById('nofile2').style.display='';
            send = false;
        }
    }

    if (foto3.value==""){
        document.getElementById('wrongfile3').style.display='';
        send = false;
    } else{
        foto= foto3.value.toString().toLowerCase();
        foto = foto.substring(foto.length-4,foto.length);
        if( foto != '.jpg' && foto != 'jpeg' && foto != '.gif' && foto != '.png' &&
            foto != '.bmp') {
            document.getElementById('nofile3').style.display='';
            send = false;
        }
    }
    if (foto4.value==""){
        document.getElementById('wrongfile4').style.display='';
        send = false;
    } else{
        foto= foto4.value.toString().toLowerCase();
        foto = foto.substring(foto.length-4,foto.length);
        if( foto != '.jpg' && foto != 'jpeg' && foto != '.gif' && foto != '.png' &&
            foto != '.bmp') {
            document.getElementById('nofile4').style.display='';
            send = false;
        }
    }

    if (foto5.value==""){
        document.getElementById('wrongfile5').style.display='';
        send = false;
    } else{
        foto= foto5.value.toString().toLowerCase();
        foto = foto.substring(foto.length-4,foto.length);
        if( foto != '.jpg' && foto != 'jpeg' && foto != '.gif' && foto != '.png' &&
            foto != '.bmp') {
            document.getElementById('nofile5').style.display='';
            send = false;
        }
    }
    //Terminos y condiciones
    if(!document.getElementById('chkrules').checked){
        document.getElementById('reglas').style.display='';
        send = false;
    }

    //Checamos si la forma se va a mandar o no
    if(send){
        var pics = document.getElementById('bttregular');
        pics.innerHTML = '';
        var loader = document.createElement('img');
        loader.src = domain+'/images/playboylatino2009/loader.gif';
        pics.appendChild(loader);
        return true;
    }else{
        return false;
    }
}
function loginToVote()
{    
    $('#logError').show();
}

function loginToInvite()
{
    $('#logErrorInvite').show();
}
function validateScrapper()
{
    if(document.getElementById('user_email').value != '' && 
        document.getElementById('sType').value != '') {
        document.forms.miniscrapper.submit();
    } else {
        $('#logErrorScrapper').show();
    }
}
function validateMacroScrapper()
{
    if(document.getElementById('user_email').value != '' && 
        document.getElementById('sType').value != '') {
        document.forms.macroscrapper.submit();
    } else {
        $('#logErrorScrapper').show();
    }
}
function loginToAdd()
{
    $('#loginToAdd').show();
}
function checkValue(){
    var group = document.getElementById('group').value;
    if(document.getElementById('group').value != 5 && document.getElementById('group').value != 6 && document.getElementById('group').value != '') { 
        document.getElementById('isCountryCode').value = 'true';        
    }
    document.forms.frmSwitch.submit();    
}
/*Twofish*/
function pickgift(giftId,giftTitle, giftName,giftPrice,giftPic){
	var GiftId = document.getElementById('backgiftid').value;
	var GiftTitle = document.getElementById('backgifttitle').value;
	var GiftName = document.getElementById('backgiftname').value;
	var GiftPrice = document.getElementById('backgiftprice').value;
	var GiftPic = document.getElementById('backgiftpic').value;
		
	if (GiftId != 0 && GiftPic != 0){
		document.getElementById('gift' + GiftId).style.background = 'none';
	}
	
	document.getElementById('backgiftid').value = giftId;
	document.getElementById('backgifttitle').value = giftTitle;
	document.getElementById('backgiftname').value = giftName;
	document.getElementById('backgiftprice').value = giftPrice;
	document.getElementById('backgiftpic').value = giftPic;
	document.getElementById('gift' + giftId).style.background = '#43668E';
	var res = showBalance();
	if(res){
		document.getElementById('itemname').innerHTML = giftTitle;
		document.getElementById('itemprice').innerHTML = giftPrice;
		document.getElementById('img-gift').src = giftPic;
		var store = document.getElementById('store').value;
		if(store == 'gift'){
			document.getElementById('exchange').innerHTML = document.getElementById('qpointsformat').value;
		}else if(store == 'qp'){
			document.getElementById('exchange').innerHTML = 'Qdollars';
		}
		$('#show-gift').show('slow');
		$('#bottoms').show('slow');
	}else{
	$('#show-gift').hide('slow');
	$('#bottoms').hide('slow');
	}	
	changeBtn();
}

function removeGift(tab){
	var GiftId = document.getElementById('backgiftid').value;
	$('#show-gift').hide('slow');
	$('#item-balance').hide('slow');
	if(tab == 'gift'){
		document.getElementById('purchasebtngift').style.display='none';
		document.getElementById('BtnCancelgift').style.display='none';
	}else if(tab == 'qp'){
		document.getElementById('purchasebtnqp').style.display='none';
		document.getElementById('BtnCancelqp').style.display='none';
	}if(tab == 'contestant'){
		document.getElementById('purchasebtncont').style.display='none';
		document.getElementById('BtnCancelcont').style.display='none';
	}
	
	if (GiftId != 0){
		document.getElementById('gift' + GiftId).style.background = 'none';
	}
	document.getElementById('backgiftid').value = 0;
	document.getElementById('backgiftname').value = 0;
	document.getElementById('backgifttitle').value = 0;
	document.getElementById('backgiftprice').value = 0;
	document.getElementById('backgiftpic').value = 0;	
}

function pickcontestant(contId,contName,contPic){
	var ContId = document.getElementById('backid').value;
	var ContName = document.getElementById('backname').value;
	var ContPic = document.getElementById('backpic').value;	
		
	if (ContId != 0 && ContName != 0 && ContPic != 0){
		document.getElementById('contestant' + ContId).style.background = 'none';
	}
	
	document.getElementById('backid').value = contId;
	document.getElementById('backname').value = contName;	
	document.getElementById('backpic').value = contPic;
	document.getElementById('contestant' + contId).style.background = '#43668E';	
	document.getElementById('a-earner').innerHTML = contName;	
	document.getElementById('img-earner').src = contPic;
	$('#show-contestant').show('slow');
	changeBtn();	
}

function removeContestant(tab){
	var ContId = document.getElementById('backid').value;
	$('#show-contestant').hide('slow');
	if(tab == 'gift'){
		document.getElementById('purchasebtngift').style.display='none';
		document.getElementById('BtnCancelgift').style.display='none';
	}else if(tab == 'qp'){
		document.getElementById('purchasebtnqp').style.display='none';
		document.getElementById('BtnCancelqp').style.display='none';
	}if(tab == 'contestant'){
		document.getElementById('purchasebtncont').style.display='none';
		document.getElementById('BtnCancelcont').style.display='none';
	}
			
	if (ContId != 0){
		document.getElementById('contestant' + ContId).style.background = 'none';
	}
	document.getElementById('backid').value = 0;
}

function removeandsendgift(){
	var backId = document.getElementById('backid').value;
	var store = document.getElementById('store').value;
	window.location.href='/economy/gifts/playboy2009/sendgiftcontestant.php?tab='+store+'&backid='+backId+'&store='+store;
}

function removeandsendcontestant(){	
	var GiftName = document.getElementById('backgiftname').value;	
	var store = document.getElementById('store').value;
	var str = '';
	if(GiftName != 0){		
		str = '&namegift='+GiftName;
	}
	window.location.href='/economy/gifts/playboy2009/sendgiftcontestant.php?tab=contestant&store='+store+str;
	
}

function cancelPurchase(){
	window.location.href='/community/playboymexico.html';
}

function purchasegift(){
	var store = document.getElementById('store').value;
	var namegift = document.getElementById('backgiftname').value;
	var tab = document.getElementById('tab').value;
	var note = document.getElementById('commentText').value;
	var idcontestant = document.getElementById('backid').value;
	var giftid = document.getElementById('giftid').value;
	window.location.href='/economy/gifts/playboy2009/actions/purchasegift.php?store='+store+'&note='+note+'&idcontestant='+idcontestant+'&giftid='+giftid+'&tab='+tab+'&namegift='+namegift;
}

function addnote_Cont(obj){
	window.location.href=obj.href;

}