/*Functions for the category pages*/
function show_hide_img(forum_id){
	var plus_img = document.getElementById('plus_img_'+forum_id);
	if(plus_img.style.display=="none"){
		plus_img.style.display=""
	}else{
		plus_img.style.display="none"
	}
	var minus_img = document.getElementById('minus_img_'+forum_id);
	if(minus_img.style.display=="none"){
		minus_img.style.display=""
	}else{
		minus_img.style.display="none"
	}
}

function show_hide(id, forum_id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = '';
		} else {
			document.getElementById(id).style.display = 'none';
		}
	} else {
		if (document.layers) {
			if (document.id.display == "none"){document.id.display = '';}
			else {document.id.display = 'none';}
		} else {
			if (document.all.id.style.visibility == "none"){document.all.id.style.display = '';}
			else {document.all.id.style.display = 'none';}
		}
	}
	show_hide_img(forum_id);
}

//this switches expand collapse icons
function filter(imagename,objectsrc){
	if (document.images){
		document.images[imagename].src=eval(objectsrc+".src");
	}
}

/*Functions for the category pages*/
//Resize images in the post content
function resizeImgs(maxHeight, maxWidth){
	var total_posts_obj = document.getElementById('hf_total_posts');
	if(total_posts_obj){
		var total_posts = total_posts_obj.value;
		for (var i = 0; i < total_posts; i++) {
			var post_text_container_id = i+'_post';
			var post_text_container_obj = document.getElementById(post_text_container_id);
			if(post_text_container_obj){
				var img = document.getElementById(post_text_container_id).getElementsByTagName('img');
				for (var j = 0; j < img.length; j++) {
					if (img[j].width > maxWidth){
						img[j].width = maxWidth;
					}
				}
			}
		}
	}
}

//Check if cookies are enabled or disabled
function check_cookie() {
	var c="cookietestwithjs=valid";
	document.cookie=c;
	if(document.cookie.indexOf(c)==-1){
		cookies_are_disabled = true;
	} else {
		cookies_are_disabled = false;
	}
	return cookies_are_disabled;
}
//Functions for setting and getting cookies
function set_cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) +( ( secure ) ? ";secure" : "" );
}
function record_unique_visit(category_id, forum_id, forum_url, user_id, premium, tracking_url, tracking_topic_id){
	if(!check_cookie()){
		var unique_number = new Date().getTime();
		var i=new Image();
		i.src=forum_url+"analytics/unique_visits_daily.php?c="+category_id+"&f="+forum_id+"&un="+unique_number+"&u="+user_id+"&p="+premium+"&turl="+tracking_url+"&t="+tracking_topic_id;
	}
}

/*Scripts for the side poll*/
//AJAX AJAX POST REQUEST
function surveys_makePostAjaxRequest(url, params, callback_function, return_xml) {
   var http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) { // IE
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) { }
       }
   }
   if (!http_request) {
       alert('Unfortunatelly you browser doesn\'t support this feature.Try another browser please');
       return false;
   }
   http_request.open('POST', url, true);
   //Send the proper header information along with the request
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", params.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
               } else {
                   eval(callback_function + '(http_request.responseText)');
               }
           } else {
               //alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
   http_request.send(params);
}
//END AJAX POST REQUEST

function show_side_survey(ad_position_id, f_id){
	if(!ad_position_id){
		var ad_position_id = 0;
	}
	if(!f_id){
		var f_id = 0;
	}
	var url = "poll_display.php";
	var params = "ad_pos_id="+ad_position_id+"&f_id="+f_id;
	document.write('<div id="side_survey_box"></div>');
	surveys_makePostAjaxRequest(url, params, 'show_side_survey_handler', false);
}

function show_side_survey_handler(data){
	if(data){
		var surv_div = document.getElementById('side_survey_box');
		if(surv_div){
			surv_div.innerHTML = data;
		}
	}
}

function validate_vote(){
	var question_type = document.getElementById('question_type').value;
	if(question_type==0){
		//Single choice question
		var answer_id = document.getElementById('selected_answer').value;
		if(!answer_id){
			alert('Please select an answer');
			return false;
		}
	}else if(question_type==1){
		var one_answer_selected = false;
		//Multiple choice question
		var answer_ids_obj = document.getElementById('answer_ids');
		if(answer_ids_obj){
			var answer_ids = document.getElementById('answer_ids').value;
			if(answer_ids.length>0){
				var answer_ids_array = answer_ids.split(",");
				for(i=0; i<answer_ids_array.length-1;i++){
					var answer_name = "answer_"+answer_ids_array[i];
					var answer_selected = document.getElementById(answer_name).checked;
					if(answer_selected){
						one_answer_selected = true;
						break;
					}
				}
			}
		}
		if(one_answer_selected == false){
			alert('Please select at least one answer');
		}
		return one_answer_selected;
	}
	return true;
}

