function correctPNG(){
for(var i=0; i<document.images.length; i++)
  {
  var img = document.images[i]
  var imgName = img.src.toUpperCase()
  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	 {
	 var imgID = (img.id) ? "id='" + img.id + "' " : ""
	 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	 var imgStyle = "display:inline-block;" + img.style.cssText
	 if (img.align == "left") imgStyle = "float:left;" + imgStyle
	 if (img.align == "right") imgStyle = "float:right;" + imgStyle
	 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	 var strNewHTML = "<span " + imgID + imgClass + imgTitle
	 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	 + "(src=\'" + img.src + "\', sizingMethod='image');\"><"+"/span>"
	 img.outerHTML = strNewHTML
	 i = i-1
	 }
  }
}

// Simple function to make a Live Validation with the login form
$(function(){

		$("#m_email").validate({
			expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
			message: "Please provide a valid email address."
		});
		
		$("#m_password").validate({
			expression: "if (VAL.length > 5 && VAL) return true; else return false;", 
			message: "Field password is required. More than 5 characters."
		});
	
		 $("#m_name").validate({
			expression: "if (VAL) return true; else return false;",
			 message: "Field firstname is required"
		 });

		 $("#m_username").validate({
			expression: "if (VAL) return true; else return false;",
			 message: "Field username is required"
		 });

		 $("#country").validate({
				expression: "if (VAL != '0') return true; else return false;",
                message: "Please select a country"
		 });
		 
		$("#radio").validate({
                    expression: "if (isChecked(SelfID)) return true; else return false;",
                    message: "Please provide your gender."
        }); 
		
		 $("#login_pass").validate({
			expression: "if (VAL) return true; else return false;",
			 message: "Field password is required"
		 });

		 $("#login_name").validate({
			expression: "if (VAL) return true; else return false;",
			 message: "Field email is required"
		 });
		 
});

function isChecked(id){
    var ReturnVal = false;
    $("#" + id).find('input[type="radio"]').each(function(){
        if ($(this).is(":checked")) 
            ReturnVal = true;
    });
    $("#" + id).find('input[type="checkbox"]').each(function(){
        if ($(this).is(":checked")) 
            ReturnVal = true;
    });
    return ReturnVal;
}

//Function to confirm logout action
function confirm_logout(){
	confirm_msg = confirm('Are you sure your want to logout?');
	
	if(confirm_msg){
		window.location = 'logout.php';
	}
}

function confirm_cat_deletion(id){
	var title = document.getElementById('c_name').innerHTML;
	confirm_msg = confirm('Confirm deletion!\n\nDo you want to delete this category?');
	
	if(confirm_msg){
		window.location = 'index.php?page=categories&action=delete_cat&id='+id;
	}	
}

function confirm_user_deletion(id){
	confirm_msg = confirm('Confirm deletion!\n\nDo you want to delete this user?');
	
	if(confirm_msg){
		window.location = 'index.php?page=members&action=delete_member&id='+id;
	}	
}

function confirm_q_deletion(id){
	confirm_msg = confirm('Confirm deletion!\n\nAre you sure you want to delete this question?');
	
	if(confirm_msg){
		window.location = 'index.php?page=questions&action=delete_question&id='+id;
	}
}

function confirm_answer_deletion(id){
	confirm_msg = confirm('Confirm deletion!\n\nAre you sure you want to delete this answer?');
	
	if(confirm_msg){
		window.location = 'index.php?page=answers&action=delete_answer&id='+id;
	}
}

function member_filter_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=members&status=' + myOption.value;
}

function country_filter_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=members&country=' + myOption.value;
}

function question_filter_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=questions&status=' + myOption.value;		
}

function categories_filter_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=questions&categories=' + myOption.value;
}

function answer_filter_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=answers&status=' + myOption.value;		
}

function categories_answer_by(id){
	var myOption = document.getElementById(id);

	location.href = 'index.php?page=answers&categories=' + myOption.value;
}

$('.further').click(function(){
	$('.details').toggle('500');
	$(this).text($(this).text() == 'Hide further details' ? 'Add further details' : 'Hide further details');
	
	if($('.details:hidden')){
		$('#q_content').val('');
	}
})

$(document).ready(function($) {
  $('a[rel*=facebox]').facebox()
}) 

$('#m_dob').datepick({
			showOn: 'button', 
			buttonImageOnly: true, 
			buttonImage: 'js/datepicker/calendar.gif',
			yearRange:'-100:-13',
			dateFormat:'yy-mm-dd'
});	

$('#title').keyup(function(){
		var badWords = new Array('fuck','bitch','f u c k');
		
		var alert_arr = new Array();
		var alert_count = 0;
		var compare_text = $(this).val();
		
		for(i = 0;i < badWords.length;i++){
			for(j = 0;j < compare_text.length;j++){
				if(badWords[i]==compare_text.substring(j,(j+badWords[i].length)).toLowerCase()){
					alert_count++;
				}
			}
		}
		
		if(alert_count > 0){
			$('#q_error').text('Your question contain bad words. Please clean it up.');
			$('.q_submit').attr('disabled','disabled');
		}else{
			$('#q_error').text('Before submitting your question, make sure it\'s well formatted.');
			$('.q_submit').attr('disabled',false);
		}
});

