var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



function showDiv(id) {
	document.getElementById(id).style.display = "block";
}

function showHideDiv(id,show) {
	if (show == 1) {
		document.getElementById(id).style.display = "block";
	} else {
		document.getElementById(id).style.display = "none";
	}
}

function showDivFromFormField(field,id) {
	// switch based on the type of field
	switch (field.type) {
		case "radio":
			//if ()
			break;
	}
}

function swapDiv(hide,show) {
	document.getElementById(hide).style.display = "none";
	document.getElementById(show).style.display = "block";
}

function test_profile_popup(path, width, height) {
	
	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var results = confirm("You are about to enter a TEST profile. No data will be saved. Click 'Cancel' and click the 'Live Profile' button if you need to enter live data. Otherwise click 'OK' to continue with the TEST profile.");
	
	if (results) {
		profile_popup(path, width, height);
	}

}

function profile_popup(path, width, height) {
	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var extra = "";
	if (width != 0) {
		extra += "width="+width;
	}
	if (height != 0) {
		extra += ", height="+height;
	}
	
	//alert('Extra Info: '+extra)
	
	new_window = window.open(path,"",extra + ',toolbar=0,menubar=0,location=0,directories=0,scrollbars=1,resizable=1');
	
	/*
	status  	The status bar at the bottom of the window.
	toolbar 	The standard browser toolbar, with buttons such as Back and Forward.
	location 	The Location entry field where you enter the URL.
	menubar 	The menu bar of the window
	directories 	The standard browser directory buttons, such as What's New and What's Cool
	resizable 	Allow/Disallow the user to resize the window.
	scrollbars 	Enable the scrollbars if the document is bigger than the window
	height 	Specifies the height of the window in pixels. (example: height='350')
	width 	Specifies the width of the window in pixels.
	*/
	
}

function externalLink(path,width,height) {
	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var extra = "";
	if (width != 0) {
		extra += "width="+width;
	}
	if (height != 0) {
		extra += ", height="+height;
	}
	
	new_window = window.open(path,"");
}

function popup_link(path,width,height) {
	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var extra = "";
	if (width != 0) {
		extra += "width="+width;
	}
	if (height != 0) {
		extra += ", height="+height;
	}
	
	new_window = window.open(path,"",extra);
}

function print_ex(path) {

	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var extra = "";
	if (width != 0) { extra += "width="+width; }
	if (height != 0) { extra += ", height="+height; }
	
	new_window = window.open(path,"",extra + ',toolbar=0,menubar=0,location=0,directories=0,scrollbars=1,resizable=1');

}

function searchSort(string,parentDiv,parent_aDiv,sort) {
	
	var searchLetter = string.toLowerCase();
	var letter = searchLetter.replace(/ /g, "_");
	var searchString = '';
	if (sort) {
		searchString = 'sort_letter_'+letter;
	} else {
		searchString = letter;
		
	}
	
	var p_aDiv = document.getElementById(parent_aDiv);
	
	// get the sort parent div
	if (parentDiv.indexOf(',') != -1) {
		pDivIds = parentDiv.split(',');
	} else {
		pDivIds = new Array();
		pDivIds[0] = parentDiv;
	}
	
	for (x = 0; x < pDivIds.length; x++) {
		var pDiv = document.getElementById(pDivIds[x]);
		for (i = 0; i < pDiv.childNodes.length; i++) {
			var style = '';
			if (pDiv.childNodes[i].nodeName == 'DIV') {
				if (pDiv.childNodes[i].id.indexOf('sort_letter_') != -1) {
					if (letter == 'all') {
						pDiv.childNodes[i].style.display = '';
					} else {

						if (pDiv.childNodes[i].id.indexOf(searchString) == -1) {
							style = 'none';
						}

						pDiv.childNodes[i].style.display = style;
					}
				}
			}
		}
	}
	
	
	// Make sure the sort options have their styles updated
	if (letter.length == 1 || letter == "all") {
		for (i = 0; i < p_aDiv.childNodes.length; i++) {
			if (p_aDiv.childNodes[i].nodeName == 'A') {
				
				if (p_aDiv.childNodes[i].id.indexOf('search_sort_option_'+letter) != -1) {
					p_aDiv.childNodes[i].style.color = "#909093";
					p_aDiv.childNodes[i].style.textDecoration = "none";
					p_aDiv.childNodes[i].style.cursor = "default";
				} else {
					p_aDiv.childNodes[i].style.color = "#002966";
					p_aDiv.childNodes[i].style.textDecoration = "underline";
					p_aDiv.childNodes[i].style.cursor = "pointer";
				}
			}
		}
	}
}

function clearDefaultText(id) {
    document.getElementById(id).value = "";
}

function resetDefaultText(id,t) {
    document.getElementById(id).value = t;
}