function survey_vote(survey_id){
	if(validate_vote()){
		var question_id = 1;
		var answer_id = document.getElementById('selected_answer').value;
		var quickpoll = 0;
		//Ehealth user id
		var e_uid = 0;
		var side_e_uid_obj = document.getElementById('side_e_uid');
		if(side_e_uid_obj){
			e_uid = side_e_uid_obj.value;
		}
		//Ad position id
		var e_ad_pos_id = 0;
		var side_e_ad_pos_id_obj = document.getElementById('side_e_ad_pos_id');
		if(side_e_ad_pos_id_obj){
			e_ad_pos_id = side_e_ad_pos_id_obj.value;
		}
		var url = "../surveys/poll_vote.php";
		var params =
		"survey="+survey_id+
		"&question_id="+question_id+
		"&answer="+answer_id+
		"&quickpoll="+quickpoll+
		"&e_uid="+e_uid+
		"&e_ad_pos_id="+e_ad_pos_id
		;
		var question_type = document.getElementById('question_type').value;
		if(question_type==1){
			//Multiple choices poll
			params += "&selected_answers="+register_mulitple_answers();
		}
		surveys_makePostAjaxRequest(url, params, 'survey_vote_handler', false);
	}
}

function survey_vote_handler(data){
	//Redirect to the thank you for voting page
	//document.location.href="poll_display.php?mode=thanks";
	var side_survey_box = document.getElementById('side_survey_box');
	if(side_survey_box){
		side_survey_box.innerHTML =
			'<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>'+
			'<script type="text/javascript">'+
			'_udn = "none";'+
			'_uacct = "UA-258123-4";'+
			'urchinTracker();'+
			'</script>'+
			'<div class="survey_thank_you">'+
			'	Thank you for voting'+
			'</div>		';
	}
}

function register_answer(radio_btn){
	if(radio_btn){
		var answer = radio_btn.value;
		if(answer){
			document.getElementById('selected_answer').value = answer;
		}
	}
}

function register_mulitple_answers(){
	var selected_answeres = "";
	var answer_ids_obj = document.getElementById('answer_ids');
	if(answer_ids_obj){
		var answer_ids = document.getElementById('answer_ids').value;
		if(answer_ids.length>0){
			var answer_ids_array = answer_ids.split(",");
			for(i=0; i<answer_ids_array.length-1;i++){
				var answer_name = "answer_"+answer_ids_array[i];
				var answer_selected = document.getElementById(answer_name).checked;
				if(answer_selected){
					selected_answeres = selected_answeres+"1";
				} else {
					selected_answeres = selected_answeres+"0";
				}
			}
		}
	}
	return selected_answeres;
}
/*END - Scripts for the side poll*/

/*Validation scripts*/
function isPosInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
/*END - Validation scripts*/

/*Jumpbox scripts*/
function jump_to_url(jump_box){
	if(jump_box){
		var selected_value = jump_box.options[jump_box.selectedIndex].value;
		if(isPosInteger(selected_value) || selected_value==-1){
			if(selected_value!=-1){
				document.getElementById('jumpbox').submit();
			}
		}else{
			document.location.href = selected_value;
		}
	}
}
/*END - Jumpbox scripts*/

/* User profile pages */
function add_onmouse_over_event_to_all_child_elements(parent_node, element_id){
	var all_child_nodes = parent_node.childNodes;
	for (i=0;i<all_child_nodes.length; i++){
		all_child_nodes[i].onmouseover  = function f(){
			document.getElementById(element_id).style.display="";
		}
		all_child_nodes[i].onmouseout = function f1(){
			document.getElementById(element_id).style.display="";
		}
	}
}

