/*** this file defines js functions the application
	 *** @modified: Monday, March 22, 2010
	 *** @author:   www.kursorsolutions.com
	 --------------------------------------------------------------*/
	var reset = 'home';
	var reset_to = '';
	var to_get = "";
	var json_crap;
	var obj_xmlhttp;
	var url_prefix = (location.href.indexOf("accraexpat.com") == -1 ) ? "http://www.accraexpat.site/" : "http://www.accraexpat.com/";

	// shortcut for dom getelementbyid
	function get_id (str_el) {return document.getElementById(str_el);}
	
	// show element.. change display
	function show_element ( str_el ) {
		str_el	=	get_id(str_el);		
		if( str_el ) str_el.className = str_el.className.replace('hidden', '');;		
	}
	
	// hide element.. change display
	function hide_element ( str_el ) {
		str_el	=	get_id(str_el);
		str_el.className.replace('hidden', '');
		str_el.className = (str_el.className == '') ? 'hidden' : str_el.className+' hidden';		
	}
	
	// toggle between show n hide
	function show_hide_element(str_el) {
		strEl	=	get_id(str_el);
		if (strEl.className.indexOf('hidden') == -1 ) {
			hide_element(str_el);
		} else {
			show_element(str_el);
		}
	}
	
	function display_message ( str_msg, str_typ )	{
		msg_box	=	get_id('msg-box');
		msg_box.className = str_typ;
		msg_box.innerHTML = str_msg;
		//show_element(get_id('msg-box'));
		opacity('msg-box', 0, 100, 700);
		clear_info_bar('');
	}
	
	// hide info bar if its being displayed
	function clear_info_bar(mode) {
		tmr_info = setTimeout("clear_info_bar('yes')", 5000)
		if (mode == 'yes') {
			if (get_id('msg-box').className != "hidden") {
				opacity('msg-box', 100, 0, 700);
				//hide_element('msg-box');
				clearTimeout(tmr_info);
			}
		}
	}
	
	function confirm_action(msg, action) {
		if (confirm(msg)) {
			if (action != '') {
	  	location.href = action;
	  }else{
			return true;
		}
		} else {
			 return false;			
		}
	}
  
	function validate(str_els) {
		arr_els = str_els.split(",");
		for (i=0; i < arr_els.length; i++ ) {
//                    alert("in: "+arr_els.length)
			strEl = arr_els[i];
			objEl = get_id(strEl);
                        objVAL = $.trim(objEl.value)
                        if (objEl.tagName == 'SELECT'&&objEl.options[objEl.selectedIndex].value == '') {
                                objEl.className = 'title error-field';
                                if(objEl.alt) {msg = objEl.title;} else {msg = "Select an item for this field";}
                                display_message(msg, 'error');
                                objEl.focus();
                                return false;
                        }
                         if (objVAL == '') {
                                objEl.className = 'title error-field';
                                if(objEl.alt) {msg = objEl.alt;} else {msg = "This field cannot be empty";}
                                display_message(msg, 'error');
                                objEl.focus();
                                return false;
                        } else {
                                if (objVAL != '' && objEl.id.indexOf('email') != -1 ) {
                                        if (!check_email(strEl)) {
                                                objEl.className = 'title error-field';
                                                if(objEl.alt) {msg = objEl.alt;} else {msg = "Please enter a valid email";}
                                                display_message(msg, 'error');
                                                objEl.focus();
                                                return false;
                                        } else {
                                                objEl.className = 'title';
                                        }
                                } else {
                                        objEl.className = 'title';
                                }
                        }
                        if(strEl=='fname'||strEl=='lname'){
                            if(check_names()){}else{return check_names()}
                        }
                        
		}
                return true;
	}
	
    function do_toggling(el) {
        current = get_id('toggler');
        if (current.value != '') {
			el2 = current.value.replace("dv-","pdv-");
            hide_element(current.value);
			get_id(el2).className = get_id(el2).className.replace("minus","plus");
        }show_element(el);
            current.value = el;
			el2 = el.replace("dv-","pdv-");
			get_id(el2).className = get_id(el2).className.replace("plus","minus");
    }
    
    function check_email(fld) {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(get_id(fld).value)) {return (true);}
		else {return (false);}
    }
    
	function bookmarksite(title,url){
	    if (window.sidebar) // firefox
	        window.sidebar.addPanel(title, url, "");
	    else if(window.opera && window.print){ // opera
	        var elem = document.createElement('a');
	        elem.setAttribute('href',url);
	        elem.setAttribute('title',title);
	        elem.setAttribute('rel','sidebar');
	        elem.click();
	    } 
	    else if(document.all) { // ie
	        window.external.AddFavorite(url, title);
	    }
	}
	
	function validate_search() {
		error_fld = get_id('search-error');
		search_fld = get_id('search');
		search_val = search_fld.value;
		if (search_val == '') {error_fld.innerHTML = 'Please enter something to search for';return false;}
		if ( search_val.length < 4) {error_fld.innerHTML = 'Oops! your keyword is a little too short';return false;}
	}

	function opacity(id, opacStart, opacEnd, millisec) {
		//speed for each frame
		var speed = Math.round(millisec / 100);
		var timer = 0;
		
		//determine the direction for the blending, if start and end are the same nothing happens
		if(opacStart > opacEnd) {
			for(i = opacStart; i >= opacEnd; i--) {
				setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		} else if(opacStart < opacEnd) {
			for(i = opacStart; i <= opacEnd; i++) {
				setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
				timer++;
			}
		}
	}
	
	//change the opacity for different browsers
	function changeOpac(opacity, id) {
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
	
	//
	function sub_menu(hover, hover_to) {
		el_reset = get_id(reset);
		el_reset.className = reset_to;
		get_id('sub-'+reset).className = 'hidden';
		
		el_hover = get_id(hover);
		el_hover.className = hover_to;
		get_id('sub-'+hover).className = '';
		get_id('subnav-main').className = hover_to.replace('selected','off');
		
		reset = hover;
		reset_to = hover_to.replace('selected-','');
	}
	
	function show_lightbox() {
		get_id('lightbox').style.height = (document.body.offsetHeight+20)+'px';
		opacity('lightbox', 0, 100, 500); 
		show_element('lightbox');
		location.href = '#';
		//document.body.style.overflow = 'hidden';
	}
	
	function hide_lightbox() {
		opacity('lightbox', 100, 0, 100); 
		document.body.style.overflow = '';
		hide_element('lightbox');
	}
	
	function show_popup(el) {
		show_lightbox();
		show_element(el);
		center_element(el);
	}
	
	function center_element(str_element) {
		el = get_id(str_element);
		viewHeight = document.body.scrollTop;
		docWidth = document.body.offsetWidth;
		elWidth = el.offsetWidth;
		el.style.top = '20px';
		el.style.left = ((docWidth - elWidth)/2)+'px';
		//alert(document.body.offsetHeight);
	}
	
	// functions for ajax stuff ===================================================================
    
    function	ajax_object()	{
    	
    	// create ajax object based on the user's browser
    	// author : michael ocansey; saturday oct 4, 2008
    	obj_xmlhttp	=	null;
    	try	{	// mozilla & webkit based browsers + IE7
    		obj_xmlhttp	=	new XMLHttpRequest();
    	}	catch (e)	{
    		try	{	// not mozilla, IE6+ maybe
    			obj_xmlhttp	=	new ActiveXObject(Msxml2.XMLHTTP);
    		}	catch (e)	{
    			try	{	// not IE6+ too? ah! IE5+ possibly
    				obj_xmlhttp	=	new ActiveXObject(Microsoft.XMLHTTP);
    			}	catch (e)	{	// sorry man!! it's 2008 for hevs sake
    				return false;
    			}
    		}
    	}
    }
    
    function	ajax_request (method, url, asynchronism, fxn)	{
    	
    	// sends an ajax request to the specified url
    	// method : type=>string; how should the request be sent (get, post)
    	// url : type=>string; url to send request to
    	// asynchronism : type=>boolean; should this be an asynchronous request?
    	// fxn : type=>function; function to run when onreadystatechange
    	// author : michael ocansey; sunday oct 5, 2008
        ajax_object();
		
    	if ( obj_xmlhttp != null )	{
			obj_xmlhttp.open(method, url, asynchronism);
    		//obj_xmlhttp.onreadystatechange=eval(fxn);
			obj_xmlhttp.onreadystatechange=function(){
					if (obj_xmlhttp.readyState == 4) {
						if (obj_xmlhttp.status == 200) {
							eval(fxn);
						}
					} else {
						// request failed
					}
				};
    		obj_xmlhttp.send(null);
    	}
    }

    function	get_response()	{
    	
    	// returns the results of an ajax request
    	// author : michael ocansey; monday sept 6, 2009
		return obj_xmlhttp.responseText;
    }
	
	function	set_unset_default_text (str_el, str_str) {
		el = get_id(str_el);
		if (el.value == str_str) {
			el.value = '';
			el.className = el.className.replace('gray', '');
		} else if (el.value == '') {
			el.value = str_str;
			el.className = el.className + 'gray';
		}
			
	}
	
	function check_email_availability(str_email) {
		str_url = url_prefix+"register/_register_ajax.php?chkemail="+str_email;
		ajax_request ("get", str_url, true, "write_email_status()");
		get_id('td_email').className = 'spinner';
	}
	
	function write_email_status() { 
		str_returnVal = get_response();
		if (str_returnVal == 'ERR') {
			get_id('td_email').className = 'failed';
			get_id('td_email').title = 'Email entered is invalid';
			display_message("Please enter a valid email", "error");
		} else if (str_returnVal == '0') {
			get_id('td_email').className = 'passed';
			get_id('td_email').title = '';
		} else  if (str_returnVal == '1') {
			get_id('td_email').className = 'failed';
			get_id('td_email').title = 'Email already taken';
			display_message("This email is already taken. Please enter another", "error");
		}
	}
	
	function check_registration_form(){
            if(check_passwords() && validate('fname,lname,email,pass,pass2,gender,yob,country')) {
                return true;
            } else {
                return false;
            }
	}
	
	function check_passwords() {
            pass1 = get_id('pass');
            pass2 = get_id('pass2');
            if (pass1.value != pass2.value) {
                display_message("Your passwords do not match.", 'error');
                pass1.className = pass2.className = 'title error-field';
                return false;
            }
            else {
                pass1.className = pass2.className = 'title';
                return true;
            }
	}
	
	function check_gender() {
            if(!get_id('male').checked && !get_id('female').checked) {
                display_message('Please specify your gender', 'error');
                return false;
            } else {
                return true;
            }
	}
	
	function check_names() {
		f = get_id('fname');
		l = get_id('lname');
		if($.trim(f.value) != '' && $.trim(f.value).length < 2){
			display_message('Your first name is too short', 'error');
			return false;	
		}
		if($.trim(l.value) != '' && $.trim(l.value).length < 2){
			display_message('Your last name is too short', 'error');
			return false;	
		}
		if($.trim(l.value) != '' && $.trim(f.value) != '' && ($.trim(f.value) == $.trim(l.value))){
			display_message('Your first name cannot be the same as your last name', 'error');
			return false;	
		}
		return true;
	}
	
	function check_other(val, el) {
		if(val == 'Other') show_element(el);
	}
	
	function	set_focus(str_el) {get_id(str_el).focus();}
	
	function recommend() {
		
	}
	
	function add_remove(srcel, dest, avalue) {
		srcel = get_id(srcel);
		dest = get_id(dest);
		if(srcel.checked) {
			dest.innerHTML += '; ' + avalue; 
		} else {
			dest.innerHTML = dest.innerHTML.replace('; '+avalue, '');
		}
	}

