$(document).ready(function() {			
	//Input Box BG Color
	$("input,select,textarea").focus(function() {
		//$(this).css({'background-color' : 'yellow'});
		$(this).addClass("bgyellow");				
	});
	
	$("input,select,textarea").blur(function () {
		$(this).removeClass("bgyellow");	
	});	
	
	//Ordering / Sorting
	var ser_field = $(".filter #ser_field").val();
	var ser_type = $(".filter #ser_type").val();
	if(ser_field) {
		$("a[name='"+ser_field+"']").addClass(ser_type);				
	}
	$(".list-header a").click(function() {									  
		if(!$(this).attr("class")) {
			$("#ser_type").val("");
		}
		$(".list-header a").removeClass("desc");
		$(".list-header a").removeClass("asc");
		var field=$(this).attr("name");		
		$("#ser_field").val(field);	
		$("#ser_type").val()=="asc" ? $("#ser_type").val("desc") : $("#ser_type").val("asc");			
		$(this).addClass($("#ser_type").val());
		filter();
	});
	
	//Check & Uncheck Checkboxes
	$("input[name=checkall]").click(function() {		
		$("input[name=checkall]").is(":checked") ? $("input[name=check]").attr("checked", true) : $("input[name=check]").attr("checked", false);			
	});
	
	//Filter
	$(".filter input[type=button]").click(function() {
		filter();
	});
	$(".filter select").change(function() {
		filter();
	});
	
	//Pirobox
	if($("a[class^=pirobox]").attr("class")) {
		$().piroBox({
				my_speed: 300,
				bg_alpha: 0.5,
				radius: 4,
				pirobox_next : 'piro_next',
				pirobox_prev : 'piro_prev',
				close_all : '.piro_close,.piro_overlay',
				slideShow : 'slideshow',
				slideSpeed : 3
		});
	}

	if($("input[name='uploadify']").attr("name")) {
		if(page=="product") {
			$.ajaxify({				
				crop		: false,
				thumbWidth	: '90~120~170~640',
				thumbHeight	: '68~90~128~480',
				cminSize	: [90,68],
				csetSelect	: [0,0,170,128]				
			});		
		} else {
			$.ajaxify();					
		}		
	}
	
	//CK Editor	
	if($("textarea.editor").attr("id")) {
		$("textarea.editor").ckeditor();	
	}
	
	
	// CKEditor Encryption / Decryption
	if($("textarea.editor").attr("id")) {
		$("textarea.editor").val($.rc4DecryptStr($("textarea.editor").val(),""));
	}

	if($(".ckdescrip").html()) {
		if($.rc4DecryptStr($(".ckdescrip").html(),"")=="<br />\n") {
			$(".ckdescrip").html("<span class='error'>Content not available</span>");
		} else {
			$(".ckdescrip").html($.rc4DecryptStr($(".ckdescrip").html(),""));
		}
		$(".ckdescrip").css("display","block");
	}
	
	if($(".ckdescrip_art").html()) {
		if($.rc4DecryptStr($(".ckdescrip_art").html(),"")=="<br />\n") {
			$(".ckdescrip_art").html("<span class='error'>Content not available</span>");
		} else {
			$(".ckdescrip_art").html($.rc4DecryptStr($(".ckdescrip_art").html(),""));
		}
	}
	
	if($(".ckcontainer").html()) {
		if($.rc4DecryptStr($(".ckcontainer").html(),"")=="<br />\n") {
			$(".ckcontainer").html("<span class='error'>Content not available</span>");
		} else {
			$(".ckcontainer").html($.rc4DecryptStr($(".ckcontainer").html(),""));
		}
		$(".ckcontainer").css("display","block");
	}
	
	//Set Class for all input buttons
	$("input[type='button']").addClass("stylebutton");
	
	$("input[type='button']").hover(function() {
		$(this).addClass("stylebutton-h");										 
	},function() {
		$(this).removeClass("stylebutton-h");	
	});
	$("#pmt_clear_button").click(function(){
		var allId="email`first_name`lname`telephone`mobile`zipcode`country_name`state_name`city_name";
		clearForm('', allId,"",allId);
		$(".middle").html("<span></span>");
	});									 
	$("#pmt_frm_btn").click(function(){
		var allId="email`first_name`lname`telephone`mobile`zipcode`country_name`state_name`city_name";
		clearForm('', allId,"");
		$(".middle").html("<span></span>");
		
		var flag = $.validate({
			email		: {required:true, email: true},
			telephone		: {required:true, telephone: true},
			first_name		: {required:true},
			zipcode		: {required:true},
			country_name		: {required:true},
			state_name		: {required:true},
			city_name		: {required:true}
		});
		if(flag) {		
			var value = getData(allId);
			$.ajax({
				type: "POST",
				url: base_path+"ajax_redirect.php?page=payment&option=setPayment",
				dataType: "html",
				data: "data="+value,
				beforeSend: function() {
					callProcessing();
				},
				success: function(data) {
					$(".middle span").html(data);
					document.forms[1].action = base_path+"payment";
					document.forms[1].submit();
				}			
			});
		} else {
			//$("#result").html("<span class='error'>"+ajax_misfield+"</span>");	
		}
		
		
	});
	if($(".ckdetails").html()) {
		if($.rc4DecryptStr($(".ckdetails").html(),"")=="<br />\n") {
			$(".ckdetails").html("<span class='error'>Content not available</span>");
		} else {
			$(".ckdetails").html($.rc4DecryptStr($(".ckdetails").html(),""));
		}
		$(".ckdetails").css("display","block");
	}

	docReady();	
	onlyNumber();	
	$("#linkhurl a[href^='http']").attr('target','_blank');
	$("#featuredlay a[href^='http']").attr('target','_blank');
	$(".footer_dev a[href^='http']").attr('target','_blank');
	$("#rss_home_href a[href^='http']").attr('target','_blank');
	$(".art-ursp a[href^='http']").attr('target','_blank');
	
	
});

