function show_error(error)
{
    var $dialog = $('<div></div>')
    .html(error)
    .dialog({
            width:450,
            bgiframe: true,
            modal: true,
            autoOpen: true,
            title: '<div class="ui-icon ui-icon-alert"></div>',
            buttons: {
            Close: function() {
                                $(this).dialog('close');
                            }
                    }
    });
}

function show_message(message)
{
    var $dialog = $('<div></div>')
    .html(message)
    .dialog({
            width:450,
            bgiframe: true,
            modal: true,
            autoOpen: true,
            title: '<div class="ui-icon ui-icon-info"></div>',
            buttons: {
            Close: function() {
                                $(this).dialog('close');
                            }
                    }
    });
}


String.prototype.wrapURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
		return url.link(url);
	});
};

String.prototype.parseTwitterUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};

function gridDateFormatter(cellvalue, options, rowObject)
{
	if (cellvalue.indexOf('<br>') > 1)
	{
		return cellvalue;
	}
	else
	{
		var commaPosition = cellvalue.indexOf(',');
		var cutOff = commaPosition + 5;

		return cellvalue.substring(0, cutOff + 1) + "<br>" + cellvalue.substring(cutOff + 1);
	}
}

function gridDateUnFormatter(cellvalue, options, rowObject)
{
	var brPosition = cellvalue.indexOf('<br>');
	if (brPosition > 1)
	{
		return cellvalue.substring(0, brPosition) + cellvalue.substring(brPosition + 3);
	}
	else
	{
		return cellvalue;
	}
}

function gridAgeFormatter(cellvalue, options, rowObject)
{
	return "" + parseInt(currentYear - cellvalue) + "";
}

function uniqid(prefix, more_entropy)
{

if(typeof prefix == 'undefined')
	prefix = "";

	var retId;
	var formatSeed = function(seed, reqWidth) {
	seed = parseInt(seed,10).toString(16); // to hex str

	if (reqWidth < seed.length) { // so long we split
		return seed.slice(seed.length - reqWidth);
	}

	if (reqWidth > seed.length) { // so short we pad
		return Array(1 + (reqWidth - seed.length)).join('0')+seed;
	}
	return seed;
	};


	if (!this.php_js) {
		this.php_js = {};
	}

	if (!this.php_js.uniqidSeed) { // init seed with big random int
		this.php_js.uniqidSeed = Math.floor(Math.random() * 0x75bcd15);
	}
	this.php_js.uniqidSeed++;

	retId = prefix; // start with prefix, add current milliseconds hex string
	retId += formatSeed(parseInt(new Date().getTime()/1000,10),8);
	retId += formatSeed(this.php_js.uniqidSeed,5); // add seed hex string

	if(more_entropy) // for more entropy we add a float lower to 10
		retId += (Math.random()*10).toFixed(8).toString();

	return retId;

}

function checkEmailFormat(theString)
{
    var at="@";
    var dot=".";
    var lat;
    var lstr;
    var ldot;

    str = trimString(theString);
    lat=str.indexOf(at);
    lstr=str.length;
    ldot=str.indexOf(dot);

    if (str.indexOf(at)==-1){
       return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }

    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }

    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }

    if (str.indexOf(" ")!=-1){
        return false;
     }

     return true;
}

function trimString(str)
{
    return str.replace(/^\s+|\s+$/g, '');
}

function renderErrorList(aMsg)
{
	var errorHeader = 'Error(s) encountered or Invalid Data found: \n'
    var errorMsg = '';

	if (aMsg.length > 0)
	{
		for (i=0; i<aMsg.length; i++)
		{
			errorMsg += aMsg[i] + '\n';
		}

		window.alert(errorHeader + errorMsg);
		return false;
	}
	else
	{
		return true;
	}
}

function captilizeFirstLetter(str)
{
	return str.replace(/^./, str.match(/^./)[0].toUpperCase());
}

function checkEnter(e)
{
	var characterCode;

	if(e && e.which)
	{
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else
	{
		if (typeof(e) != "undefined")
		{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}
	}

	if(characterCode == 13)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function numberButtonHighlight(element)
{
	$("#" + element).css({backgroundImage : 'url('+base_url+'/images/bg-bullet.gif)',
						backgroundColor : 'transparent',
						backgroundRepeat: false,
						backgroundPosition : '0 -25px'
					  });
}

function numberButtonUnhighlight(element)
{
	$("#" + element).css({backgroundImage : 'url('+base_url+'/images/bg-bullet.gif)',
						   backgroundColor : 'transparent',
						   backgroundRepeat: false,
						   backgroundPosition: '0 0'
						});
}

function trackerRecord(lang, contentId, contentTypeId)
{
	if (lang > 0 && contentTypeId > 0 && contentId > 0)
	{
		$.ajax({
			type: "POST",
			url: base_url + "/ajax/add-hit",
			async: true,
			dataType: "json",
			data: {language: lang, content_id: contentId, content_type_id:contentTypeId},
			success: function (response) {
				if (response.success == true)
				{
					return true;
				}
				else
				{
					return false;
				}
			},
			error: function () {
				window.alert('Unable to process request.');
				return false;
			}
		});
	}
}

function trackerContact(lang, contentId, contentTypeId, linkType)
{
	if (lang > 0 && contentTypeId > 0 && contentId > 0)
	{
		$.ajax({
			type: "POST",
			url: base_url + "/ajax/track-link",
			async: true,
			dataType: "json",
			data: {language: lang, content_id: contentId, content_type_id:contentTypeId, link_type:linkType},
			success: function (response) {
				if (response.success != false && response.link != false)
				{
                    window.open(response.link, "_blank");
				}
				else
				{
					window.alert('Error while processing request.');
					return false;
				}
			},
			error: function () {
				window.alert('Unable to process request.');
				return false;
			}
		});
	}
}

function startBlock(element, msgParam)
{
	var msg = 'Please wait...';
	if (msgParam === null || msgParam === undefined)
	{
		msgParam = msg;
	}

	blockCss = {
				border: 'none',
				padding: '15px',
				backgroundColor: '#000',
				'-webkit-border-radius': '10px',
				'-moz-border-radius': '10px',
				opacity: .5,
				color: '#fff'
			};

	if (element === null || element === undefined)
	{
		$.blockUI({ message: msg, css:  blockCss});
	}
	else
	{
		$("#" + element).block({ message: msg, css: blockCss});
	}
}

function stopBlock(element)
{
	if (element === null || element === undefined)
	{
		$.unblockUI();
	}
	else
	{
		$("#" + element).unblock();
	}
}
