var relativeDirectory = "../btns/";

//pre-load images
function preLoad(){
	if (document.images){
	
		var names = new Array("", "", "", "");
	
		for (var i=0; i < names.length; i++) {	
			var navOn = new Array();
			navOn[i] = new Image();
			navOn[i].src = relativeDirectory + names[i] + "_on.gif";
		}
	}
}




//rollover
function roll(name, state, type){

	if (document.images){
		eval("document." + name + ".src = " + "\""  + relativeDirectory  + type + "." + name + "_" + state + ".gif\"");

	}
}

function goHere(where){
	document.location = where;
}

function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

//this is for encoding email
var this_one = '&';var sign = ';';var is = '#';var at = '4';var the = '6';var s = '<a h';var a = 'f="ma';var t = 'o:';var r = 'ilt';var m = 're';




//this is for sending email from form data
function send_form(formid, return_div_id){
        var elem = $(formid).elements;
		var pars = 'form_id='+formid+'&page='+page_link_base64;
        for(var i = 0; i < elem.length; i++){
			if (elem[i].type != 'button'){
				if (elem[i].className.match(/required/)){
					if (elem[i].value.length < 1){
						alert('Please complete all required fields before submitting this form');
						return;
					}
					if (elem[i].name == 'Email'){
						if (validateEmail(elem[i].value) != true){
							alert('Your appear to have entered an invalid email address');
							return;
						}
					}
				}
				if (elem[i].type.toLowerCase() == 'radio'){
					elem[i].value = $RF(elem[i].name);
		            pars += "&" + elem[i].name + "=" + elem[i].value;
				}
				else if (elem[i].type.toLowerCase() == 'checkbox'){
					if(elem[i].checked){
			            pars += "&" + elem[i].name + "=" + elem[i].value;
					}
				}
				else{
		            pars += "&" + elem[i].name + "=" + encodeURIComponent(elem[i].value);
				}
			}
        }
		//alert(pars);
		var return_div_id = return_div_id ? return_div_id : 'form_results';
		//somehow, this screws up the ajax call from the popup...
		//$(return_div_id).innerHTML = '<img src="/common/images/ajax-loader.gif" alt="" width="24" height="24" border="0">';


		var url = '/include/process_request.php';
		var myAjax = new Ajax.Updater(
		return_div_id,
			url, 
			{
				method: 'post', 
				parameters: pars, 
				evalScripts: true
			});
}




//this is for logging in
function ro_login(formid, return_div_id){
        var elem = $(formid).elements;
		var pars = '';
        for(var i = 0; i < elem.length; i++)
        {
			if (elem[i].type != 'button'){
				if (elem[i].className.match(/required/)){
					if (elem[i].value.length < 1){
						alert('Please complete all required fields before submitting this form');
						return;
					}
					if (elem[i].name == 'Email'){
						if (validateEmail(elem[i].value) != true){
							alert('Your appear to have entered an invalid email address');
							return;
						}
					}
				}
				if (elem[i].type.toLowerCase() == 'radio'){
					elem[i].value = $RF(elem[i].name);
		            pars += "&" + elem[i].name + "=" + elem[i].value;
				}
				else if (elem[i].type.toLowerCase() == 'checkbox'){
					if(elem[i].checked){
			            pars += "&" + elem[i].name + "=" + elem[i].value;
					}
				}
				else{
		            pars += "&" + elem[i].name + "=" + elem[i].value;
				}
			}
        }
		
		var url = '/include/login.php';
		var myAjax = new Ajax.Updater(
		return_div_id,
			url, 
			{
				method: 'post', 
				parameters: pars, 
				evalScripts: true
			});
			return false;
}





//I don't think we're giong to use this; it's to post to your wall.
//using LIKE instead.
function fb_post(){
	u=location.href;
	//t=document.title;
	t='Red Oak Realty';
	
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
}




function save_favorite(mlsid, action, return_div){
	var pars = {
	'mlsid' : mlsid,
	'action' : action,
	'return_div' : return_div
};
	var return_div_id = 'results';
	var url = 'save_favorite.php';
	var myAjax = new Ajax.Updater(
	return_div_id,
		url, 
		{
			method: 'post', 
			parameters: pars, 
			evalScripts: true
		});
}