function docReady() {
	$("a.targetbl").click(function() {
		var url=$(this).attr("name");
		window.open("http://"+url);
	});
	
	$("a.edit").unbind('click');		
	$("a.edit").bind('click', function() {
		var id = $(this).attr("name");
		$.post(base_path+"ajax_redirect.php?page=action&option=setSessionId", { data: id},function(data) {
			if(data==1) {
				location.href=base_path+"myaccount/edit";
			}
		});		
	});
	
	if(page == "payment"){
		document.form.submit();	
	}
	setHeight();
}

function setHeight() {
	//Set Height
	$(".redirect").css("height","auto");
	var sub_height = $(".page-header").height()+$(".menu").height()+$(".header-bottom").height();	
	var set_height = $(window).height()-sub_height;		
	set_height = set_height-46;	
	var org_height = $(".redirect").height();	
	if(org_height < set_height) {				
		$(".redirect").css("height",set_height);
	} else {		
		$(".redirect").css("height","auto");		
	}	
}

function onlyNumber() {
	$("#salary").keypress(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if((code<46 || code >57) && (code<35 || code >40) && code!=8 && code!=13 && code!=9) {
			e.preventDefault();	
		}
	});
}

function keyDownRegister(evt,fl) {
	if(document.all) {
		if(event.keyCode==13) {
			getParam(fl);
		}
	} else {
		if(evt.which==13) {
			getParam(fl);
		}
	}
}

function keyDownUser(evt,fun) {	
	if(document.all) {
		if(event.keyCode==13) {
			fun;
		}
	} else {
		if(evt.which==13) {
			fun;
		}
	}
}

function keyDownForpass(evt) {	
	if(document.all) {
		if(event.keyCode==13) {
			forgotEmail();
		}
	} else {
		if(evt.which==13) {
			forgotEmail();
		}
	}
}




function sendContactUs() {
	var flag = true;
	$("#result").html("");
	var allId="name`email`mobile`message";
	clearForm('', allId,"");	
	var norId = allId;
	var funId = "email`mobile";
	var valFun = "vemail`vtelephone";
	
	var flag = $.validate({
		name: {required: true},
		email: {required: true, email:true},		
		message: {required: true}
	});
	
	
	var value = getData(allId);
	if(flag) {		
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page="+page+"&option=sendContactUs",
			dataType: "html",
			data: "data="+value,
			beforeSend: function() {
				callProcessing();
			},
			success: function(data) {				
				$(".middle").html(data);
				setTimeout("location.href='"+base_path+"contact-us'",700);
			}			
 		});
	} else {		
		//$("#result").html("<span class='error'>"+ajax_misfield+"</span>");	
	}	
}

//Getting the Checkbox Value
function getCheckboxValue() {
	var val = [];
	$("input[name=check]:checked").each(function(i) {
    	val[i] = $(this).val();
    });	
	val=val.join("~");
	return val;
}

//Getting the value of filter fields
function getFilterValue() {
	var val = [];
	$(".filter input[type!=button],.filter select").each(function(i) {
		var value = $(this).val();
		value = value.replace(/&/g, "%26");
    	val[i] = value;
    });	
	val=val.join("`");
	return val;
}