function discard_show_popup(){
	if(alertTimerId){
		clearTimeout (alertTimerId);
	}
}
function show_short_user_profile(a,b) {

}
function main_show_short_user_profile(user_id, x, y){
	$('short_user_profile').hide();
	if(user_id){
		var url = 'generate_short_user_profile.php';
		var params = '?user_id=' + user_id;
		url = url+params;
		mouseX = x;
		mouseY = y;
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
			$('short_user_profile').style.top = mouseY+"px";
			$('short_user_profile').style.left = mouseX+"px";
  			$('short_user_profile').update(transport.responseText);
			add_onmouse_over_event_to_all_child_elements($('short_user_profile'), 'short_user_profile');
			document.getElementById('short_user_profile').style.display="block";
		  }
		});
	}
}
function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
		else if(obj.y)
		curtop += obj.y;
	return curtop;
}
function show_user_profile_under_image(avatar){
	var avatar_relation = avatar.getAttribute('rel');
	var avatar_data = avatar_relation.split("_");
	var user_id = avatar_data[1];
	if(!user_profile_popup_opened || (user_id_profile_popup_opened != user_id)){
		if(user_id>0){
			var the_image = avatar;
			if(the_image){
				var x = findPosX(the_image);
				var y = findPosY(the_image);
				y+=10;
				var image_height = the_image.height;
				if(image_height){
					y = y + image_height;
				}
			}
			main_show_short_user_profile(user_id, x, y)
			user_profile_popup_opened = true;
			user_id_profile_popup_opened = user_id;
		}
	} else {
		user_profile_popup_opened = false;
		user_id_profile_popup_opened = 0;
		document.getElementById('short_user_profile').style.display="none";
	}
}
function close_user_profile(popup_obj){
	popup_obj.style.display="none";
	user_profile_popup_opened = false;
}

function attach_event_listeners(avatar){
	avatar.onclick  = function f(){
		show_user_profile_under_image(avatar);
	};

	avatar.onmouseout  = function f1(){
		discard_show_popup();
	};
}

function append_user_details_to_all_avatars(){
	var all_avatars = getElementsByClassName("avatar_image", null);
	if(all_avatars){
		for(var i=0,j=all_avatars.length; i<j; i++){
			if(all_avatars[i]){
				attach_event_listeners(all_avatars[i]);
			}
		}
	}
}
function getElementsByClassName(classname, node)  {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className)) a.push(els[i]);
	return a;
}
function show_report_note_menu(e, user_note_id) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	document.getElementById('selected_user_note_id').value=user_note_id;
	show_and_postion_report_div(posx, posy);
}

function show_and_postion_report_div(x, y) {
	x+=-20;
	y+=-60;
	var report_div = document.getElementById('report_note');
	if(report_div) {
		report_div.style.top = y+"px";
		report_div.style.left = x+"px";
		add_onmouse_over_event_to_all_child_elements(report_div, 'report_note')
		report_div.style.display="";
		report_div.className = "report_user_note_box_visible";
	}
}
function hide_report_div() {
	var report_div = document.getElementById('report_note');
	if(report_div){
		report_div.style.display="none";
	}
}

function report_user_note(note_complaint_type_id) {
	var selected_user_note_id = document.getElementById('selected_user_note_id').value;
	var reporter_user_id = document.getElementById('reporter_user_id').value;
	if(selected_user_note_id && reporter_user_id) {
		var url = "thanking/report_user_note.php";
		var params = "user_note_id="+selected_user_note_id+
			"&reporter_user_id="+reporter_user_id+
			"&note_complaint_type_id="+note_complaint_type_id;
		makePostAjaxRequest(url, params, 'report_user_note_handler', false);
		hide_report_div();
		document.getElementById("r_"+selected_user_note_id).innerHTML = '<img src="images/loading1-0.gif" alt="loading..." align="center">';
	}
}

function report_user_note_handler(resultText){
	var selected_user_note_id = document.getElementById('selected_user_note_id').value;
	document.getElementById("r_"+selected_user_note_id).innerHTML = '<span class="reported_user_note">Reported</span>';
}

function show_all_forums(url){
	var win = window.open(url, "","location=0, status=0,scrollbars=1, toolbar=0, directories=0, resizable=0, titlebar=0, menubar=0, width=790, height=400, top=100, left=200");
	win.focus();
}

function validate_appointment_form(form) {
	if (
			trim_str(form.name.value) == "" ||
			trim_str(form.phone.value) == "" ||
			trim_str(form.email.value) == "" ||
			(
				!form.insurance[0].checked &&
				!form.insurance[1].checked
			) ||
			trim_str(form.provider.value) == "" ||
			trim_str(form.reason.value) == "" ||
			(
				!form.new_patient[0].checked &&
				!form.new_patient[1].checked
			) ||
			trim_str(form.date.value) == "" ||
			trim_str(form.time.value) == "" ||
			trim_str(form.entered_security_code.value) == ""
		) {
		alert("Please fill the required fields");
		return false;
	}
	return true;


}

