function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trimboth( value ) {
	return LTrim(RTrim(value));
}
// this function validate currency format
function isCurrency (val) {
	str = trimboth(val);
	return (/^\d+(\.\d{1,2})?$/.test(str));
}

function checkSendMyBid(roomBidPaymentId, hotelprice, nights){
	var mybidprice = $("#mybidprice_"+roomBidPaymentId).val();
	
	if(isCurrency(mybidprice)){
		if(parseFloat(mybidprice) * nights > hotelprice)
		{
			if (!confirm("Your bid price bigger than hotel counter price, are you sure to submit?")){
				return false;
			}
		}
		
		if (confirm("Are you sure to make new bid?")){
			var htmlTag = "<input type='hidden' name='task' value='user_bid_price' />" +
						  "<input type='hidden' name='room_bid_payments_id' value='"+roomBidPaymentId+"' />" +
						  "<input type='hidden' name='nights' value='"+nights+"' />" +
						  "<input type='hidden' name='bidder_bid_price' value='"+mybidprice+"' />";
			$("body").append("<form id='send-mybid' action='' method='post' style='display:none;'></form>"); 
			$("#send-mybid").html(htmlTag);
			$("#send-mybid").submit();
			return true;
		}
	}else{
		alert("Please input a valid number!");
		return false;
	}
}

//errychen code
function acceptCounterBid(room_bid_payments_id, roomBidId, hotelBidPrice)
{
	if (confirm("Are you sure to accept the counterbid?"))
	{
		var htmlTag = "<input type='hidden' name='room_bid_payments_id' value='"+room_bid_payments_id+"' />" +
				 	  "<input type='hidden' name='room_bid_id' value='"+roomBidId+"' />" +
				 	  "<input type='hidden' name='hotel_bid_price' value='"+hotelBidPrice+"' />" +
				 	  "<input type='hidden' name='task' value='accept_counterbid' />";
		$("body").append("<form id='accept-counterbid' action='' method='post' style='display:none;'></form>");
		$("#accept-counterbid").html(htmlTag);
		$("#accept-counterbid").submit();
		return true;
	}
	
	return false;
	
}

function changePrice(nights, roomBidPayment) 
{
	var mybidprice = $("#mybidprice_"+roomBidPayment).val();
	if(isNaN(mybidprice) || mybidprice == "")
	{
		alert("please input a valid number!");
		$("#mybidprice_"+roomBidPayment).attr("value", "");
		return false;
	}
	var totalprice = mybidprice * nights;
	$(".total_price_"+roomBidPayment).text("$" + totalprice.toFixed(2) );
	$(".fee_"+roomBidPayment).text("$" + (parseFloat(totalprice) * 0.1 + 5).toFixed(2) );
	$(".arrival_price_"+roomBidPayment).text("$" + (parseFloat(totalprice) * 0.9).toFixed(2) );
}

function counterBid(roomBidPayment)
{
	$('div.bid-price').css("border", "1px solid #499b0b");
	$('#yourbid_'+roomBidPayment).show();
	$('#control_'+roomBidPayment).hide(); 
	$("div.hotel").css({border:"0", color:"#c0a065"});
	$("div.hotel_charge").remove();
	return false;
}
//errychen code end

function checkCancelMyBid(room_bid_payments_id){
	if(confirm("You will no longer have access to this area until you complete the bidding process again.\nAre you sure to cancel all bids on this room?")){
		$("body").append("<form id='cancelledbid' action='' method='post' style='display:none;'></form>");
		$("#cancelledbid").append("<input name='room_bid_payments_id' value='"+room_bid_payments_id+"' />");
		$("#cancelledbid").append("<input name='task' value='user_bid_cancel_all' />");
		$("#cancelledbid").submit();
		return true;
	}
	return false;	
}

function checkLogin(loginForm){
	var name=trimboth(loginForm.bidusername.value);
	var pass=trimboth(loginForm.bidpassword.value);
	if(name == ""){
		alert("Please enter userName!");
		loginForm.bidusername.focus();
		return false;
	}else if(pass == ""){
		alert("Plase enter Password!");
		loginForm.bidpassword.focus();
		return false;
	}
	return true;
}