function getSesFilterValue() {
	var val = [];
	var id = [];
	$(".filter input[type!=button],.filter select").each(function(i) {
		var value = $(this).val();
		value = value.replace(/&/g, "%26");
    	val[i] = value;		
		id[i] = $(this).attr("id");
    });	
	if(val) {		
		var len = id.length;
		val[len] = page;
		id[len] = "page";
	}
	val = val.join("`");
	id = id.join("`");
	return id+"~"+val;
}
//Searching
function filter(indx) {
	if(!indx || indx<0){
		indx = 0;
	}
	$("#indx").val(indx);
	var value = getFilterValue();
	var ses_value = getSesFilterValue();	
	$.post(base_path+"ajax_redirect.php?page=action&option=setSession", { data: ses_value});
	$.ajax({
		type: "POST",
		url: base_path+"ajax_redirect.php?page="+page+"&option=filter",
		dataType: "html",
		data: "data="+value,
		beforeSend: function() {
			$("#nav_process").html("<img src='"+base_path+"images/site-admin/nav_loader.gif' alt='' />");
		},
		success: function(data) {			
			$("#layout").html(data);
			docReady();
			if(page=="portfolio") {
				$.enableEdit();	
			}
		}			
	});	
}

function enable(id,status) {
	var table = $("#table").val();
	var value=table+"`"+id+"`"+status;
	var indx=$("#indx").val();
	$.ajax({
		type: "POST",
		url: base_path+"ajax_redirect.php?page=action&option=enable",
		dataType: "html",
		data: "data="+value,
		success: function(data) {
			filter(indx);	
		}			
	});
}

function del(folder) {
	var id = getCheckboxValue();
	var table = $("#table").val();
	var check_total=$("input[name=check]").size();
	var checked_total=$("input[name=check]:checked").size();
	var indx=$("#indx").val();
	if(check_total==checked_total) {
		$("#indx").val(indx-1)	
	}
	if(id) {
		var cnf = confirm("Do you want to delete this record?");
		if(cnf) {
			var value=table+"`"+id+"`"+folder;			
			$.ajax({
				type: "POST",
				url: base_path+"ajax_redirect.php?page=action&option=del",
				dataType: "html",
				data: "data="+value,
				success: function(data) {
					indx=$("#indx").val();
					filter(indx);	
				}			
			});
		}
	} else {
		alert("Select atleast one record to delete");	
	}
}


function callRedirect() {
	if($(".middle span").attr("class")=="emi newinfo") {
		if($("#mode").val()=="New") {
			setTimeout("window.location='"+base_path+page+"'",2000);			
		} else {
			setTimeout(callList,2000);
		}	
	}
}

function callError() {	
	var errArr = [];	
	var i = 0;
	$(".list input[type=text], .list input[type=password], .list textarea, .list select").each(function() {
		var classes = $(this).attr("class");
		classes = classes.split(" ");
		if(jQuery.inArray("err", classes)  == 1) {			 
			$(".replaceTemp").html($(this).parents("li").children("label").html());
			$(".replaceTemp em").remove();
			errArr[i] = $(".replaceTemp").html();			
			i++;
		}
	});		
	errArr = errArr.join(", ");
	$(".middle span").replaceWith(ajax_misfield);
	$(".middle span").append(" <span class='errLabel'>"+errArr+"</span>");
		
}

function callProcessing() {	    
	$(".middle span").replaceWith("<span><img src='"+base_path+"images/spinner_small.gif' class='imgLoader' alt='' /></span>");	
	$(".middle").fadeIn();	
}



/* Register start*/
function sendRegisterPassword(chk_spam) {
	$(".middle").hide();
	var allId="uname`npass`email`fname`lname`country";
	var flag = $.validate({
		uname	: {required: true, username: true},				  
		npass	: {required: true, minLength: 6, compare: "cpass"},
		cpass	: {required: true, minLength: 6,compareMain: "npass"},					  
		email: {required: true, email: true},
		fname	: {required: true},				 
		txtCaptcha: {required: true}	
	});
	
	if(flag && (chk_spam==1)) {		
		var value=getData(allId);		
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page="+page+"&option=register",
			dataType: "html",
			data: "data="+value,			
			beforeSend: function() {
				//callProcessing();				
				$("#processli").attr("style", "display:inline;");
				$(".processimg span").replaceWith("<span><img src='"+base_path+"images/spinner_small.gif' class='imgLoader' alt='' /></span>");	
				$(".processimg").fadeIn();	
			},
			success: function(data) {
				$(".middle").fadeIn();	
				$("#processli").attr("style", "display:none");
				$(".processimg span").replaceWith("");	
				$(".middle span").replaceWith(data);		
				/*if(data!=1){
					$(".middle").fadeIn();	
					$("#processli").attr("style", "display:none");
					$(".processimg span").replaceWith("");	
					$(".middle span").replaceWith(data);	
				}else{				
					//location.href=base_path+"myaccount";
					//$(".middle span").replaceWith(data);	
				}*/
			}				
 		});
	} else {
		document.getElementById("txtCaptcha").value="";
		callError();
		
	}	
	
}

