/*********************
 * Open popup window *
 *********************/

var windowobject = "";

function popupWindow(url, width, height, attributes, windowname, target_url)
{
	var args = "width=" + width + ",height=" + height + ",scrollbars=yes,menubar=no";

	if (attributes && ((typeof attributes) == "string")) {
		args += (attributes.indexOf("resize") != -1) ? ",resizable=yes" : ",resizeable=no";
		args += (attributes.indexOf("tool") != -1) ? ",toolbar=yes" : ",toolbar=no";
		args += (attributes.indexOf("menu") != -1) ? ",menubar=yes" : ",menubar=no";
		args += (attributes.indexOf("loc") != -1) ? ",location=yes" : ",location=no";
		args += (attributes.indexOf("status") != -1) ? ",status=yes" : ",status=no";
	} else {
		args += ",resizeable=yes,toolbar=no,location=no,status=yes";
	}	

	windowobject = window.open(url, windowname, args);

	if (windowobject) {
		windowobject.focus();
	}

	if (target_url) {
		windowobject.location = target_url;
	}
}

/*****************************************
 * Select all items in a multiple select *
 *****************************************/

function selectAllMultiple(id)
{
	for (b = 0; b < document.getElementById(id).options.length; b++) {
		document.getElementById(id).options[b].selected = true;
	}
}

/*************************************************
 * Change button text while completing operation *
 *************************************************/

function changeButtonText(button, text)
{
	button.value = text;
	button.disabled = true;
	button.blur();
}

function resetButtonText(button, text)
{
	button.value = text;
	button.disabled = false;
	button.blur();
}

function formatNumber(value, decimals)
{
	if (decimals == 0) {
		return Math.round(value);
	} 

	var power = Math.pow(10, decimals);
	value = '' + Math.round(value*power)/power;

	var decimal_point = value.indexOf('.')

	if (decimal_point < 0) {
		var decimal_value = '';

		for (var i = 0; i < decimals; i++) {
			decimal_value = decimal_value + '0';
		}

		return value + '.' + decimal_value;
	} else {
		var decimal_value = '' + value.substr(decimal_point+1);

		if (decimal_value.length < decimals) {
			var pad_zeroes = decimals-decimal_value.length;

			for (var i = 0; i < pad_zeroes; i++) {
				decimal_value = decimal_value + '0';
			}

			return value.substring(0, decimal_point) + '.' + decimal_value;
		}
	}

	return value;
}

function formatMoney(number, include_cents)
{
	if (number < 0) {
		var negative_number = true;
		number = Math.abs(number);
	} else {
		var negative_number = false;
	}

	var dollars_string;
	var cents_string;
	var mod;

	var dollars_number = Math.floor(number-0) + '';
	var cents_number = number - 0;

	cents_number = Math.round(((cents_number) - Math.floor(cents_number)) *100);

	if (cents_number == 100) {
		dollars_number = dollars_number-0+1 + '';
		cents_number = 0;
	}

	dollars_string = formatNumberSeparated(dollars_number);

	if (cents_number < 10) {
		cents_string = '.0' + cents_number;
	} else {
		cents_string = '.' + cents_number;
	}

	if (include_cents == 'auto') {
		if (cents_number == 0) {
			include_cents = false;
		} else {
			include_cents = true;
		}
	}

	if (negative_number) {
		dollars_string = '-' + dollars_string;
	}

	if (include_cents == true) {
		return dollars_string + cents_string;
	} else {
		return dollars_string;
	}
}

function formatNumberSeparated(number)
{
	var number = Math.floor(number-0) + '';

	if (number.length <= 3) {
		number_string = (number == '' ? '0' : number);
	} else {
		mod = number.length%3;
		number_string = (mod == 0 ? '' : (number.substring(0,mod)));

		for (var i=0 ; i < Math.floor(number.length/3) ; i++) {
			if ((mod ==0) && (i ==0)) {
				number_string += number.substring(mod+3*i,mod+3*i+3);
			} else {
				number_string += ',' + number.substring(mod+3*i,mod+3*i+3);
			}
	        }
	}

	return number_string;
}

function escapeHtml(string)
{
	string = string.replace(/</g, "&lt;");
	string = string.replace(/>/g, "&gt;");

	return string;
}

/**************
 * Toggle box *
 **************/

function toggleBox(link_id, box_id, action)
{
	var box_element;

	box_element = document.getElementById(box_id);

	if ((box_element.style.display == 'none' || box_element.style.display == '' || action == 'expand') && action != 'close') {
		if (link_id) {
			document.getElementById(link_id).innerHTML = '-';
		}

		box_element.style.display = 'block';
	} else {
		if (link_id) {
			document.getElementById(link_id).innerHTML = '+';
		}

		box_element.style.display = 'none';
	}
}