function trim_str(str) {
	var	str = str.replace(/^\s\s*/, ''),ws = /\s/,i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function show_hide_object(id, show_hide){
	var object = document.getElementById(id);
	if(object){
	   if(show_hide){
	   		object.style.display="";
	   }else{
	   		object.style.display="none";
	   }
	}
}
// functions from ajax.js
function makeHttpRequest(url, callback_function, return_xml){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Unfortunatelly you browser doesn\'t support this feature.Try another browser please');
		return false;
	}
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (return_xml) {
					eval(callback_function + '(http_request.responseXML)');
				} else {
					eval(callback_function + '(http_request.responseText)');
				}
			} else {}
		}
	}
	http_request.open('POST', url, true);
	http_request.send(null);
}
function makePostAjaxRequest(url, params, callback_function, return_xml) {
	var http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
	} else if (window.ActiveXObject) { // IE
		try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { http_request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Unfortunatelly you browser doesn\'t support this feature.Try another browser please');
		return false;
	}
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", params.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (return_xml) {
					eval(callback_function + '(http_request.responseXML)');
				} else {
					eval(callback_function + '(http_request.responseText)');
				}
			} else {
				alert('There was a problem with the request.(Code: ' + http_request.status + ')');
			}
		}
	}
	http_request.send(params);
}
// end functions from ajax.js
// testimonials functions
function start_testimonials() {
	if (total_testimonials > 1) {
		swith_testimonials();
	} else {
		if (typeof(testimonials_el) != undefined && testimonials_el != null) {
			testimonials_el.innerHTML = '<span class="testimonial_quotes">"' + testimonials[0]['text'] + '" - </span><span class="testimonial_by">' + testimonials[0]['name'] + '</span>';
		}
	}
}
function swith_testimonials() {
	if (typeof(testimonials_el) != undefined && testimonials_el != null) {
		testimonials_el.innerHTML = '<span class="testimonial_quotes">"' + testimonials[tcnt]['text'] + '" - </span><span class="testimonial_by">' + testimonials[tcnt]['name'] + '</span>';
		tcnt++;
		if (tcnt > (total_testimonials -1)) tcnt = 0;
		setTimeout('swith_testimonials()', delay);
	}
}
// end testimonials functions
// gs.js
function gs_style(){
	var f = document.getElementById('searchbox_018324609363426854129:foame-13mwe');
	if (f) {
		var q = f.q;
		//Search textbbox
		if (q) {
			q.style.border = '2px solid #A8C4E0';
			q.style.height = '18px';
//			q.style.width = '200px';
			q.style.fontSize = '12px';
			q.style.verticalAlign = 'middle';
			var mouse_over = function() {
				q.style.border = '2px solid #FFC40C';
			}
			var mouse_out = function() {
				q.style.border = '2px solid #A8C4E0';
			}
			q.onmouseover = mouse_over;
			q.onmouseout = mouse_out;
		};
		//Search button
		var sa = f.sa;
		if(sa){
			sa.className = 'search_button';
			var mouse_over = function() {
				sa.className = 'search_button_over';
			}
			var mouse_out = function() {
				sa.className = 'search_button';
			}
			sa.onmouseover = mouse_over;
			sa.onmouseout = mouse_out;
		}
	}
}
// end gs.js


//Font size picker scripts
var min=8;
var max=18;
function increaseFontSize() {
	var p = document.getElementsByClassName('KonaBody');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByClassName('KonaBody');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}
//END - Font size picker scripts

function focusOnElement(element_id){
	document.getElementById(element_id).focus();
}
//VALIDATION JAVASCRIPTS
function validateField(field_id, msg_box_id){
	var validated = false;
	var first_value = document.getElementById(field_id).value;
	if(first_value.length == 0){
		show_hide_object(msg_box_id, true);		
		validated = false;
	}else{
		show_hide_object(msg_box_id, false);
		validated = true;
	}	
	return validated;
}

function is_email_valid(field_id, msg_box_id, focus){
    var validated = false;
    var fieldValidated = validateField(field_id, msg_box_id);
    if(fieldValidated){
        var validRegExp = /^.+@.+\..+$/;
        var strEmail = document.getElementById(field_id).value;
        if (strEmail.search(validRegExp) == -1){
        	show_hide_object(msg_box_id, true);
        	if(focus) {
        		focusOnElement(field_id);	        		
        	}		    
		    validated = false;
        }else{
        	show_hide_object(msg_box_id, false);
		    validated = true;
        }
   }
   return validated;
}
//END - VALIDATION JAVASCRIPTS