function checkLogin(){
	var nameVal = $('#name');
	var pwdVal = $('#password');
	var redirect = $('#_red').val();
			
	if(nameVal.val() == "") {
		alert("Please Enter Name");
		nameVal.focus();
		return false;
	} 		
	if(pwdVal.val() == "") 	{
		alert("Please Enter Password");
		pwdVal.focus();
		return false;
	}	
	$.ajax({
		  type: "POST",
		  url: SITE_URL+"?_a=login",
		  data: {name:nameVal.val(),pwd:pwdVal.val()}
		}).done(function( msg ) {
			msg = $.trim(msg);	
			var errmsg = "";
			var resObj = eval("("+msg+")");
			if(resObj['status'] == 1){			
				if(redirect!=''){
					top.location.href=SITE_URL+"?_a="+redirect;
				}else{
					top.location.href=SITE_URL;
				}
				//top.location.href=SITE_URL;
				//window.top.location.reload();
			} else if(resObj['status'] == 2){
				errmsg = "Incorrect Username or Password!";
			} 
			$('#error_msg').html(errmsg);			
	});
}
function checkRegister(mpop){
	if(mpop == 'm'){
		var nameVal = $('#mregname');
		var locVal = $('#mloc');
		var emailVal = $('#mregemailId');
		var userVal = $('#musername');
		var pwdVal = $('#mregpassword');
		var errVal = $('#merror_msg1');
	}else{
		var nameVal = $('#regname');
		var locVal = $('#loc');
		var emailVal = $('#regemailId');
		var userVal = $('#username');
		var pwdVal = $('#regpassword');
		var errVal = $('#error_msg1');
	}
	
	var rcf = $('#recaptcha_challenge_field').val();
	var rrf = $('#recaptcha_response_field').val();
	var genderVal = $('#gender');
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	var redirect = $('#_red').val();
	
	var gender = '';	
     var radioButtons = document.getElementsByName("gender");
     for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
         gender = radioButtons[x].value;
       }
    }
	  
	if(nameVal.val() == "") {
		alert("Please Enter Name");
		nameVal.focus();
		return false;
	} 
	if(locVal.val() == "") {
		alert("Please Enter Location");
		locVal.focus();
		return false;
	} 
	if(emailVal.val() == "") {
		alert("Please Enter Email Id");
		emailVal.focus();
		return false;
	} 
	if(!emailPattern.test(emailVal.val())) {
		alert('Please Enter Valid Email Id');
		emailVal.focus();
		return false;
	} 
	if(userVal.val() == "") {
		alert("Please Enter Username");
		userVal.focus();
		return false;
	} 	
	if(pwdVal.val() == "") 	{
		alert("Please Enter Password");
		pwdVal.focus();
		return false;
	}
	if(gender == "") 	{
		alert("Please Select Gender");
		gender.focus();
		return false;
	}
				
	
	$.ajax({		
		  type: "POST",
		  url: SITE_URL+"?_a=register",		
		  data: {name:nameVal.val(),emailId:emailVal.val(),loc:locVal.val(),un:userVal.val(),pwd:pwdVal.val(),gender:gender}
		}).done(function( msg ) {		 
			msg = $.trim(msg);
			var errmsg = "";
			var resObj = eval("("+msg+")");
			var mColor = 'red';
			if(resObj['status'] == 1){
				errmsg ="The CAPTCHA wasn't entered correctly. Please try again.";
			} else if(resObj['status'] == 2){
				errmsg ="This username or email already exists. Please try again.";
			} else if(resObj['status'] == 3){
				errmsg ="Your Registration is successful. Please Login";
				mColor ='#02733d';				
			}
			nameVal.val('');
			emailVal.val('');
			userVal.val('');
			pwdVal.val('');
			errVal.css('color',mColor);
			errVal.html(errmsg);
			errVal.show();
			if(mpop == 'm'){
				setTimeout("$('#merror_msg1').hide();",6000);
			}else{
				setTimeout("$('#error_msg1').hide();",6000);
			}
			
	});
}