function showBox(id)
{
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).style.display = 'block';
}

function hideBox(id)
{
	document.getElementById(id).style.display = 'none';
	document.getElementById(id).style.visibility = 'hidden';
}

function changeBoxState(id)
{
	var box_element;

	box_element = document.getElementById(id);

	if (box_element.style.display == 'none' || box_element.style.display == '') {
		box_element.style.visibility = 'visible';
		box_element.style.display = 'block';
	} else {
		box_element.style.display = 'none';
		box_element.style.visibility = 'hidden';
	}
}

function togglePanel(link_id, panel_id)
{
	var link_element = document.getElementById(link_id);
	var panel_element = document.getElementById(panel_id);

	if (panel_element.style.display == 'none' || panel_element.style.display == '') {
		link_coords = getCoords(link_id);

		panel_top = (link_coords.y + link_element.offsetHeight) + 'px';
		panel_left = link_coords.x + 'px';

		panel_element.style.top = panel_top;
		panel_element.style.left = panel_left;

		panel_element.style.visibility = 'visible';
		panel_element.style.display = 'block';
	} else {
		panel_element.style.display = 'none';
		panel_element.style.visibility = 'hidden';
	}

	link_element.blur();
}

function setBoxPosition(box_id, reference_element_id)
{
	var box_element = document.getElementById(box_id);
	var reference_element = document.getElementById(reference_element_id);

	reference_coords = getCoords(reference_element_id);

	box_top = (reference_coords.y + reference_element.offsetHeight) + 'px';
	box_left = reference_coords.x + 'px';

	box_element.style.position = 'absolute';
	box_element.style.top = box_top;
	box_element.style.left = box_left;
}

function getCoords(id)
{
	var x = 0
	var y = 0;

	element = document.getElementById(id);

	if (element.offsetParent){
		while (element.offsetParent){
			x += element.offsetLeft;
			y += element.offsetTop;
			element = element.offsetParent;
		}
	} else if (element.x || element.y) {
		x = element.x;
		y = element.y;
	}

	/* IE bug */
	x += document.body.offsetLeft;
	y += document.body.offsetTop;

	return { x:x, y:y };
}

