// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}

function strip(txt) {
  return txt.replace(/^\s+|\s+$/g,"");
}



function formatWithDecimals (n,decimals) {
      decimals=(decimals>=0?decimals:2)
    var t = Math.floor(n*Math.pow(10,decimals)+0.5).toString();
    while (t.length < decimals+1) t = "0"+t;
          var i = t.length-decimals;
      if(decimals)
          t = t.substr(0,i)+"."+t.substr(i); // insert decimal separator (a dot or comma)
    for (i -= 3; i > 0; i -= 3) 
            t = t.substr(0,i)+","+t.substr(i); // insert thousands separators (commas or dots)
    return t;
}


function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(ms) {
var sec = Math.floor(ms/1000)
ms = ms % 1000
t = three(ms)

var min = Math.floor(sec/60)
sec = sec % 60
t = two(sec) + ":" + t

var hr = Math.floor(min/60)
min = min % 60
t = two(min) + ":" + t



return t
}

function repositionInfoTab(){
	
		var offset = "50px";
		if (window.pageYOffset > 0) 
		{
			offset = window.pageYOffset;
		}
		jQuery("#info_message").css("top", offset);
		
	}
	function startProcess(text){
		repositionInfoTab();
		jQuery("#info_message").css("left", (jQuery(window).width() - text.length) / 2);
		jQuery("#info_message #text").html(text);
		jQuery("#info_message").show();
	}
	
	function endProcess(){
		jQuery("#info_message #text").html("");
		jQuery("#info_message").hide();
	}


function showModal(divId, title, height, width){
	
	
	
	showDialog(divId, title, height, width, {
					"Close" : function() {
						$(this).dialog("close");
						$(this).dialog("destroy");
					
					}
					
				}); 
	
	
	
}

/*function alert(message){
	$("#confirm").html(message);
	
		showDialog("confirm", "Alert", 100, 300, {
					
					"OK" : function() {
						$(this).dialog("close");
						$(this).dialog("destroy");
					}
					
				}); 
}*/

function confirm(message, callback, oncancel){
	
	jQuery("#sdblDialog").html(message);
	
	showDialog("sdblDialog", "Confirm", 80, 400, {
					"Cancel" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
						if (oncancel != null){
							oncancel();
						}
					
					},
					"OK" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
						if (callback != null) {
							callback();
						}
					
					}
					
				}); 
	
}


function upgrade(message){
	
	jQuery("#sdblDialog").html(message);
	
	showDialog("sdblDialog", "You need to upgrade", 200, 550, {
					"Cancel" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
					
					},
					"Upgrade" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
						window.location="/upgrade";
						
					
					}
					
				}); 
	
}

function upgradeTopUp(message){
	
	if ($WL_ON == true && $AF_ON == false) 
	{
		if ( $WL_TOKENS  == true) {
			jQuery("#sdblDialog").html(message);
		
			showDialog("sdblDialog", "You need to top up your tokens", 250, 550, {
						"Cancel" : function() {
							jQuery(this).dialog("close");
							jQuery(this).dialog("destroy");
						
						},
						"Top Up" : function() {
							jQuery(this).dialog("close");
							jQuery(this).dialog("destroy");
							window.location="/addcredits/sendible";
						
						}
						
					}); 
		}
		else 
		{
			jQuery("#sdblDialog").html(message);
			showDialog("sdblDialog", "You have used up all your message credits", 250, 550, {
						"Cancel" : function() {
							jQuery(this).dialog("close");
							jQuery(this).dialog("destroy");
						}
					}); 
		}
	}
	
	else {
	
	jQuery("#sdblDialog").html(message);
	
	showDialog("sdblDialog", "You need to upgrade or top up your tokens", 250, 550, {
					"Cancel" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
					
					},
					"Upgrade" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
						window.location="/upgrade";
						
					
					},
					"Top Up" : function() {
						jQuery(this).dialog("close");
						jQuery(this).dialog("destroy");
						window.location="/addcredits/sendible";
					
					}
					
				}); 
	}
	
}

function showDialog(divId, title, height, width, buttons){
	
	var $dialog = jQuery("#" + divId);
	$dialog.dialog(
	{
		modal: false,
		title: title,
		width: width,
		height: height,
		buttons: buttons,
		close: function(){$dialog.dialog("destroy");}
	}
	).show();
	//tb_show(title, "#TB_inline?height="+height+"&width="+width+"&inlineId="+divId);
	
}

function showModalDialog(divId, title, height, width, buttons){
	
	var $dialog = jQuery("#" + divId);
	$dialog.dialog(
	{
		modal: false,
		title: title,
		width: width,
		height: height,
		buttons: buttons,
		close: function(){$dialog.dialog("destroy");}
	}
	).show();
	//tb_show(title, "#TB_inline?height="+height+"&width="+width+"&inlineId="+divId);
	
}

