// PGTTS traffic trade system
	var ac;
	var timeoutId;
	$(document).ready(function() {

		
	 	ac = $("#gametitle").autocomplete("pgtts.ajax.search.php", { cellSeparator:"|",minChars:1 ,onItemSelect:selectItem});
	
	});


		
	function getGame(gameid) {
		
		$.blockUI({ message: '<h1><img src="/images/pgtts/ajax-loader.gif" /> <br /><center>Loading game...</center></h1>' });
		
		$.getJSON("/pgtts.ajax.php?action=getgameinfo&gameid="+gameid,
			function(data) {
				if(data['message'] == 'success') {
					// display game thumbs
											
					$.each(data['game'], function(i,item) {
						var content = "<div name='game_"+gameid+"'>";
							content += "<div style='float:left'><img src='/games/images/"+item.gameicon+"' width='80' height='60' /></div>";
							content += "<div style='float:left;margin-left:12px;'><div><b>"+item.gametitle+"</b></div><div><input type='text' name='txt_game_"+gameid+"' value='"+$("input[name=websiteurl]").val()+"' size='80'/></div></div>";
							content += "<div style='float:left;margin-left:12px;'><div style='line-height:16px'>&nbsp;</div><input type='button'  value='remove' onClick='removeSelection("+gameid+")'/></div>";								
							content += "<input type='hidden' name='hdn_game_"+gameid+"' value='"+gameid+"'/>";
							content += "<div style='clear:both'>&nbsp;</div>";
							content += "</div>";
						$("[name=dtrade]").append(content);
					});
				}
				else 
					if(data['reason'] !== undefined) 
						alert(data['reason']);
					else
						alert("An error occured");
				
				// unblock user interface
				$.unblockUI();							
			}
		);			
				
	}
	
		
	// remove a game previously selected
	function removeSelection(gameid) {
		$("[name=game_"+gameid+"]").remove();
	}

	// on game selected
	function selectItem(li) {
		if( li == null ) 
			return alert("No match Found!");
	
		// if coming from an AJAX call, let's use the gameid
		if( !!li.extra ) { 
			var sValue = li.extra[0];
		}
		// otherwise, let's just display the value in the text box
		else 
			var sValue = li.selectValue;
			
		getGame(sValue);
		// empty search box
		$("#gametitle").val('');
		
	}
	

	
	function validate(f) {
		if(!f.websitename.value.length) {
			alert("At least you should consider give us your website name!");
			return false;
		}
		if(!f.websiteurl.value.length) {
			alert("At least you should consider give us your website url!");
			return false;
		}		
		
		// add http:// in front if omited
		
		if(f.websiteurl.value.indexOf("http://") == -1)
			f.websiteurl.value = "http://"+f.websiteurl.value;

		if(!f.verifycode.value) {
			alert("Please, fill the verification code");
			return false;		
		}
								
		if(!emailCheck(f.emailaddress.value)) {
			alert("E-mail address is invalid");				
			f.emailaddress.focus();
			return false;
		}	
		
		// verify the gameplug
		found_one=false;
		$("input[name^=txt_game]").each(function(i,item) {
			if(item.value.length>7) {
				found_one=true;
			}
		});

		if(!found_one) {
			alert("Please add at least one game plug in order to exchange traffic. The more you set, the more chance you have to receive traffic!");
			return false;
		}
		
		sendSignup();
	}


	function sendSignup() {
		str = '';
		$("input").each(function(i,item) {
				str +="&"+$(this).serialize();
		});
		$.blockUI({ css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
        } ,message:"Your Link Has Been Added! Start sending traffic to any part of our website and your clicks will be tracked. Please exit out of this screen"});
		
		$.getJSON("/pgtts.ajax.php?action=pgttslinkadded&"+str,function(data) {
			if(data['message'] == 'success') {
				$.blockUI({ css: { 
					border: 'none', 
					padding: '15px', 
					backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: '.5', 
					color: '#fff' ,
					'text-align': 'justify'
				} ,message:data['reason']});		
			
				timeoutId = setTimeout("unBlockUI(\""+data['url']+"\")",15000);

			}
			else {
				
				if(data['reason'] !== undefined) {
					$.blockUI({ css: { 
						border: 'none', 
						padding: '15px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '10px', 
						'-moz-border-radius': '10px', 
						opacity: '.5', 
						color: '#fff' 
					} ,message:data['reason']});

				}
				else {
					$.blockUI({ css: { 
						border: 'none', 
						padding: '15px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '10px', 
						'-moz-border-radius': '10px', 
						opacity: '.5', 
						color: '#fff' 
					} ,message:"An Error Occured, Please Try Again!"});					
				}
				timeoutId = setTimeout("unBlockUI('')",4000);
			}
																		
		});
	}
	
	function unBlockUI(msg) {
		// unblock user interface
		$.unblockUI();			
		$("#secretcode").html(msg);
		$("#secretcode").show("slow");			
		$("input[name=submit]").attr("disabled",'true');
		clearTimeout(timeoutId);
	}
	function emailCheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
			return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			return false
		 }
		
		 if (str.indexOf(" ")!=-1){
			return false
		 }

		 return true					
		
	}
	