function updateUserDetails() {	
	$(".middle").hide();
	var allId="cname`uname`npass`id";
	var flag = $.validate({
		uname	: {required: true, username: true},				  
		npass	: {required: true, minLength: 6, compare: "cpass"},
		cpass	: {required: true, minLength: 6,compareMain: "npass"}			
	});	
	if(flag) {
		var value=getData(allId);			
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page="+page,
			dataType: "html",
			data: "data="+value,			
			beforeSend: function() {
				callProcessing();
			},
			success: function(data) {				   
				$(".middle").replaceWith(data);					
				if(from_page){					
					window.location=from_page;
				}else{
					location.href=base_path+"myaccount";					
				}
				
			}				
 		});
	} else {		
		callError();
	}	
}

function editAccountDetails() {	
	$(".middle").hide();	
	var flag = $.validate({
		cname	: {required: true, username: true},				  	
		email	: {required: true, email: true},				  
		npass	: {minLength: 6, compare: "cpass"},
		cpass	: {minLength: 6,compareMain: "npass"},
		fname	: {required: true}	
	});	
	if(flag) {
		var value=getData("cname`npass`email`fname`lname`country`id");			
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page=myaccount&option=editAccount",
			dataType: "html",
			data: "data="+value,			
			beforeSend: function() {
				callProcessing();
			},
			success: function(data) {	
				$(".middle span").replaceWith(data);				
			}				
 		});
	} 
}
/* Register End */

/*Login */
function signIn() {	
	var flag = true;
	var allId="username`login_pass";
	/*var flag = $.validate({
		username	: {required: true},				  
		login_pass	: {required: true}		
	});*/	
	
	clearForm('', allId,"");
	
	var norId = allId;
	
		
	flag = checkValidation(norId);
	
	
	if(flag) {
		var value=getData(allId);		
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page=login&option=userLogin",
			dataType: "html",
			data: "data="+value,
			beforeSend: function() {		
				callProcess();
				//setTimeout(callProcess,5000);
			},
			success: function(data) {			
				if(data==1) {
					if(from_page){							
						window.location=from_page;
					}else{						
						$(".loginoutput").replaceWith(data);	
						window.location=base_path+"myaccount";
					}
				} else {						
					$(".loginoutput span").replaceWith(data);				
				}
			}			
 		});
	} else {		
		//$("#output").html("<span class='signin-err'>Invalid Username/Password.</span>");
		
		$(".loginoutput").html("<span class='emi newerror'>Invalid Username/Password.</span>");
		$(".loginoutput").fadeIn(2000,function(){
			setHeight();
		});
	}
}
function callProcess(){
	$(".loginoutput span").replaceWith("<span><img src='"+base_path+"images/spinner_small.gif' class='imgLoader' alt='' /></span>");	
	$(".loginoutput").fadeIn();	
}
/*Login End */

function getFeaturedLink() {	
	var flag = true;	
	value="";
	if(flag) {
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page=myaccount&option=getFeaturedLink",
			dataType: "html",
			data: "data="+value,
			beforeSend: function() {
				callProcessing();
			},
			success: function(data) {				
				$("#featuredlay").replaceWith(data);	
			}			
 		});
	}
	 
}

function viewLink(id) {	
	var flag = true;
	value=id;	
	if(flag) {
		$.ajax({
			type: "POST",
			url: base_path+"ajax_redirect.php?page=myaccount&option=setHits",
			dataType: "html",
			data: "data="+value,
			beforeSend: function() {
				callProcessing();
			},
			success: function(data) {					
				$('#linkhurl a[href^=""]').attr("target", "_blank");
			}			
 		});
	}
	 
}
function forgotPassword(){
	location.href=base_path+"password_resets";
}
function forgotEmail(){
	var flag = true;
	var allId="email";
	clearForm('', allId,"");
		var value=getData(allId);
		$.ajax({
			   	type : "post",
				url  : base_path+"ajax_redirect.php?page="+page,
				dataType : "html",
				data : "data="+value,
				beforeSend : function(){
					callProcess();
				},
				success : function(data){
					$(".loginoutput").fadeOut(200,function() {
						$(".loginoutput span").replaceWith(data);
						$(".loginoutput").fadeIn(2000);
					});	
				}
			   });
	
}
function changeLink(){
	var reciprocal = $("#reciprocal").val();
	var reciprocal_val = "";
	for(var i=1;i<=reciprocal;i++){
		if(document.getElementById("reciprocal"+i).checked == true){
			reciprocal_val = document.getElementById("reciprocal"+i)["value"];	
		}
		
	}

	if(reciprocal_val == "Post without reciprocal"){
		document.getElementById("linkURL").style.display = "none";	
		document.getElementById("linkHtml").style.display = "none";	
		$("#rec_link_url").val("");
	}else{
		document.getElementById("linkURL").style.display = "block";	
		document.getElementById("linkHtml").style.display = "block";	
	}
}

