//<script>

var popupAbsolute = false;

  Search = {
	fillDropdown: function( type, id, div, width, child )
	{
		var child=child||'';
		$.ajax({
			url:'/ajax/ajax_dropdown.php?type=' + type + '&id=' + id + '&div=' + div + '&width=' + width + '&child=' + child,
			success:function( str )
			{
				var loc = str.indexOf( ':' );
				var divname = str.substring( 0, loc );
				var divcontents = str.substring( loc + 1 );
				$( '#'+divname ).html(divcontents);
			}
		});
	},

	inviteEmail: function( email )
	{
		$('#inviteNow').html('(Please wait...)');
		$.ajax({
			type: 'POST',
			url: '/invitations/invites.php',
			data: 'step2=1&address='+email,
			success: function( res )
			{
				$('#inviteNow').html('<b>'+res+'</b>');
			}
		});
	},

	sendSecretAdmire: function( userid )
	{
    if( popupAbsolute == true )
    {
      $('#quickSend').css({ left: '10px', top: '10px' });
    }
    else
    {
      if( (posY - 80) < 0 )
      {
        posY = 80;
      }
      $('#quickSend').css({ left: (posX+10)+'px', top: (posY-80)+'px' });
    }
		$.ajax({
			type: 'POST',
			url: '/apps/match/ajax/admire/',
			data: 'page=profile&userid='+parseInt(userid),
			complete: function( str )
			{
				$('#reason').html('');
				$('#quickSendMsg').html( str.responseText );
				$('#quickSend').show();
			}
		});
	},

	showReason: function( type, userid, firstname )
	{
    captchaCode = "";
		if( type == 'friend' )
		{
			$.ajax({
				type: 'POST',
				url: '/ajax/profile/addfriend.php',
				data: 'action=check&u='+userid,
				dataType: 'script'
			});
			$('#quickSendMsg').html('<span style="font-weight:normal">Loading...</span>');
			$('#reason').html('');
      if( popupAbsolute == true )
      {
      $('#quickSend').css({ left: '10px', top: '10px' }).show();
      }
      else
      {
        if( ( posY - 80 ) < 0 )
        {
          posY = 80;
        }
			$('#quickSend').css({ left: (posX+10)+'px', top: (posY-80)+'px' }).show();
      }
		}
    else if( type == 'removeFriend' )
    {
      $('#reason').html('<div style="text-align: center"><a href="#" onclick="removeFriendship('+"'"+userid+"'"+');document.getElementById('+"'quickSend'"+').style.display='+"'none'"+'; return false;"><img src="' + IMAGE_URL +'/btn_remove_friend2.gif" border=0></a><div>');
      $('#quickSendMsg').html('Are you sure you want to remove your friendship with '+firstname+'?');
      if( popupAbsolute == true )
      {
      $('#quickSend').css({ left: '10px', top: '10px' }).show();
      }
      else
      {
        if( ( posY - 80 ) < 0 )
        {
          posY = 80;
        }
			$('#quickSend').css({ left: (posX+10)+'px', top: (posY-80)+'px' }).show();
      }
    }
    else
    {
      if( type=='highfive' )
      {
        sendText = 'give ' + firstname + ' a high five';
      }
      else if( type=='flirt' )
      {
        sendText = 'flirt with ' + firstname;
      }
      else if( type=='goldstar' )
      {
        sendText = 'give ' + firstname + ' a gold star';
      }
      sendCaptcha = pageCaptcha;

      if( sendCaptcha )
      {
        var dateval = new Date();
        var ts = dateval.getTime();
        captchaCode = '<td></td></tr><tr><td colspan="2">Please enter the text shown:</td></tr><tr><td><img src="' + SITEURL + '/popup_captcha.php?' + ts + '" id="captchaImage" /></td><td><input name="captcha" id="captcha" maxlength="10" size="10" class="textbox" /><br><a href="#" onclick="Search.refreshCaptcha(); return false;" class="normaltextlink" style="color: #009;">Refresh text</a></td></tr><tr>';
      }
        

      $('#reason').html('<form name="reasonwhy" action="" onsubmit="return false;"><table><tr><td colspan="2"><input name="reasontext" id="reasontext" type="text" maxlength="100" size="40" class="textbox" /></td></tr><tr><td><input type="checkbox" name="private" id="private" value="private">Keep reason private</td>' + captchaCode +'<td style="text-align:right"><img src="' + IMAGE_URL + '/btn_submit.gif" onclick="return Search.sendItem(' + "'" + userid + "','" + type + "'" + ');" style="cursor: pointer;"></td></tr></table></form>');
      $('#quickSendMsg').html('I want to ' + sendText + ' because... (optional)');
      if( popupAbsolute == true )
      {
        $('#quickSend').css({ left: '10px', top: '10px' }).show();
      }
      else
      {
        if( ( posY - 80 ) < 0 )
        {
          posY = 80;
        }
			posLeft = ( posX > 400 ? posX-200 : posX+10 );
        $('#quickSend').css({ left: posLeft+'px', top: (posY-80)+'px' }).show();      
      }

    }
  },

  sendFriendRequest: function( userid, email )
  {
    var email = email || '';
    $('#quickSendMsg').html("Sending Friend request...");
    $('#reason').html('');
    $.ajax({
      type: 'POST',
      url: '/ajax/profile/addfriend.php',
      data: 'u='+userid+'&email='+email,
      success: function( xhr )
      {
        $('#reason').html('');
        $('#quickSendMsg').html(xhr);
        $('#quickSend').show();
      }
    });
  },

  sendItem: function( userid, type )
  {
    if( $('#captcha').val() != "")
    {
      $('#quickSend').hide();
      poststr = 'userid='+parseInt(userid)+'&reason='+$('#reasontext').val();
      if( $('#private').attr('checked') == true )
      {
        poststr += '&private=1';	
      }
      else
      {
        poststr += '&private=0';
      }

      if( type=='goldstar' )
      {
        if( pageCaptcha == true )
        {
          poststr += "&captcha=" + $('#captcha').val();
        }
        postURL = '/ajax/profile/givegoldstar.php';
      }
      else if( type=='highfive' )
      {
        if( pageCaptcha == true )
        {
          poststr += "&captcha=" + $('#captcha').val();
        }
        postURL = '/ajax/profile/givehighfive.php';
      }
      else if( type=='flirt' )
      {
        if( pageCaptcha == true )
        {
          poststr += "&captcha=" + $('#captcha').val();
        }
        postURL = '/ajax/profile/giveflirt.php';
      }

      $.ajax({
        type: 'POST',
        url: postURL,
        data: poststr,
        dataType: 'json',
        success: function( data ){
          $('#reason').html('');
          $('#quickSendMsg').html( data.msg );
          $('#quickSend').show();
          pageCaptcha = data.captcha;
        }
      });
    }
    else
    {
      return false;
    }
  },


  showQMForm: function( userid, firstname )
  {
    captchaCode = "";
    if( pageCaptcha )
    {
      var dateval = new Date();
      var ts = dateval.getTime();
      captchaCode = '<table align="center"><tr><td colspan="2">Please enter the text shown:</td></tr><tr><td><img src="' + SITEURL + '/popup_captcha.php?' + ts + '" id="captchaImage" /></td><td><input name="captcha" id="captcha" maxlength="10" size="10" class="textbox" /><br><a href="#" onclick="Search.refreshCaptcha(); return false;" class="normaltextlink" style="color:#009;">Refresh text</a></td></tr></table>';
    }
    
    $('#quickSendMsg').html('Message to '+firstname+':');
    $('#reason').html('<form id="qmForm" action=""><div style="text-align:center;padding-top:5px;"><input type="text" style="width:250px;margin-bottom:4px;" class="textbox" name="qmsubject" onFocus="Search.checkQMSubject(this,true);" onBlur="Search.checkQMSubject(this,false);" value="(No Subject)"><br><textarea style="width:250px;" class="textbox" name="qmbody" id="qmbody" rows="4"></textarea><br>' + captchaCode + 
        '<img src="' + IMAGE_URL + '/btn_send.gif" name="sendMessage" style="padding-top:6px; cursor: pointer;" class="normaltextlinkbold" alt="Send" onclick="Search.sendQuickMessage()"></div><input type="hidden" name="userid" value="'+userid+'"></form>');
    if( popupAbsolute == true )
    {
      $('#quickSend').css({ left: '10px', top: '10px' }).show();
    }
    else 
    {
        if( ( posY - 80 ) < 0 )
        {
          posY = 80;
        }
			posLeft = ( posX > 400 ? posX-200 : posX+10 );
      $('#quickSend').css({ left: posLeft+'px', top: (posY-80)+'px' }).show();
    }
    $('embed, object').hide();
		$('#quickSend img').click(function(){
			$('embed, object').show();
		});

  },

  sendQuickMessage: function( )
  {  
    if( $('#captcha').val() != "" && $('#qmbody').val() != "" )
    {
      $('#quickSend').hide();
      $.ajax({
        type: 'POST',
        url: '/ajax/ajax_myfriendsupdate.php',
        data: $('#qmForm :input').serialize()+'&type=quickmessage&token='+$('#token').val(),
        dataType: 'json',
        success: function( data )
        {
          pageCaptcha = data.captcha;
          $('#reason').html('');
          $('#quickSendMsg').html( data.msg );
          $('#quickSend').show();
        }
      });
    }
  },

  checkQMSubject: function( s, focus )
  {
    if( focus )
    {
      if( s.value == '(No Subject)' )
      {
        s.value = '';
      }
    }
    else
    {
      if( s.value == '' )
      {
        s.value = '(No Subject)';
      }
    }
  },

  autographPopup: function(u,name,convo,from){
    if(convo==1){
      var link='<a href="/?mysession='+ 
        encodeBase64('listing_conversation_view_autographs&postedto='+u+'&postedby=' + from ) + '" class="normaltextlink">View Autograph Conversation</a>';
    }
    else{
      var link='';
    }

    captchaCode = ""
    if( pageCaptcha )
    {
      var dateval = new Date();
      var ts = dateval.getTime();
      captchaCode = '<table align="center"><tr><td colspan="2">Please enter the text shown:</td></tr><tr><td><img src="' + SITEURL + '/popup_captcha.php?' + ts + '" id="captchaImage" /></td><td><input name="captcha" id="captcha" maxlength="10" size="10" class="textbox" /><br><a href="#" onclick="Search.refreshCaptcha(); return false;" class="normaltextlink" style="color:#009">Refresh text</a></td></tr></table>';
    }

    $("#quickSendMsg").html("Sign "+name+"'s yearbook:");
    $("#reason").html('<div style="width:80%;margin:auto;"><textarea id="autographText" style="width:100%;height:120px;margin-top:5px;" class="textbox"></textarea>' + captchaCode + '<div style="margin-top:5px"><img src="' + IMAGE_URL + '/btn_submit.gif" style="cursor:pointer;float:right;" onclick="Search.sendAutograph('+ "'" + u + "'" + ');">'+link+'</div></div>');
    if( popupAbsolute == true )
    {
      $('#quickSend').css({ left: '10px', top: '10px' }).show();
    }
    else
    {
        if( ( posY - 80 ) < 0 )
        {
          posY = 80;
        }
      $('#quickSend').css({left:(posX-165)+'px',top:(posY-80)+'px'}).show();
    }
  },

  sendAutograph: function(u)
  {
    if( $('#captcha').val() != "")
    {
      $('#quickSend').hide();
      dataString = 'user='+u+'&content='+escape($('#autographText').val());
      if( pageCaptcha == true )
      {
        dataString += "&captcha=" + $('#captcha').val();
      }
      $.ajax({
        type:'POST',
        url:'/ajax/profile/autograph.php',
        data: dataString,
        dataType: 'json',
        success:
          function( data )
          {
            pageCaptcha = data.captcha;
            $('#reason').html('');
            $('#quickSendMsg').html( data.msg );
            $('#quickSend').show();
          }
      });
    }
  },

  giftPopup: function(to,from,page,isnew){
    $('#reason').html('');
	   
    dataString = 'toUser=' + to + '&cat=' + $('#cat').val() + '&pmax=' + $('#pmax').val() + '&order=' + $('#order').val() + '&page=' + page;
    $.ajax({
        type:'POST',
        url:'/ajax/gifts/displayGifts.php',
        data: dataString,
        dataType: 'json',
        success:
          function( data )
          {
            if( popupAbsolute == true )
    		{
      			$('#giftSend').css({ left: '10px', top: '10px' }).show("slow");
    		}
    		else
    		{
	          	if( isnew == true )
    	      	{
    	      		if(document.all)
					{
						fHeight = document.body.clientHeight;
					}
					else if(document.getElementById &&!document.all)
					{
						fHeight = document.body.offsetHeight;
					}
	      			if( ( posY - 120 ) < 0 )
	      			{
			          posY = 120;
			        }
			        if(fHeight>840)
			        {
				        if(posY + 760 > fHeight)
				        {
				          posY = fHeight - 760;
				        }
			        }
			        if (jQuery.browser.msie) 
					{
  						if(parseInt(jQuery.browser.version) == 6)
  						{
  							$('select').hide();
  						}
					}
			        if($('#viewallgifts').length)
			        {
			        	posY = 300;
			        }
				    $('#giftSend').css({left:'120px',top:(posY-80)+'px'}).show("slow");
    	      	}
    	   	}
    	   	pageCaptcha = data.captcha;
       	  	captchaCode = ""
    		if( pageCaptcha )
    		{
			      var dateval = new Date();
			      var ts = dateval.getTime();
			      captchaCode = '<table align="left"><tr><td colspan="2">Please enter the text shown:</td></tr><tr><td><img src="' + SITEURL + '/popup_captcha.php?' + ts + '" id="captchaImage" /></td><td><input name="captcha" id="captcha" maxlength="10" size="10" class="textbox" /><br><a href="#" onclick="Search.refreshCaptcha(); return false;" class="normaltextlink" style="color:#009">Refresh text</a></td></tr></table>';
    		}
            $('embed, object').hide();
       		$('#messageArea').html(''); 
			$('#giftArea').html( unescape(data.output.replace(/\+/g,' ')) );
			Search.makeCurtain( posY + 580);
    		$('#captchaSpot').html( captchaCode );
		      
	      }
      });
  },
  
  
sendGift: function()
  {
  	var selectedGiftPrice = parseInt( $('#selectedGiftPrice').val() );
  	var totalMoney = parseInt( $('#totalMoney').val() );
  	if($('#selectedGift').val() !='' && $('#selectedGiftPrice').val() !='')
  	{
  		if( selectedGiftPrice  <= totalMoney  )
  		{
		  	$('#loadingArea').show();
  		  	$('#Submit').hide();
			dataString = 'toUser=' + parseInt($('#toUser').val()) + '&giftid=' + parseInt($('#selectedGift').val()) + '&messageText=' + $('#giftMessage').val() + '&privacyValue=' + parseInt($('input[@name=giftPrivacy]:checked').val()) + '&giftTok=' + $('#giftTok').val();
  			
  			
  	  		if( pageCaptcha == true )
      		{
        		dataString += "&captcha=" + $('#captcha').val();
      		}
      		$.ajax({
		        type:'POST',
		        url:'/ajax/gifts/giveGift.php',
		        data: dataString,
		        dataType: 'json',
		        success:
		          function( data )
		          {
		            pageCaptcha = data.captcha;
				  	$('#loadingArea').hide();
				  	$('#Submit').show();

		            if(data.success)
		          	{
		          		var newTotal = (totalMoney - selectedGiftPrice)/100;
		          		newTotal = newTotal.toFixed(2);
		          		newTotal = Search.commify(newTotal);
		          		$('#moneyDiv').html(newTotal); 
		          		$('#formArea').html(unescape(data.msg.replace(/\+/g,' ')));
		          		$('#messageArea').html(''); 
		          	}
		          	else
		          	{
		       	  		if( pageCaptcha == true )
      					{
		          			Search.refreshCaptcha();
      					}
		          		$('#messageArea').html(unescape(data.msg.replace(/\+/g,' ')));
		          	}             
			      }
		      });
  		}
  	  	else
  		{
  			$('#messageArea').html('You can\'t afford this Gift!'); 
  		}
  		
  	}
  	else
  	{
  		$('#messageArea').html('Please Select a Gift'); 
  	}
  },
  
  commify: function(Num) 
  { 
    var newNum = "";
    var newNum2 = "";
    var count = 0;
	
	if (Num.indexOf('.') != -1)
	{ 
	    if (Num.indexOf('.') == Num.length-1)
	    {
	        Num += "00";
	    }
	    if (Num.indexOf('.') == Num.length-2)
	    {
	        Num += "0";
	    }
	    
	    var a = Num.split("."); 
	    Num = a[0];   //the part we will commify
	    var end = a[1] //the decimal place we will ignore and add back later
	}
	else {var end = "00";}  
	
	//this loop actually adds the commas   
	for (var k = Num.length-1; k >= 0; k--)
	{
	  var oneChar = Num.charAt(k);
	  if (count == 3)
	  {
	    newNum += ",";
	    newNum += oneChar;
	    count = 1;
	    continue;
	  }
	  else 
	  {
	    newNum += oneChar;
	    count ++;
	  }
	}  	
	for (var k = newNum.length-1; k >= 0; k--)
	{
	  var oneChar = newNum.charAt(k);
	  newNum2 += oneChar;
	}
	
	 newNum2 = newNum2 + "." + end;
	
	 return(newNum2); 
  },
  
  hideGifts: function()
  {
  	$('#giftArea').html('');
  	$('#giftSend').hide();
    $('embed, object').show();
	$('select').show();
	Search.destroyCurtain();

  },
  
  darken: function(theDiv)
  {
  	$('#main' + theDiv).css('border-color', '#bcd2e5');
  },

  lighten: function(theDiv)
  {
  	$('#main' + theDiv).css('border-color', '#f2f5f7');
  },
  
  
  selectGift: function(giftID, giftPrice)
  {
  	$('#main' + giftID).css('background-color', '#bcd2e5');	
	prevGiftValue = $('#selectedGift').val();
	if( prevGiftValue != '') 
	{
	  $('#main' + prevGiftValue).css('background-color','#f2f5f7'); 		
	}
	if( prevGiftValue == giftID )
	{
		$('#selectedGift').val('');
		$('#selectedGiftPrice').val('');
	}
	else
	{
		$('#selectedGift').val(giftID);
		$('#selectedGiftPrice').val(giftPrice);
	}
  },
  
  makeCurtain: function(posY) {
  	window.scroll( 0, parseInt($('#giftSend').css('top')));
  	if(!(parseInt($('#DD_curtain').height()) > 0))
  	{
  		if(document.all)
		{
			fHeight = document.body.clientHeight;
		}
		else if(document.getElementById &&!document.all)
		{
			fHeight = document.body.offsetHeight;
		}
		if(fHeight < posY)
		{
			fHeight = posY;
		}	        
		
		var cur = $('<div />').attr('id', 'DD_curtain').addClass('DD_curtain').css({ top: 0, left: 0, width: '100%', height: fHeight, 'z-index': ( 100 ) }).appendTo('body');
  	}
		
  },
  
  destroyCurtain: function() {
	$('#DD_curtain').remove();
  },

  refreshCaptcha: function()
  {
    var dateval = new Date();
    var ts = dateval.getTime();
    $('#captchaImage').attr( 'src', SITEURL + '/popup_captcha.php?' + ts );
  }

}
var posX;
var posY;
$(document).mousemove(function(e){
    posX = e.pageX;
    posY = e.pageY;
    });