function shareOnFb(title,desc,link,pic,storyId){
	FB.login(function(response) {
	   if (response.authResponse) {
		FB.api('/me', function(response) {
				$.ajax({
					type: "POST",
					url: SITE_URL+"?_a=fblogin",
					data:{data: JSON.stringify(response)}
				}).done(function( msg ){
					//alert(msg); return false;
					msg = $.trim(msg);		
					var resObj = eval("("+msg+")");
					var errmsg = "";
					if(resObj['status'] == 3 ||resObj['status'] == 2){
						FB.ui({
							method: 'feed',
							link: link+"&ref="+response.id,
							picture: pic,
							name: title,
							description: desc,
							caption: 'jaagore'
							},
							function(response){
							    if (response && response.post_id) {
							      $.ajax({
									type: "POST",
									url: SITE_URL+"?_a=saveFBShare",
									data:{storyId:storyId}
								}).done(function( msg ){
									msg = $.trim(msg);		
									var resObj = eval("("+msg+")");
									if(resObj['status'] == 1){
										//alert('Thanks for sharing.');
										if( cont_page && (typeof cont_page != 'undefined') && cont_page == 'storyDetail'){
											var ss_count = parseInt($('#story_share_count').html());
											ss_count ++;
											$('#story_share_count').html(ss_count);
											//top.location.reload();
										}
									}else if(resObj['status'] == 2){
										//alert("Please Try after sometime");
									} 
								});
							    } else {
							      alert('Post was not published.');
							    }
							});
					}else if(resObj['status'] == 1 || resObj['status'] == 4){
						alert("Error in Login! Please Try after sometime");
					} 
				});
		 });
	   } 
	 }, {scope: 'email,publish_stream,offline_access,read_friendlists'});
	
}

function initTwitter(){		
	if(typeof(twttr) != 'undefined'){
		 twttr.ready(function (twttr) {					 
			twttr.events.bind('tweet', function(event) {								
				$.ajax({
					type: "POST",
					url: SITE_URL+"?_a=tweet",
					data:{data: JSON.stringify(event['target']["src"])}
				}).done(function( msg ){
					msg = $.trim(msg);
					var resObj = eval("("+msg+")");
					if(resObj['status'] == 1){
							//alert('Thanks for Tweet.');
							if( cont_page && (typeof cont_page != 'undefined') && cont_page == 'storyDetail'){
								var ss_count = parseInt($('#story_share_count').html());
								ss_count ++;
								$('#story_share_count').html(ss_count);
								//top.location.reload();
							}
					}else if(resObj['status'] == 2){
							//alert("Please Try after sometime");
					} 
				}); 					
			});
		});
	}else{
		setTimeout('initTwitter()',3000);
	}				
}

function checkForgot(){
	var emailVal = $('#forgotEmailId');
		
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	
	if(emailVal.val() == "") {
		alert("Please Enter Email Id");
		emailVal.focus();
		return false;
	} 
	if(!emailPattern.test(emailVal.val())) {
		alert('Please Enter Valid Email Id');
		emailVal.focus();
		return false;
	} 

	$.ajax({		
		  type: "POST",
		  url: SITE_URL+"?_a=forgotpassword",
		  data: {emailId:emailVal.val()}
		}).done(function( msg ) {			
			msg = $.trim(msg);			
			var errmsg = "";
			var resObj = eval("("+msg+")");			
			if(resObj['status'] == 1 ){
				alert("Your new password link is sent to your email id.");
				top.location.href=SITE_URL;
				//window.top.location.reload();
			} else if(resObj['status'] == 2 ){
				alert("Email Id is not exists!");
				//top.location.href=SITE_URL;
			} 
			//$('#error_msg').html(errmsg);			
	});
}

function check_form(){
	if($('#category').val()=='select'){
		alert("Please Select Category.");
		$('#category').focus();
		return false;
	}
	if($('#title').val()==""){
		alert("Please Enter title.");
		$('#title').focus();
		return false;
	}else if($('#title').val().length>=100){
		alert("Title can not be more than 100 characters");
		$('#title').focus();
		return false;
	}
	if($('#story').val().length>=4500){
		alert("Description can not be more than 500 words");
		$('#story').focus();
		return false;
	}
	if($('#up_img_url').val().length>0){
		var upFile = $('#up_img_url').val();
		 if (!/(\.(gif|jpg|jpeg|png))$/i.test(upFile)){
			alert("You can only upload images of the following formats .jpg,.gif,.png. Please try again.");
			$('#up_img_text').focus();
			return false;
		}
	}
	
	/*
	if($('#story').val()==""){
		alert("Please Enter Description.");
		$('#story').focus();
		return false;
	}else if($('#story').val().length>=1000){
		alert("Description can not be more than 1000 characters");
		$('#story').focus();
		return false;
	}
	
	
	if( $("#image_url").val()=="" && $("#video_url").val() =="" ){
		alert("Filling any one of the url fields is mandatory.");
		$('#image_url').focus();
		return false;	
	}*/
	
	/*
	if($('#category').val()==""){
		alert("Please select category.");
		$('#category').focus();
		return false;
	}
	
	if($('#tags').val()==""){
		alert("Please Enter tags.");
		$('#tags').focus();
		return false;
	}
	*/
	
	
	return true;
}

	function checkContactus(){
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
		if($('#cname').val()==""){
			alert("Please Enter Name.");
			$('#cname').focus();
			return false;
		}
		if($('#cemailId').val()==""){
			alert("Please Enter Email Id.");
			$('#cemailId').focus();
			return false;
		}
		if(!emailPattern.test($('#cemailId').val())) {
			alert('Please Enter Valid Email Id');
			$('#cemailId').focus();
			return false;
		} 	
		if($('#message').val()==""){
			alert("Please Enter Your Comments.");
			$('#message').focus();
			return false;
		}
		
	}

