/*
+--------------------------------------------------------------------------
|   php Listing
|   ========================================
|   > file: js/submit.js
|	> verison : 0.1beta $
|   > $Author: danky pang $URL http://www.dankypang.com
+---------------------------------------------------------------------------
*/

var waitNote		= 'processing, please hold...';
var msgId		= '#errmsg';
var ajaxurl = '/ajax.php';


function get_title(){

	var msgId = ":input.fetchx";
	var urlId = "#url";
	var _u = $.trim($("#url").val());

	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	var matchArray=_u.match(regexp);

	if (matchArray==null){
	
		alert(1);
		$(urlId).css({ border: "1px solid red"});
		$(urlId).focus();
			
	}else{
	
		$(msgId).attr({ disabled: "disabled"});
		$(msgId).val("fetching...").fadeIn('slow',function(){
		
			$.post(ajaxurl,{act:'fetchx',u:_u},function(data){
				if(data.status==true){
					$("#name").val(data.t); 
					$("#description").val(data.d);
					$(msgId).attr({ enabled: "enabled"});
					$(msgId).val("fetch");
				}else{
					$(urlId).focus();
				}

			},'json');

	
		});
	}



}



function apply(cid){

	var msgId	='#editors';
	$(msgId).text(waitNote).slideUp('slow',function(){
	
		$.post(ajaxurl,{act:'apply',cid:cid},function(data){
			if(data.status==true){
				$(msgId).text(data.message).fadeIn('slow');
			}else{
				$(msgId).html(data.message).fadeIn('slow').delay(5000).fadeOut();	
			}

		},'json');

	});

}




function manage(type,id)
{
	var msgId			='.manage'+id;

	$(msgId).text(waitNote).slideUp('slow',function(){
	
		$.post(ajaxurl,{act:'manage',type:type,id:id},function(data){
			if(data.status==true){
				$(msgId).text(data.message).fadeIn('slow',function(){
					$(msgId).parent().slideUp('slow');
				});

			}else{
				$(msgId).text(data.message);	
			}

		},'json');
	
	});

}


//editme
$(document).ready(function() {

	$(".editme").click(function() {
		var v = $(this).html();
		var size = v.length + 10; 
		var i = $(this).attr("id");
		var Id = "#" + i;
		if ($(this).children('input').length == 0) {
			var inputbox = "<input type='text' class='inputbox' size='"+size+"' value=\""+$(this).text()+"\">";
			$(Id).html(inputbox);
			
			$("input.inputbox").focus();

			$("input.inputbox").blur(function() {
				var value = $(".inputbox").val();
				if(value.length > 0 && value != v){
					$.post(ajaxurl,{act:'editme',v:value,p:i},function(data){
						if(data.status==true)
						{
							$(Id).text(data.message);
						}else{$(Id).text(v);}
					
						}
					
					,"json");
				}else{$(Id).text(v);}
				
			});
		}
	});
});	



function suggest_blog()
{
	var _n = $.trim($("#name").val());	//
	var _u = $.trim($("#url").val());	// 
	var _d = $.trim($("#description").val());	// 
	var _v = $.trim($("#verify").val());
	var _t = $("#type").val();
	var _cid = $("#cid").val();

	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	var matchArray=_u.match(regexp);

	$(msgId).html(waitNote).slideDown('slow');//waitNote

		if (_n.length<2){$(msgId).html("The Blog Name must be between 3 and 20 characters.").fadeIn('slow',function(){$("#name").focus();});} 
		else{
		if (matchArray==null){$(msgId).html("Invalid url,please try again.").fadeIn('slow',function(){$("#url").focus();});}
		else{
			if (_d.length<10){$(msgId).html("The Blog Description must have more than 10 words.").fadeIn('slow',function(){$("#description").focus();});}
			else{
				if (_v.length<6){$(msgId).html("Please enter the 6 digit code shown above in image format.").fadeIn('slow',function(){$("#verify").focus();});}
				else{
						$(msgId).html(waitNote).fadeIn("slow",function()
						{
							$.post(ajaxurl,{act:_t,n: _n, u: _u, d: _d, v: _v, cid: _cid},function(data){

								if(data.status==true)
								{
								
									$("#w").html(data.message).slideDown().delay(5000).slideUp('slow',function(){});									
								}
								else
								{
									$(msgId).html(data.message).slideDown('slow', function(){$("#verify").focus();refresh_cap();});
								}				
						
							},"json");

						});
					}	
				}
								
			}
		
		}
}