function getWindowSize() 
{
	var x,y;
	if (self.innerHeight) { // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return { x:x, y:y };
}

function getScrollCoords() 
{
	var x,y;
	if (self.pageYOffset) { // all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	} else if (document.body) { // all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	return { x:x, y:y };
}

function goToTopOfPage()
{
	window.scrollTo(0,0);
}

function jumpToBottom(id) 
{
	if ($("#" + id).css("display") == "none") {
		return;
	}

	var box_bottom = getCoords(id).y + $("#" + id).height();
	var scroll_bottom = getScrollCoords().y + getWindowSize().y;

	// opera doesn't show all of the contents, extend document
	if (window.opera && box_bottom > $("html").height()) {
		$("html").height(box_bottom);
	}

	if (box_bottom > scroll_bottom) {
		window.scrollBy(0, (box_bottom-scroll_bottom));
	} 

}

/*******
 * Geo *
 *******/

var all_provinces = new Array();

function countryHasProvinces(country_code)
{
	if (country_code == 'us' || country_code == 'ca') {
		return true;
	} else {
		return false;
	}
}

function provincesChangeCountry(country_code_select_id, province_code_select_id)
{
	var country_code;
	var province_code_select_element;
	var provinces;
	var i;
	var j;
	var in_country;
	var selected_index;
	var province_code;
	var text;
	var selected;

	country_code = $("select#" + country_code_select_id).val();

	province_code_select_element = document.getElementById(province_code_select_id);

	provinces = all_provinces[province_code_select_id];

	if (country_code && countryHasProvinces(country_code)) {
		province_code_select_element.length = 0;
		province_code_select_element.options[0] = new Option('', '');

		j = 1;
		in_country = false;
		selected_index = -1;
		for (i = 0; i < provinces.length; i++) {
			if (provinces[i].length == 1) {
				if (provinces[i][0] == country_code) {
					in_country = true;
					continue;
				} else if (in_country) {
					break;
				}
			}

			if (!in_country) {
				continue;
			}

			province_code = provinces[i][0];
			text = provinces[i][1];
			selected = provinces[i][2];

			province_code_select_element.options[j++] = new Option(text, province_code);

			if (selected) {
				selected_index = j-1;
			}
		}

		if (selected_index != -1) {
			province_code_select_element.selectedIndex = selected_index;
		}
	} else {
		province_code_select_element.length = 0;
	}
}

function updateNumChars(field_id, display_id, max_length) 
{
	var field_element = $('#' + field_id);
	var display_element = $('#' + display_id);
	var num_chars = field_element.val().length;

	if (num_chars > (max_length - 2)) {
		var val = field_element.val().substr(0, max_length - 2);
		field_element.val(val);
		num_chars = max_length - 2;
	}

	/* Extra space is IE hack */
	display_element.text(num_chars + '/' + max_length + ' ');
}

function checkJobTitle(title)
{
	if (title.match(/(Online Ad Posting Job|Ad Posting Job|Online Data Entry|webyallowpage|Zengroup|Zen group|Online Ad Matter|On line admater|tejinfoline|Data Entry job)/i)) {
		alert("The title of your job, " + title + ", matches a common Work at Home/Data Entry/Wellness/Affiliate job title.  These types of jobs are not allowed and you will not be able to post your job.");

		return 'prevent';
	} else if (title.match(/(Work.*Home|From Home|At Home|Your Own Boss|Your.*Boss|Stay at Home|Data Entry|Data Keyer|Moms|Home Data|Make.*Money|Home.*Business|Fast Money|More Money|Home.*Based|Home.*Typing|Home.*Typist|Typing.*Home|Typist.*Home|Your Own Hours|WomenWorkingSmarter|Home.*Rebate|Rebate.*Home|Online Typing Jobs|Online Ad Posting|ProsperingFromHome|Home.*Wellness|Wellness.*Home|Earn Extra Income|Home.*Income|Income.*Home|webyallowpage)/i)) {
		alert("YOUR JOB WILL NOT BE APPROVED.\n\nThe title of your job, " + title + ", matches a common Work at Home/Data Entry/Wellness/Affiliate job title.  These types of jobs are not allowed and will not be approved.  Most Work at Home/Data Entry/Wellness/Affiliate jobs are Multi-level Marketing (MLM) schemes, which are illegal.\n\nViolators will be reported.\n\nYOUR JOB WILL NOT BE APPROVED.");

		return 'warning';
	}

	return 'ok';
}

function checkJobDescription(description)
{
	if (description.match(/(myzengroup|bdrc\.biz|payformoney\.com|unitechinfoservice|webyallowpage|zenithservices\.net|typeinternational\.com|tejinfoline|jobs\.redstar\.com\.np|amitinfoservice\.com|Unitech info|30,000.*9884555566|home.*9884555566|part time.*9884555566|infopayformoney|postadsearnmoney\.com|magicmoneyearn|Lifelook|onlinemagicmoney|thenetjobs\.com|smartjobsforall\.com|in-shop\.co\.in|DreamEndlessSuccess\.com|womensdreamteam\.com|Hitch Your Wagon To A Star)/i)) {
		alert("The description of your job matches a common Work at Home/Data Entry/Wellness/Affiliate job description.  These types of jobs are not allowed and you will not be able to post your job.");

		return 'prevent';
	} else if (description.match(/(300.*500.*per week|500.*1000.*per day|womensdreamteam|lifechanges123|womenworkingwiser|dreamworkathome|foremmagrace|Hitch Your Wagon.*Star|HereForMyFamily|MomsWorkSmarter|WomensDestiny|WomenWorkingSmarter|Online Ad Posting|ProsperingFromHome|Ameriplan|webyallowpage|payformoney|clickordials|bdrc\.biz)/i)) {
		alert("YOUR JOB WILL NOT BE APPROVED.\n\nThe description of your job matches a common Work at Home/Data Entry/Wellness/Affiliate job description.  These types of jobs are not allowed and will not be approved.  Most Work at Home/Data Entry/Wellness/Affiliate jobs are Multi-level Marketing (MLM) schemes, which are illegal.\n\nViolators will be reported.\n\nYOUR JOB WILL NOT BE APPROVED.");

		return 'warning';
	} else if (description.match(/\w@\w/i)) {
		alert("Your job description contains an email address in it.\n\nTo receive applications via email, we recommend entering your email address in the Send Resumes section below, and removing your email address from the job description.\n\nUsing the Send Resumes option, you will still receive applications via email, but your email address will not be displayed publicly.  The application emails you receive will also be in a standard, convenient format.");

		return 'ok';
	}

	return 'ok';
}

/******
 * JT *
 ******/

var JT = {
	BulkPricing: false,

	ajax: {
		getJSON: function(url, params, callback)
		{
			$.getJSON(url, params, callback);
		}
	}
};