function checkNewPassword(){	
	var pwdVal = $('#pwd');
	var rpwdVal = $('#rpwd');
	
	if(pwdVal.val() == "") {
		alert("Please Enter Password");
		pwdVal.focus();
		return false;
	} 		
	if(rpwdVal.val() == "") 	{
		alert("Please Re-enter Password");
		rpwdVal.focus();
		return false;
	}
	if(rpwdVal.val() != pwdVal.val()){
		alert("Please enter the same password as above");
		rpwdVal.focus();
		return false;
	}
}

function publishStory(sid){	
	$.ajax({
		cache: false,
		type: "POST",
		url: SITE_URL+"?_a=publish",
		data: {id : sid}
	}).done(function( msg ){
		msg = $.trim(msg);		
		var resObj = eval("("+msg+")");	
		if(resObj['status'] == 1){
			//alert('Published');
			top.location.href=SITE_URL+'?_a=afterpublish';
		} else if(resObj['status'] == 2){ 
			top.location.href=SITE_URL+'?_a=alreadypublished';
		} else {
			alert('Try after some time');
		}
	});
}

function unpublishStory(sid){	
	
	$.ajax({
		cache: false,
		type: "POST",
		url: SITE_URL+"?_a=unpublish",
		data: {id : sid}
	}).done(function( msg ){
		msg = $.trim(msg);		
		var resObj = eval("("+msg+")");	
		if(resObj['status'] == 1){
			//alert('Published');
			top.location.href=SITE_URL+'?_a=curateddashboard';
		} 
	});
}

function catQuestion(){
	var cat_id = $('#cat_sel').val();

	if($('#cat_sel').val()=='select'){
		alert("Please Select Category.");
		$('#category').focus();
		return false;
	}
	$.ajax({
		cache: false,
		type: "POST",
		url: SITE_URL+"?_a=catques",
		data: {cid : cat_id}
	}).done(function( msg ){
		msg = $.trim(msg);		
		$('#curContent').css("height","1800px");
		//alert(msg.replace(/[\/]/g, '\\/'));
		$("#qusId").html(msg);
		/*var resObj = eval("("+msg+")");	
		if(resObj['status'] == 1){
			//alert('Published');
			top.location.href=SITE_URL+'?_a=afterpublish';
		} else if(resObj['status'] == 2){ 
			top.location.href=SITE_URL+'?_a=alreadypublished';
		} else {
			alert('Try after some time');
		}*/
	});

}

function check_curator_form(){
	
		
	if($('#why_curator').val()==""){
		alert("Please Enter Why do you want to be a curator.");
		$('#why_curator').focus();
		return false;
	}else if($('#why_curator').val().length>=500){
		alert("Text can not be more than 500 characters");
		$('#why_curator').focus();
		return false;
	}
	if($('#date1').val()==""){
		alert("Plese select your date of birth");
		$('#date1').focus();
		return false;
	}
	if($('#cat_sel').val()=='select'){
		alert("Please Select Category.");
		$('#cat_sel').focus();
		return false;
	}	
	
		
	var ids = $('#qids').val();	
	
	var temp = ids.slice(0, -1);
	
	var ids1 = new Array();
	ids1 = temp.split(',');		
	var len = ids1.length;
	//alert(len);
	for(var i = 0;i<len;i++){
		var radioName = 'ans'+ids1[i];
		if($('input[name='+ radioName +']:checked').length) {
			//alert("attribute"+$(this).attr('name')+" value="+$(this).val()); 		
			
		}
		else{
			alert("select answer for question "+(i+1));
			return false;
		}
	}
	
	//if($('#selReport').is(":radio:checked")) {
	if($('input[name=selReport]:checked').length) {
		//alert("attribute"+$(this).attr('name')+" value="+$(this).val()); 			
	}
	else{
		alert("select Report");
		return false;
	}
	/*
	for(var i = 0;i<len;i++){
		var obj = ('#ans'+ids1[i]);
		var len1 = obj.length;
		//alert(obj);
		for(var j = 0; j<len1; j++) {
			//alert($('#ans'+ids1[i][j]));
			var v = obj+"["+j+"]";
			if(v.checked) {
				
			} else {
				alert("select answer for question "+(i+1));
				return false;
			}
			
		}
	}
	*/
	
	/*
	
	
	if($('#story').val()==""){
		alert("Please Enter Description.");
		$('#story').focus();
		return false;
	}else if($('#story').val().length>=1000){
		alert("Description can not be more than 1000 characters");
		$('#story').focus();
		return false;
	}
	
	
	if( $("#image_url").val()=="" && $("#video_url").val() =="" ){
		alert("Filling any one of the url fields is mandatory.");
		$('#image_url').focus();
		return false;	
	}*/
	
	/*
	if($('#category').val()==""){
		alert("Please select category.");
		$('#category').focus();
		return false;
	}
	
	if($('#tags').val()==""){
		alert("Please Enter tags.");
		$('#tags').focus();
		return false;
	}
	*/
	
	return true;
}