//popup a screen
function ro_screen(type, vars){
	if (type == 'login'){
		show_popup(400,400, '/screens/login.php?pg='+vars);
	}
	if (type == 'login_favorite'){
		show_popup(400,400, '/screens/login_favorite.php?pg='+vars);
	}
	if (type == 'login_save_search'){
		show_popup(400,400, '/screens/login_favorite.php?search=TRUE&pg='+vars);
	}
	else if (type == 'signup'){
		show_popup(400,400, '/screens/signup.php');
	}
	else if (type == 'lost_password'){
		show_popup(400,400, '/screens/lost_password.php');
	}
	else if (type == 'message'){
		show_popup(500,400, '/screens/message.php?vars='+vars);
	}
	else if (type == 'email_message'){
		show_popup(500,400, '/screens/email_message.php?vars='+vars);
	}
	else if (type == 'hp_img'){
		show_popup(600,400, '/screens/hp_img.php');
	}
	else if (type == 'send_friend'){
		show_popup(460,280, '/screens/send_friend.php?pg='+vars);
	}
	else if (type == 'invite_friend'){
		show_popup(460,280, '/screens/invite_friend.php');
	}
	else if (type == 'tweak_search'){
		show_popup(400,420, '/screens/tweak_search.php?'+vars);
	}
	else if (type == 'fb_logged_in'){
		show_popup(400,400, '/screens/fb_logged_in.php');
	}
	else if (type == 'thanks'){
		show_popup(400,400, '/screens/thanks.php');
	}
	else if (type == 'favorite'){
		show_popup(400,400, '/screens/favorite.php?'+vars);
	}
	else if (type == 'newsletter_sample'){
		show_popup(600,500, '/east-bay-ca-neighborhoods/include/sample_newsletter.php');
	}
	else if (type == 'newsletter_signup'){
		show_popup(350,400, '/screens/newsletter_signup.php?'+vars);
	}
	else if (type == 'save_search'){
		show_popup(350,400, '/screens/save_search.php?'+vars);
	}
	else if (type == 're_save_search'){
		show_popup(350,400, '/screens/re_save_search.php?'+vars);
	}
	else if (type == 'search_saved'){
		show_popup(350,400, '/screens/search_saved.php');
	}
	else if (type == 'settings_saved'){
		show_popup(400,200, '/screens/settings_saved.php');
	}
	
	else if (type == 'logout'){
		show_popup(400,250, '/screens/logout.php?', vars);
		/*
		$('login').innerHTML = "<img style=\"margin-bottom: -2px; margin-right: 8px;\" src=\"/common/images/carat_rt.png\" alt=\"\" height=\"12\" border=\"0\"><a style=\"color: #fff\" href=\"#\" onclick=\"ro_screen('login', ''); return false\">Login or Register</a>&nbsp;&nbsp;|&nbsp;&nbsp;510.292.2000&nbsp;&nbsp;|&nbsp;&nbsp;<a style=\"color: #fff\" href=\"#\" onclick=\"ro_screen('message'); return false\"><img style=\"margin-bottom: -6px;\" src=\"/common/images/icon_email.png\" alt=\"\" width=\"21\" height=\"18\" border=\"0\"></a>";
*/
	}
	
}




function validateEmail(email) { 
    var re = /^\S+@\S+$/;
    return re.test(email);
} 















/*BASE 64 encode*/


var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

  function encode64(input) {
     input = escape(input);
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;

     do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
           enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
           enc4 = 64;
        }

        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
     } while (i < input.length);

     return output;
  }

  
  function dropdown(n){
		if (drop = $(n)){
			var pos = Position.cumulativeOffset($('n_'+n));
			drop.style.top = Math.floor(pos[1] + 47)+'px';
			drop.style.left = pos[0]+'px';
			drop.show();
		}
	}
	
	
	
	
	
	function base64_encode (data) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['atob'] == 'function') {
    //    return atob(data);
    //}
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        enc = "",
        tmp_arr = [];

    if (!data) {
        return data;
    }

    data = this.utf8_encode(data + '');

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1 << 16 | o2 << 8 | o3;

        h1 = bits >> 18 & 0x3f;
        h2 = bits >> 12 & 0x3f;
        h3 = bits >> 6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    enc = tmp_arr.join('');

    switch (data.length % 3) {
    case 1:
        enc = enc.slice(0, -2) + '==';
        break;
    case 2:
        enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}
function utf8_encode (argString) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // +   bugfixed by: Rafal Kukawski
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'

    if (argString === null || typeof argString === "undefined") {
        return "";
    }

    var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
    var utftext = "",
        start, end, stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.slice(start, end);
            }
            utftext += enc;
            start = end = n + 1;
        }
    }

    if (end > start) {
        utftext += string.slice(start, stringl);
    }

    return utftext;
}