function suggest_category()
{
	var _n = $.trim($("#name").val());	//
	var _d = $.trim($("#description").val());	// 
	var _v = $.trim($("#verify").val());
	var _l = $("#level").val();
	var _t = $("#type").val();
	var _cid = $("#cid").val();
	var _cpath = $("#cpath").val();

	$(msgId).html(waitNote).slideDown('slow');//waitNote

		if (_n.length<2){$(msgId).html("The Category Name must be between 3 and 20 characters.").fadeIn('slow',function(){$("#name").focus();});} 
		else{
			if (_d.length<10){$(msgId).html("The Category Description must have more than 10 words.").fadeIn('slow',function(){$("#description").focus();});}
			else{
				if (_v.length<6){$(msgId).html("Please enter the 6 digit code shown above in image format.").fadeIn('slow',function(){$("#verify").focus();});}
				else{
						$(msgId).html(waitNote).fadeIn("slow",function()
						{
							$.post(ajaxurl,{act:_t,n: _n, d: _d, v: _v, l: _l, cid: _cid, cpath: _cpath},function(data){

								if(data.status==true)
								{
								
									$("#w").html(data.message).slideDown().delay(5000).slideUp('slow');									
								}
								else
								{
									$(msgId).html(data.message).slideDown('slow', function(){$("#verify").focus();refresh_cap();});
								}				
						
							},"json");

						});
					}	
				
								
			}
		
		}
}




function signin(){

	var _u = $.trim($("#name").val());	//
	var _p = $.trim($("#p1").val());	//
	var _r = $("input").is(':checked');
	var _v = $.trim($("#verify").val());
	var n_regexp = /\W/;

	if (_u.length<2||_u.match(n_regexp)){$(msgId).html("The username must be between 3 and 20 characters.").fadeIn('slow',function(){$("#name").focus();});} 
	else{
		if (_p.length<6){$(msgId).html("The password must be between 3 and 20 characters.").fadeIn('slow',function(){$("#p1").focus();});}
		else{
			if (_v.length<6){$(msgId).html("Please enter the 6 digit code shown above in image format.").fadeIn('slow',function(){$("#verify").focus();});}
			else{
			
				$(msgId).html(waitNote).fadeIn("slow",function()
				{
					$.post(ajaxurl,{act:'signin',u: _u, p: _p, r:_r,v: _v},function(data){

						if(data.status==true){
								
							$("#w").html(data.message).slideDown().delay(2000).slideUp('slow',function(){
							
								window.location = './';
							});									
						}
						else{
							$(msgId).html(data.message).slideDown('slow', function(){$("#verify").focus();});
							refresh_cap();
						}				
						
					},"json");

				});				
					
			}	
		}
	}


}



function signup(){

	var _n = $.trim($("#name").val());	//
	var _e = $.trim($("#email").val());
	var _p1 = $.trim($("#p1").val());
	var _v = $.trim($("#verify").val());
	var _t = $("#type").val();
	var n_regexp = /\W/;
	var _p2 = $("#p2").val();
	
	var emailPat=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var matchArray=_e.match(emailPat);
	$(msgId).html(waitNote).slideDown('slow');//waitNote

	if (_n.length<3||_n.match(n_regexp)){$(msgId).html("The username must be between 3 and 20 characters([0-9a-Z]).").fadeIn('slow',function(){$("#name").focus();});} 
		else{if (matchArray==null){$(msgId).html("The email address you entered is invalid (ex: name@domain.com)<br /> or contains illegal characters: [ ] ; # &amp; ! *  &quot; &lt; &gt; % ( ) { } ? &#092;").fadeIn('slow',function(){$("#email").focus();});}
			else{if (_p1.length<6){$(msgId).html("The password must be between 3 and 20 characters.").fadeIn('slow',function(){$("#p1").focus();});}
				else{if (_p1 != _p2){$(msgId).html("The passwords do not match!").fadeIn('slow',function(){$("#p1").focus();});}
					else{if (_v.length<6){$(msgId).html("Please enter the 6 digit code shown above in image format.").fadeIn('slow',function(){$("#verify").focus();});}
						else{

								$(msgId).html('please hold on , processing...').fadeIn("slow",function(){
									$.post(ajaxurl,{act:'signup',n: _n, p: _p1,  v: _v, e: _e },function(data){
										if(data.status==true){
								
											$("#w").html(data.message).slideDown().delay(5000).slideUp('slow',function(){});									
										}
										else{
											$(msgId).html(data.message).slideDown('slow', function(){$("#verify").focus();refresh_cap();});
										}				
						
									},"json");

								});
							}	
					}	
				}					
			}
		}
}



















//关闭
function closex()
{
	var tId = "#w";
	$(tId).slideUp('slow');
}

function refresh_cap()
{
	var timenow = new Date().getTime();
	$("#verify_img").attr("src", '/cap/cap.php?'+timenow);
}



function removeSpaces(string) {
 return string.split(' ').join('');
}



function showform(t,i)
{
	var tId = "#w";

	$(tId).html(waitNote).slideDown('slow',function(){
	
		$.post(ajaxurl,{act:'showform', type : t , cid : i }, function(data){

		$(tId).html(waitNote).slideUp('slow',function(){
		
			$(this).html(data).slideDown('slow');
		})
	
	},"html")

	
	});
}