function getJModal(imgName){		
		var msg = "<img src='"+SITE_URL+"uploads/answerimg/"+imgName+"' border='0' />";				
		$("#imgLoad").html(msg);		
		$('#modalImage').jqmShow();	
		adjustCenter('modalImage'); 
}

function catCount(){	
	var cat_id = $('#cat_sel').val();
	
	if($('#cat_sel').val()=='select'){
		alert("Please Select Category.");
		$('#category').focus();
		return false;
	}
	$.ajax({
		cache: false,
		type: "POST",
		url: SITE_URL+"?_a=submissioncount",
		data: {cid : cat_id}
	}).done(function( msg ){
		msg = $.trim(msg);	
		var resObj = eval("("+msg+")");	
		if(resObj['total'] > 10){
			top.location.href=SITE_URL+'?_a=catques&cid='+cat_id;
		}else{
			var str = "Hi, you have "+resObj['total']+" submissions published under "+resObj['title']+". <br/><br/><a href='"+SITE_URL+"?_a=submitstoryform'><img style='vertical-align:middle;' src='images/submit-button.jpg'   name='submit'  onmouseover='this.src = \'images/submit-mouseover.jpg\';' onmouseout='this.src = \'images/submit-button.jpg\';'/></a> "+(10-resObj['total'])+" more and you can be on your way to becoming a Curator of Positivity.";
		$("#msgId").html(str);
		}
		
	});

}
function onLogout(){
var c = confirm("Getting away from Positivity? Stay Positive offline. See you soon.");
	if(c==true){
		top.location.href=SITE_URL+'?_a=logout';
	}else{
	}
}

