﻿function whenCompleted2(){
	var checker = document.getElementById('check_pass').innerHTML;
	if(checker==""){
		refreshPage();
	}
}
function whenCompleted(){	
		refreshPage();
}
function whenReqLoading(){
	document.getElementById("loginBox").style.paddingTop = "3px";
	var r_div = document.getElementById('friend_func_div');
	r_div.innerHTML = '<img src="img/ajax-loader.gif" alt="loading..." title="loading..." />';
}

function loginLoading(){
	document.getElementById('check_pass').innerHTML = '<img src="img/3-0.gif" alt="Loading..." title="Loading..."/>';	
}

function doit(){
	var login = document.getElementById('user_login').value;
	var pass = document.getElementById('user_pass').value;
	ajax.requestFile = "check_pass.php?login="+login+"&pass="+pass;
	ajax.method = "get";
	ajax.element = 'check_pass';
	ajax.onLoading = loginLoading;
	ajax.onCompletion = whenCompleted2;
	ajax.runAJAX();
}
function searchit(){
	var str = document.getElementById('search_str').value;
	ajax.requestFile = "search.php";
	ajax.setVar("search", str);
	ajax.method = "post";
	ajax.element = 'main_body_div';
	ajax.onLoading = searchLoading;
	ajax.runAJAX();
}
function searchLoading(){
	document.getElementById("main_body_div").innerHTML = '<div style="padding:5px" align="center"><b>Loading...</b></div>';
}
function request_user(id, func){
	ajax.requestFile = "friends_func.php?"+func+"="+id;
	ajax.method = "get";
	//ajax.element = 'login_box';
	ajax.onLoading = whenReqLoading;
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}
function delete_msg(id, action, start, user){
	ajax.requestFile = "includes/pm_func.php?id="+id+"&action="+action+"&start="+start+"&user="+user;
	ajax.method = "get";
	ajax.element = 'list_of_messages';
	//ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}
function send_pm(user_id, my_id){
	var message = document.getElementById('message_text').value;
	if(message==""){
		alert("Please enter a message");
		return false;
	}
	else{
		ajax.requestFile = "friends_func.php";
		ajax.setVar("send_pm", 1);
		ajax.setVar("user_id", user_id);
		ajax.setVar("my_id", my_id);
		ajax.setVar("text", message);
		ajax.method = "post";
		ajax.element = 'text_area';
		ajax.onLoading = pm_onloading;
		ajax.onCompletion = message_sent;
		ajax.runAJAX();
		//alert("user_id = "+user_id+" my_id="+my_id+"\n"+"text = "+message);
	}
}
function pm_onloading(){
	document.getElementById("pm_loader").innerHTML = '<img src="img/ajax-loader.gif" alt="preloader" />';	
}
function message_sent(){
	document.getElementById("pm_loader").innerHTML = '';
}
function refreshPage() {
window.location = "index.php";
}

function show_comment_box(art_id, user_id, user_gender){
	var com_box = document.getElementById('comment_box');
	var comments_div = document.getElementById('comments_div');
	if(comments_div.style.height!=(370+'px')){
		comments_div.style.width = 470+'px'; 
		comments_div.style.height = 370+'px';
		com_box.innerHTML = '<textarea class="comment_box" id="comment_field"></textarea><div align="right"><a href="javascript:void(0)" onclick="send_comment(\''+art_id+'\', \''+user_id+'\', \''+user_gender+'\')"><b>submit</b></a>';
	}
	else{
		comments_div.style.width = 470+'px'; 
		comments_div.style.height = 320+'px';
		com_box.innerHTML="";
	}
}

/////////////comments functions ////////////
function comment_sent(gender){
	if(gender==1){
		var element = 'comment_btn';
	}
	else{
		var element = 'comment_btn2';
	}
	var comments_div = document.getElementById('comments_div');
	document.getElementById('comment_box').innerHTML = "";
	document.getElementById(element).innerHTML = '<b class="default">Your message has been added</b>';
	comments_div.style.height = 320+'px';
}
function send_comment(art_id, user_id, user_gender){
		var message = document.getElementById('comment_field').value;
		if(message==""){
			alert("Please enter a comment");
			return false;
		}
		
		ajax.requestFile = "includes/comments_func.php";
		ajax.setVar("art_id", art_id);
		ajax.setVar("user_id", user_id);
		ajax.setVar("user_gender", user_gender);
		ajax.setVar("text", message);
		ajax.method = "post";
		if(user_gender=='1'){
			ajax.element = 'he_says';
		}
		else{
			ajax.element = 'she_says';
		}
		ajax.onCompletion = comment_sent(user_gender);
		ajax.runAJAX();
}

//add voting entry
function add_vote(answer, user_id, comp_id, user_gender){
	if(user_id==0){
		alert("You need to login first");
		return false;
	}
	ajax.requestFile = "save_voting.php";
	ajax.setVar("add_vote", answer);
	ajax.setVar("user_id", user_id);
	ajax.setVar("comp_id", comp_id);
	ajax.setVar("gender", user_gender);
	ajax.method = "post";
	ajax.element = 'vote_box';
	ajax.runAJAX();	
}

///////////////for contact form ////////////////
function checkField(name){
var field = document.getElementById(name);
	if(field.value==name){
		field.value = '';
	}
}
function resetField(name){
	var field = document.getElementById(name);
	if(field.value==""){
		field.value = name;
	}
}

function whenLoading(){
	document.getElementById('send_form').innerHTML = '<img src="img/preloader3.gif" alt="preloader" />';
}
function whenCompleted3(){
	document.getElementById('send_form').innerHTML = "";
}
function sendForm(){
	var fields= new Array(4) 
	fields[0] = document.getElementById("name").value;
	fields[1] = document.getElementById("e-mail").value;
	fields[2] = document.getElementById("subject").value;
	fields[3] = document.getElementById("message").value;
	
	var not_ok = new Array('name', 'e-mail', 'subject', 'message');
	var error = document.getElementById("error");
	
	//for e-mail check
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	for (var i=0; i<fields.length; i++){
		if(fields[i]==not_ok[i]){
			error.innerHTML = "Please enter \""+not_ok[i]+"\"!";
			return false;
		}
		if(i==1){
			if (!filter.test(fields[i])) {
				error.innerHTML = "\"E-mail\" is not valid!";
				return false;
			}
		}
	}
	
	//sending form
	ajax.requestFile = "send.php";
	ajax.setVar("name", fields[0]);
	ajax.setVar("email", fields[1]);
	ajax.setVar("subject", fields[2]);
	ajax.setVar("message", fields[3]);
	ajax.method = "post";
	ajax.element = 'error';
	ajax.onLoading = whenLoading;
	ajax.onCompletion = whenCompleted3;
	ajax.runAJAX();
}
function show_friend_form(username, user_email, action, id){
	if(username==""){
		alert("You need to login to use this function");
	}
	else{
		document.getElementById('send_friend').innerHTML = 'Friend\'s E-mail:<br /><input type="text" class="input" size="15" id="friend_email" onkeypress="checkEnter(event, \'friend\', \''+username+'\', \''+user_email+'\', \''+action+'\', \''+id+'\')" /> <a class="login_btn" style="text-decoration: none;" onclick="send_to_friend(\''+username+'\', \''+user_email+'\', \''+action+'\', \''+id+'\');" href="javascript:void(0);">&nbsp;&nbsp;&nbsp;&nbsp;Send&nbsp;&nbsp;&nbsp;&nbsp;</a> <span id="fr_send"></span>';
	}
}
function send_to_friend(username, user_email, action, id){
	var email = document.getElementById('friend_email').value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(email==""){
		alert("Please enter Friend's E-mail");
	}
	else if (!filter.test(email)) {
				alert('E-mail is invalid!');
				return false;
	}
	else{		
		ajax.requestFile = "send.php";
		ajax.setVar("friend_email", email);
		ajax.setVar("username", username);
		ajax.setVar("user_email", user_email);
		ajax.setVar("action", action);
		ajax.setVar("id", id);
		ajax.method = "post";
		ajax.element = 'fr_send';
		ajax.onLoading = send_to_friend_loading;
		ajax.onCompletion = send_to_friend_complete;
		ajax.runAJAX();	
	}
}
function send_to_friend_loading(){
	document.getElementById("fr_send").innerHTML = '<img src="img/ajax-loader.gif" alt="loading..." title="loading..." />';
}
function send_to_friend_complete(){
	document.getElementById("friend_email").value = ""; 
}

<!-- menu functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

//fucntion to change login input boxes onfocus
function field_change(cont, text, text_ch){
	if(cont.value==text){
		cont.value = text_ch;
	}
}

///functions for password recovery
function send_pass(){
	var email = document.getElementById("req_email").value;
	ajax.requestFile = "includes/pass_recovery.php";
	ajax.setVar("email", email);
	ajax.method = "post";
	ajax.element = 'send_pass_msg';
	ajax.onLoading = send_pass_loading;
	ajax.onCompletion = send_pass_complete;
	ajax.runAJAX();	
}
function send_pass_loading(){
	document.getElementById("send_pass_msg").innerHTML = '<img src="img/loader-gray.gif" alt="loading..." title="loading..." />';
}
function send_pass_complete(){
	/*var check = document.getElementById("send_pass_msg").innerHTML;
	if(check!="<font color=\"#ff0000\"><b>Invalid e-mail!</b></font>"){
		document.getElementById("send_pass_but").innerHTML = "";
	}*/
}

function rec_pass(id){
	if(id!=""){
		var pass1 = document.getElementById("req_new_pass").value;
		var pass2 = document.getElementById("req_conf_pass").value;
		if(pass1==pass2){
			ajax.requestFile = "includes/pass_recovery.php";
			ajax.setVar("id", id);
			ajax.setVar("pass", pass1);
			ajax.method = "post";
			ajax.element = 'conf_pass_msg';
			ajax.onLoading = conf_pass_loading;
			ajax.onCompletion = send_pass_complete;
			ajax.runAJAX();
		}
		else{
			document.getElementById("conf_pass_msg").innerHTML = '<font color="#ff0000">Passwords you enter don\'t match</font>';
		}
	}
}
function conf_pass_loading(){
	document.getElementById('conf_pass_msg').innerHTML = '<img src="img/loader-gray.gif" alt="loading..." title="loading..." />';
}
function conf_pass_complete(){
	/*var check = document.getElementById('conf_pass_msg').innerHTML;
	if(check!='<font color="#ff0000"><b>Invalid ID!</b></font>'){
		document.getElementById("conf_pass_but").innerHTML = "";
	}*/
}

//functions for comparison polls
function add_comp_vote(id, userid, usergender, seq_id){
	if(userid==""){
		alert("You need to login first!");
	}
	else{
		ajax.requestFile = "includes/comparison_save.php";
		ajax.setVar("id", id);
		ajax.setVar("userid", userid);
		ajax.setVar("usergender", usergender);
		ajax.setVar("seq_id", seq_id);
		ajax.method = "post";
		ajax.element = 'comp_table';
		ajax.onLoading = comp_loading;
		ajax.runAJAX();
	}
}

function comp_loading(){
	document.getElementById('comp_table').innerHTML = '<div style="padding:5px" align="center">Loading...</div>';
}

function search_links(){
	var doc = document.getElementsByTagName('a');
	for (var i = 0; i < doc.length; i++){
	   //Do Work on doc[i], this sets the border of the Div black
	   var link_val = doc[i].href;
	   var checker = true;
	   
	   if(link_val.substring(0, 4)=="http"){
		 if(link_val.substring(0, 20)!="http://www.thinklike"){
			var false_ar = new Array("http://www.facebook", "http://www.myspace.", "http://digg.com/sub", "http://del.icio.us/", "http://reddit.com/s"); 
			for(var z=0; z<false_ar.length; z++){
				if(link_val.substring(0, 19)==false_ar[z]){
					checker=false;
				}
			}
			if(checker==true){
				doc[i].setAttribute("onclick", "link_count('"+link_val+"', this)");	
			}
		 }
	   }
	}
}
function link_count(url, el){
	ajax.requestFile = "includes/link_counter.php";
	ajax.setVar("url", url);
	ajax.method = "post";
	ajax.element = el;
	ajax.runAJAX();	
}