$('.q_submit').click(function(){
	var question_title = $.trim($('#title').val());
	
	if(question_title == ''){
		$('#q_error').html('<strong>Please type a question of more than 5 characters</strong>');	
		return false;
	}	
});

$('#q_content').keyup(function(){
		var badWords = new Array('fuck','bitch','f u c k');
		
		var alert_arr = new Array();
		var alert_count = 0;
		var compare_text = $(this).val();
		
		for(i = 0;i < badWords.length;i++){
			for(j = 0;j < compare_text.length;j++){
				if(badWords[i]==compare_text.substring(j,(j+badWords[i].length)).toLowerCase()){
					alert_count++;
				}
			}
		}
		
		if(alert_count > 0){
			$('#q_error').text('Your question contain bad words. Please clean it up.');
			$('.q_submit').attr('disabled','disabled');
		}else{
			$('#q_error').text('Before submitting your answer, make sure it\'s well formatted.');
			$('.q_submit').attr('disabled',false);
		}
});

$('#qa_content').keyup(function(){
		var badWords = new Array('fuck','bitch','f u c k');
		
		var alert_arr = new Array();
		var alert_count = 0;
		var compare_text = $(this).val();
		
		for(i = 0;i < badWords.length;i++){
			for(j = 0;j < compare_text.length;j++){
				if(badWords[i]==compare_text.substring(j,(j+badWords[i].length)).toLowerCase()){
					alert_count++;
				}
			}
		}
		
		if(alert_count > 0){
			$('#a_error').text('Your question contain bad words. Please clean it up.');
			$('.q_submit').attr('disabled','disabled');
		}else{
			$('#a_error').html('<span style="color:#2371C1;">Before submitting your question, make sure it\'s well formatted.</span>');
			$('.q_submit').attr('disabled',false);
		}
});

 $("#qa_content").validate({
	expression: "if (VAL && VAL.length > 8) return true; else return false;",
	 message: "Your answer should be more than 8 characters."
 });
/*function toggleLogin(){
	$('.ajax_form_login').slideDown(1000);	
}*/

function limitChars(textid, limit, infodiv){
  var text = $('#'+textid).val(); 
  var textlength = text.length;

  if(textlength >= limit){
  	$('#' + infodiv).html(limit+'. Limit exceeded!');
	$('#' + infodiv).addClass('limit');
  	$('#'+textid).val(text.substr(0,limit));
  	return false;

  }else{
  	$('#' + infodiv).hide().html((limit - textlength)).fadeIn(300);
  		return true;
  }
}

$(document).ready(function(){
	$('#title').keyup(function(){
		limitChars('title',255,'countdown');						 
	});
})

$(document).ready(function(){
	$('#q_content').keyup(function(){
		limitChars('q_content',400,'contentcount');						 
	});
})

// Get the HTTP Object
      function getHTTPObject(){
      if (window.ActiveXObject) 
      return new ActiveXObject("Microsoft.XMLHTTP");
      else if (window.XMLHttpRequest) 
      return new XMLHttpRequest();
      else {
      alert("Your browser does not support AJAX.");
      return null;
      }
      } 
	  
	   function like(id){
         httpObject = getHTTPObject();
         if (httpObject != null) {
         httpObject.open("GET", "add_like.php?id="+id, true);
         httpObject.send(null);
		 httpObject.onreadystatechange = function(){
					setlikeOutput(id)
										} 
         }
        }	  
	
	 function like_article(id){
         httpObject = getHTTPObject();
         if (httpObject != null) {
         httpObject.open("GET", "add_like_article.php?id="+id, true);
         httpObject.send(null);
		 httpObject.onreadystatechange = function(){
					setlikeOutput(id)
										} 
         }
        }
		
		  function setlikeOutput(id){
		if(httpObject.readyState == 4){
		document.getElementById(id).innerHTML="";
		document.getElementById(id).innerHTML = httpObject.responseText;
		}
		}
  
      function p_attributes(keyword,page){
         httpObject = getHTTPObject();
         if (httpObject != null) {
         httpObject.open("GET", "product_attributes.php?keyword="
         +keyword+"&page="+page, true);
         httpObject.send(null);
		 httpObject.onreadystatechange = setOutput;
         }
        }	  
		
		function setOutput(){
		if(httpObject.readyState == 4){
		document.getElementById('products').innerHTML = httpObject.responseText;
		}
		}
		
function large_image(img,w,h)
{
	var height=h+20;
	 TINY.box.show('large-image.php?img='+img+'&w='+w+'&h='+h,1,w,height,1);
}

function more_attr(id,keyword)
		{
		 TINY.box.show('more.php?id='+id+'&keyword='+keyword,1,600,700,1);
		}	
		
function hidediv(div) { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById(div).style.display = 'none'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.div.display = 'none'; 
} 
else { // IE 4 
document.all.div.style.display = 'none'; 
} 
} 
} 

function showdiv(div) { 

if (document.getElementById) { // DOM3 = IE5, NS6 

document.getElementById(div).style.display = 'block'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.div.display = 'block'; 
} 
else { // IE 4 
document.all.div.style.display = 'block'; 
} 
} 

} 				