function getScrollContent(){
	var pageNo,s_url;
	if(sPageNo && (typeof sPageNo != 'undefined')){
		pageNo = parseInt(sPageNo)+1;
	}else{
		pageNo = 1;
	}
	
	if(scat_id && (typeof scat_id != 'undefined')){
		if(s_type && (typeof s_type != 'undefined')){
			s_url = SITE_URL+"?_a=scrollTopStory&pno="+pageNo+'&cid='+scat_id;
		}else{
			s_url = SITE_URL+"?_a=scrollCatStory&pno="+pageNo+'&cid='+scat_id;
		}
	}else if(s_type && (typeof s_type != 'undefined')){
		s_url = SITE_URL+"?_a=scrollTopStory&pno="+pageNo;
	}else{
		s_url = SITE_URL+"?_a=scrollStory&pno="+pageNo;
	}
	$('#pstv-loading').show();
	$.ajax({
		cache: false,		
		url: s_url		
	}).done(function( msg ){
		$('#pstv-loading').hide();
		msg = $.trim(msg);		
		$('#ColumnContainer').append(msg);
		$('#ColumnContainer').scrollTop = $('#ColumnContainer').scrollHeight;
		$(window).resize();
		$(document).resize();
	});
}
function getScrollProfileContent(){
	var pageNo,s_url;
	if(sPageNo && (typeof sPageNo != 'undefined')){
		pageNo = parseInt(sPageNo)+1;
	}else{
		pageNo = 1;
	}
	
	if(suid && (typeof suid != 'undefined')){
		s_url = SITE_URL+"?_a=scrollViewProfile&pno="+pageNo+'&uid='+suid+'&vtype='+s_type;
	}else{
		s_url = SITE_URL+"?_a=scrollViewProfile&pno="+pageNo+'&vtype='+s_type;
	}
	$('#pstv-loading').show();
	$.ajax({
		cache: false,		
		url: s_url		
	}).done(function( msg ){
		$('#pstv-loading').hide();
		msg = $.trim(msg);		
		$('#prite').append(msg);
		$('#prite').scrollTop = $('#prite').scrollHeight;
		$(window).resize();
		$(document).resize();
	});
}
function fetchUrlDet(){
		var url = $('#shrUrl').val();
		if(url.length>11){
			if(!isValidURL(url)){
				alert('Please enter a valid url.');
				return false;
			}else{
				$('#selimg').show();
				$('#loader').show();
				$.post(SITE_URL+"?_a=fetchurl&url="+url, {
				}, function(response){
					var str = '';
					response = eval('('+response+')');
					if(response['error']){
						$('#loader').hide();
						$('#error_msg_furl').html(response['error']);
						$('#error_msg_furl').show();
						setTimeout("$('#error_msg_furl').hide()",5000);
						setTimeout("$('#selimg').hide()",5000);
					}else if(response['yt_video']){
						$('#title').val(response['title']);
						$('#story').val(response['desc']);
						$('#source_url').val(response['url']);
						$('#video_url').val(response['url']);
						$('#loader').hide();						
						str ="<img src='http://img.youtube.com/vi/"+response['yt_id']+"/0.jpg' width='200'>";
						$('#selimgcont').html(str);
						$('#totalimg').hide();
						$('#nextprev').hide();
						$('#outercont').css('height','1200px');
					}else{
						$('#title').val(response['title']);
						$('#story').val(response['desc']);
						$('#source_url').val(response['url']);
						$('#loader').hide();
						if(parseInt(response['total']) > 0){
							var total_imgs = parseInt(response['total'])-1;
							$('#totalimg').html('Total '+response['total']+' images');
							for(var val in response['images']){
								str = str+"<img src='"+response['images'][val]+"' width='200' id='"+val+"' style='display:none'>";
							}							
							$('#selimgcont').html(str);
							$('#nextprev').show();
							$('#total_images').val(total_imgs);
							$('#cur_image').val(0);
							$('img#0').fadeIn();
							$('#image_url').val($('img#0').attr("src"));
							$('#outercont').css('height','1200px');	
						}else{
							$('#error_msg_furl').html('No images found');
							$('#error_msg_furl').show();
							setTimeout("$('#error_msg_furl').hide()",5000);
							setTimeout("$('#selimg').hide()",5000);
						}						
					}
									
				});
			}
		}else{
			alert('URL entered is too short, please try again');
		}
	}	
	
	function isValidURL(url){
		var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	
		if(RegExp.test(url)){
			return true;
		}else{
			return false;
		}
	}	
	// next image
	function nextImg(){
		var curimage = parseInt($('#cur_image').val());			
		if(curimage < parseInt($('#total_images').val())){
			$('img#'+curimage).hide();
			curimage = parseInt(curimage)+parseInt(1);
			$('#cur_image').val(curimage);
			$('img#'+curimage).show();
			$('#image_url').val($('img#'+curimage).attr("src"));
		}
	}
	// prev image
	function prevImg(){
		var curimage = parseInt($('#cur_image').val());
		if(curimage>0){
			$('img#'+curimage).hide();
			curimage = parseInt(curimage)-parseInt(1);
			$('#cur_image').val(curimage);
			$('img#'+curimage).show();
			$('#image_url').val($('img#'+curimage).attr("src"));
		}
	}
	
	function activeCurator(sid,cid){
	
		$.ajax({
			cache: false,
			type: "POST",
			url: SITE_ADMIN_URL+"?_a=activecurator",
			data: {uid : sid,cid:cid}
		}).done(function( msg ){
			
			msg = $.trim(msg);		
			var resObj = eval("("+msg+")");
			var errmsg = "";
			if(resObj['status'] == 2){
				alert('The User is already Curator for the Category '+resObj['curator_cat']);
			}else if(resObj['status'] == 1){
				errmsg="";
				alert('Make as Curator is done');
				top.location.href=SITE_ADMIN_URL+'?_a=showResults';
				//$('#errorMessage').html(errmsg);					
			} else {
					alert('Error! Try After Sometime');
			}
		}); 
	}

	function rejectExam(tid){
		//	alert(tid);
	$.ajax({
			cache: false,
			type: "POST",
			url: SITE_ADMIN_URL+"?_a=rejectExam",
			data: {tid : tid}
		}).done(function( msg ){			
			msg = $.trim(msg);		
			var resObj = eval("("+msg+")");
			var errmsg = "";
			if(resObj['status'] == 1){
				errmsg="";
				alert('Rejected');
				top.location.href=SITE_ADMIN_URL+'?_a=showResults';
				//$('#errorMessage').html(errmsg);					
			} else {
					alert('error');
			}
		});
	}
	
	