function clearSearchBox(id,text) {

	if (document.getElementById(id).value == text) {
		document.getElementById(id).value = "";
	} else if (document.getElementById(id).value == "") {
		document.getElementById(id).value = text;
	}

}

function validatePassword(id) {
	
	// We have three things we need to check for a valid password.
	// 1: Must contain eight (8) or more characters.
    // 2: Must contain at least one (1) number AND one (1) of the following special character ( ! @ $ % ^ & * , ; ).
    // 3: Must contain BOTH upper and lower case letters.

	var field = document.getElementById(id);
	var value = getInputValue(id);
	var passed = true;
	
	if (value.length < 8 || value.search(/\d/) == -1 || 
		value.search(/\W/) == -1 || value.search(/[A-Z]/) == -1 || 
		value.search(/[a-z]/) == -1) {
		
		passed = false;
		
	}
	
	if (passed) {
		// We're all set!
		document.getElementById(id+'_checkPassword_status').innerHTML = "Valid Password!";
		document.getElementById(id+'_checkPassword_status').style.color = "#1D9904";
		document.getElementById(id+'_checkPassword_status').style.fontWeight = "bold";
	} else {
		// Their is a dup!
		document.getElementById(id+'_checkPassword_status').innerHTML = "Invalid Password! Try Again!";
		document.getElementById(id+'_checkPassword_status').style.color = "#E41B17";
		document.getElementById(id+'_checkPassword_status').style.fontWeight = "bold";
		setInputValue(id,"");
	}
	
}

function confirmPasswordMatch(confirm,actual) {
	
	var pw = getInputValue(actual);
	var cpw = getInputValue(confirm);
	
	if (pw != cpw) {
		document.getElementById(confirm+'_checkPassword_status').innerHTML = "Passwords do not match!";
		document.getElementById(confirm+'_checkPassword_status').style.color = "#E41B17";
		document.getElementById(confirm+'_checkPassword_status').style.fontWeight = "bold";
		// setInputValue(confirm,"");
	} else {
		document.getElementById(confirm+'_checkPassword_status').innerHTML = "Match!";
		document.getElementById(confirm+'_checkPassword_status').style.color = "#1D9904";
		document.getElementById(confirm+'_checkPassword_status').style.fontWeight = "bold";
	}
	
}

function validateForm(fields,form) {
	
	var aFields = fields.split(',');
	var error = "";
	var fieldEl = "";
	
	for (i = 0; i < aFields.length; i++)
	{
		fieldEl = document.getElementById(aFields[i]);
		if (fieldEl.type == "select-one")
		{
			if (fieldEl[fieldEl.selectedIndex].value == "NULL")
			{
				error += "You must select a " + fieldEl.title + ".\n\r";
			}
		}
		else 
		{
			if (fieldEl.value == '')
			{
				if (fieldEl.type == "file")
				{
					error += "You must select a " + fieldEl.title + " to upload.\n\r";
				}
				else
				{
					error += "You must fill in the " + fieldEl.title + " field.\n\r";
				}
			}
		}
	}
	
	if (error != "")
	{
		alert(error);
		return false;
	}
	else
	{
		// alert ('Am i here');
		document.getElementById(form).submit();
		return true;
	}	
}

function clearField(id,t) {
	
	if (document.getElementById(id).value == t) {
		document.getElementById(id).value = "";
	} else if (document.getElementById(id).value == "") {
		document.getElementById(id).value = t;
	}
	
}

function igz_debug(what){
	if(window.console) {
    	console.log(what);
	}
}


/* ***** MJ SPECIFIC FUNCTION ***** */
jQuery(document).ready(function() {
	jQuery("#menu div.menu_category").mouseover(function() {
		// jQuery('#menu div.menu_subcats').slideUp(500);
		jQuery(this).next("div.menu_subcats").slideDown(500).siblings("div.menu_subcats").slideUp("slow");
		// siblings("div.menu_subcats").slideUp("slow")
	});
});

function printCoupon(url) {
	window.open (url,"printcoupon","menubar=0,resizable=0,width=550,height=350"); 
}

function showAweberForm(url) {
	window.open (url,"aweber","menubar=0,resizable=0,width=350,height=300"); 
}

function showEmailForm(listing_url) {

	var append = USE_SEO_URL == '' ? "?" : "&";
	var url = HTTP_ROOT + USE_SEO_URL + "listing/email/" + append + "url=" + listing_url;
	window.open (url,"email a friend","menubar=0,resizable=0,width=675,height=400"); 

}


function showGalleryTitle(id) {
	jQuery('#' + id).show();
}

function hideGalleryTitle(id) {
	jQuery('#' + id).hide();
}