function showDialogContent(content, title, height, width, buttons){
	jQuery("#sdblDialog").html(content);
	var $dialog = jQuery("#sdblDialog");
	if (buttons == null){
		buttons = {"Close": function() {
			$dialog.dialog("destroy");}};
	}
	$dialog.dialog(
	{
		title: title,
		width: width,
		height: height,
		close: function(){$dialog.dialog("destroy");},
		buttons: buttons
	}
	).show();
	
	//tb_show(title, "#TB_inline?height="+height+"&width="+width+"&inlineId="+divId);
	
}

function showDialogAlert(targetId, content, title, height, width, buttons){
	jQuery("#"+targetId).html(content);
	var $dialog = jQuery("#" + targetId);
	if (buttons == null){
		buttons = {"Close": function() {
			$dialog.dialog("destroy");}};
	}
	$dialog.dialog(
	{
		title: title,
		width: width,
		height: height,
		close: function(){$dialog.dialog("destroy");},
		buttons: buttons
	}
	).show();
	
	//tb_show(title, "#TB_inline?height="+height+"&width="+width+"&inlineId="+divId);
	
}

function showFlashDialog(title, html, width, height) {

	jQuery("#imgDialog").html(decodeURIComponent(html));
	
	var $dialog = jQuery("#imgDialog");
	
		var buttons = {"Close": function() {
			$dialog.dialog("destroy");}};
	
	$dialog.dialog(
	{
		title: title,
		close: function(){$dialog.dialog("destroy");},
		buttons: buttons,
		width: width,
		height: height
	}
	).show();
}

function showImageDialog(title, url, width, height) {

	jQuery.facebox("<img src=\""+url+"\" style=\"text-align:center;max-height: 800px;\"/>");
	return;
	jQuery("#imgDialog").html("<img src=\""+url+"\" style=\"text-align:center;max-height: 800px;\"/>");
	
	var $dialog = jQuery("#imgDialog");
	
		var buttons = {"Close": function() {
			$dialog.dialog("destroy");}};
	
	$dialog.dialog(
	{
		title: title,
		close: function(){$dialog.dialog("destroy");},
		buttons: buttons,
		width: width,
		height: height
	}
	).show();
}

function showHTMLDialog(title, html, width, height) {
	jQuery("#imgDialog").html(html);
	var $dialog = jQuery("#imgDialog");
	
		var buttons = {"Close": function() {
			$dialog.dialog("destroy");}};
	
	$dialog.dialog(
	{
		title: title,
		close: function(){$dialog.dialog("destroy");},
		buttons: buttons,
		width: width,
		height: height
	}
	).show();
	
	
}


function setAutoCompleteContacts(sendToTextBoxId, widthInPx) 
{
		
		jQuery.get("/contacts/filter/all?q=", 
            function(data) {
			//alert(data);
                data = data.split("\n\r");
                jQuery("#"+sendToTextBoxId).autocomplete( data, {
                minChars:1,
                multiple: true,
                delay: 100,
                width: widthInPx + 'px',
				matchContains: true
                });
					 jQuery("#"+sendToTextBoxId).blur();
				 //jQuery("#"+sendToTextBoxId).focus();
            });
	
}


	function toggleMask(item, value, isClick) {
	
		if (jQuery(item).val() == value) {
				jQuery(item).attr("style", "color:black;");
				
				jQuery(item).val("");
				
				
			}
			else 
				if (jQuery(item).val() == "" && !isClick ) {
					jQuery(item).attr("style", "color:darkgray;");
					jQuery(item).val(value);
					
				
				}
		
		
		
	}

function setTimeZoneOnly(timeZoneId){
	var currentDate = new Date();
	jQuery("#"+ timeZoneId).val(currentDate.getTimezoneOffset().toString());
	
}

function setTimeZone(dateId, monthId, yearId, hrsId, minsId, timeZoneId){
	var currentDate = new Date();
	
	jQuery("#" + dateId).val(currentDate.getDate());
	jQuery("#" + monthId).val(currentDate.getMonth() + 1);
	if (yearId != null) {
		jQuery("#" + yearId).val(currentDate.getFullYear());
	}
	//jQuery("#schedule_recurs_until_3i").val(currentDate.getDate());
	//jQuery("#schedule_recurs_until_2i").val(currentDate.getMonth()+1);
	//jQuery("#schedule_recurs_until_1i").val(currentDate.getFullYear());
	var hrs;
	var mins;
	if (currentDate.getHours() < 10) {
		hrs = '0' + currentDate.getHours();
	}
	else {
		hrs = currentDate.getHours();
	}
	if (currentDate.getMinutes() < 10) {
		mins = '0' + currentDate.getMinutes();
	}
	else {
		mins = currentDate.getMinutes();
	}
	if (hrsId != null) {
	
	jQuery("#" + hrsId).val(hrs);
	jQuery("#" + minsId).val(mins);
	}
	jQuery("#"+ timeZoneId).val(currentDate.getTimezoneOffset().toString());
	//jQuery("#schedule_recurs_until_4i").val(hrs);
	//jQuery("#schedule_recurs_until_5i").val(mins);
	
}


