$(document).ready(function(){
	//Examples of how to assign the ColorBox event to elements
	if ($("a.signlnk").length > 0) {
		$("a.signlnk").colorbox({width:"395px", height:"290px", iframe:true});
	}

	//Examples of how to assign the ColorBox event to elements
	if ($("a.poplnk").length > 0) {
		$("a.poplnk").colorbox({width:"950px", height:"380px", iframe:true});
	}

	//Examples of how to assign the ColorBox event to elements
	if ($("a[rel='propics']").length > 0) {
	$("a[rel='propics']").colorbox({transition:"fade"});
	}
	
	if ($("a[rel='propicslight']").length > 0) {
		$("a[rel='propicslight']").lightcolorbox({transition:"fade"});
	}
						  
	//Examples of how to assign the ColorBox event to elements
	if ($("a#tourlnk").length > 0) {
	$("a#tourlnk").colorbox({width:"640px", height:"420px", iframe:true});
	}
	
	if ($("a.citysearch").length > 0) {
	$("a.citysearch").colorbox({width:"395px", height:"290px", iframe:true});
	}
	
	if ($("ul.mainnav > li.welnav").length > 0) {
		var navhelp_pos = $("ul.mainnav").width() - ($("#navhelp").width() + $("#navhelp").position().left);
		var wid = $("ul.mainnav > li.welnav").width() + $("ul.mainnav > li.navlast:last").width() + 75;
		
		$("ul.mainnav > li.welnav").css("margin-left", (navhelp_pos - wid) + 'px');
	}
	
	if ($("a#member_invite").length > 0) {
		$("a#member_invite").colorbox({width:"650px", height:"440px", iframe:true});
	}
});

function submitForm ( frm, btn )
{
	if (btn && document.forms[frm].which_button) document.forms[frm].which_button.value = btn;
	document.forms[frm].submit();
}

function submitEditForm ( frm, btn, selectedEdit )
{
	if (btn && document.forms[frm].which_button) document.forms[frm].which_button.value = btn;
	if (selectedEdit && document.forms[frm].selected_edit) document.forms[frm].selected_edit.value = selectedEdit;
	document.forms[frm].submit();
}

function changeProfile(frm, action) {
	if (action) {
		$(frm).attr("action", action); 
	}
	$(frm).submit();
}

function displaySelectedProfile(slctdid, frm, action) {
	if (action) {
	  document.getElementById('gen').value = document.getElementById(slctdid).value;
		$(frm).attr("action", action); 
	}
	$(frm).submit();
}

function coinmill_convert(obj, srcCurrency)
{
	var src = $("option:selected", obj).val();
	if (src == "")
		src = 'USD';
	
	//$('#lastupdate').html('Rate Update on: ' + currency_Update[src] + '');
	
	$('.cnvrt').each(function() {
		var current = $(this).prev().html();
		var matches = current.match(/[0-9\.]+/);
		if (matches != null) {
			$(this).hide();
			var symbol = '$';
			if (src == 'USD' || src == "AUD" || src == "CAD" || src == "SGD")
				symbol = '$';
			else if (src == 'EUR')
				symbol = '&euro;';
			else if (src == 'JPY' || src == "CNY")
				symbol = '&yen;';
			else if (src == 'AED')
				symbol = 'Dhs';
			else if (src == 'GBP')
				symbol = '&pound;';
			else if (src == 'INR')
				symbol = 'Rs';
			else {
				src = 'USD';
			}
				
      if (srcCurrency == src) {
         $(this).html(symbol + ' ' + CommaFormatted(matches[0])).fadeIn("slow");
      } else {
         $(this).html(symbol + ' ' + CommaFormatted(currency_convert(matches[0], srcCurrency, src))).fadeIn("slow");
      }
		}
	});
}

function show_state(obj, statid, init)
{
		var src = $("option:selected", obj).val();
		if (src == 'United States') {
			var label = $(statid +' dt label').html();
			if (!label.match(/state\/province/i)) {
				label = label.replace(/(state|province)/i, "State");
			
				$(statid +' dt label').html(label);
			}
			
			if (init) { $(statid).show(); } else { $(statid).fadeIn("slow"); }
		} else if (src == 'Canada') {
			var label = $(statid +' dt label').html();
			if (!label.match(/state\/province/i)) {
				label = label.replace(/(state|province)/i, "Province");
				$(statid +' dt label').html(label);
			}
			
			if (init) { $(statid).show(); } else { $(statid).fadeIn("slow"); }
		} else {
			if (init) { $(statid).hide(); } else { $(statid).fadeOut("slow"); }
		}
}

function show_folowing_combo(obj1, obj2) 
{
	if ($(obj1).length > 0) {
		show_state($(obj1), obj2, true);
		
		$(obj1).change(function () {
		show_state(this, obj2, false);
		});
	}
}

function CommaFormatted(amount)
{
	amount += '';
	x = amount.split('.');
	x1 = x[0];
  dec = x[1];

  if (dec.length > 2) {
    dec = dec.substring(0, 2);
  }

	x2 = x.length > 1 ? '.' + dec : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
// end of function CommaFormatted()

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