function loadSubcategories(id) {

	// First off we need to get the form fields
	var catID = jQuery("#"+id).val();
	
	var url = HTTP_ROOT + USE_SEO_URL + "ajax/getAllSubCategories/";
	var params = 'category_id=' + escape(catID);
	
	createRequest();
	request.open("POST",url,true);
	
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	
	request.onreadystatechange = x_loadSubcategories;
	request.send(params);	
	return false;

}

function x_loadSubcategories() {
	
	if (request.readyState == 4) {
		var updatedInfo = request.responseText;
		
		var results = eval("(" + updatedInfo + ")");
		
		jQuery('#sub_category_id').find('option').remove().end();

		if (results['data'].length >= 1) {
		
			jQuery('#sub_category_id').
				append(jQuery("<option></option>").
				attr("value","NULL").
				text("--- Choose Sub Category ---")); 
		
			jQuery.each(results['data'], function(key, value) {
				jQuery('#sub_category_id').
				append(jQuery("<option></option>").
				attr("value",value['sub_category_id']).
				text(value['sub_category_name'])); 
			});
		} else {
			jQuery('#sub_category_id').
				append(jQuery("<option></option>").
				attr("value","NULL").
				text('no sub categories'));
		}
		
	}

}

function horoscope_viewContent(id,extended) {

	if (extended == '1') {
		jQuery('#horoscope_viewoverview_' + id).hide();
		jQuery('#horoscope_viewextended_' + id).show();
	} else {
		jQuery('#horoscope_viewoverview_' + id).show();
		jQuery('#horoscope_viewextended_' + id).hide();
	}

}

function horoscope_showNewSign(id) {

	jQuery('#horoscope_main_sign_wrapper > div').each(function() {
		jQuery(this).hide();
	});
	
	jQuery('#horoscope_sign_wrapper_'+id).show();

}

function showEvents(date) {
	
	var category = jQuery('#event_category').val();
	
	var url = HTTP_ROOT + USE_SEO_URL + "ajax/getEventsByDay/";
	var params = 'date=' + escape(date) + "&category=" + escape(category);
	
	createRequest();
	request.open("POST",url,true);
	
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	
	request.onreadystatechange = x_showEvents;
	request.send(params);	
	return false;

}

function x_showEvents() {

	if (request.readyState == 4) {
		var updatedInfo = request.responseText;
		
		var results = eval("(" + updatedInfo + ")");
		
		if (results['code'] != '1') {
			alert(results['message']);
			return;
		}

		jQuery('#event_details').hide();
		
		if (results['data'].length >= 1) {
		
			temp = results['data'][0]['eveDate'].split("-");
			date = temp[1] + "-" + temp[2] + "-" + temp[0];
			jQuery('#event_details_date').html(date);
			jQuery('#event_details_content').html('');
			for (i = 0; i < results['data'].length; i++) {
				title = results['data'][i]['eveTitle'];
			
				temp = results['data'][i]['eveDate'].split("-");
				var date = temp[1] + "-" + temp[2] + "-" + temp[0];
			
				var startTime = results['data'][i]['eveStartTime'] != '' ? " at " + results['data'][i]['eveStartTime'] : '';
				var endTime = results['data'][i]['eveEndTime'] != '' ? " - " + results['data'][i]['eveEndTime'] : '';
				var image = results['data'][i]['eveImage'] != '' ? '<img src="' + HTTP_ROOT + 'images/d/EVENT/' + results['data'][i]['eveImage'] + '" alt="' + title + '" width="100" />' : '';
				var description = results['data'][i]['eveDescription'] != '' ? results['data'][i]['eveDescription'] : '';
			
				html  = '<div class="events_wrapper">\n';
				html += '<h2>' + title + '<span class="event_datetime">' + startTime + endTime + '</span></h2>\n';
				html += '\t<div class="event_home_content clearfix">\n';
				html += '\t\t' + image + '\n' + description + '\n';
				html += '\t</div>\n';
				html += '</div>\n';
			
				jQuery('#event_details_content').append(html);
			
			}
		
			positionAbsDiv_Center('event_details');
			jQuery('#event_details').show();
		
		}
		

	}

}

function setRecurringEventFields(id,div_id) {

	if (jQuery('#'+id).is(':checked')) {
		jQuery('#'+div_id).show();
		jQuery('#event_date_wrapper').hide();
		
		jQuery('#db_validate_fields').val('eveStartDate,eveEndDate,eveTitle,catID');
	} else {
		jQuery('#'+div_id).hide();
		jQuery('#event_date_wrapper').show();
		
		jQuery('#db_validate_fields').val('eveDate,eveTitle,catID');
	}
	
}

function eventValidateForm() {

	var validateFields = jQuery('#db_validate_fields').val();
	return validateForm(validateFields, 'event_form');;

}

/* ******************************** */

