/*
Strip whitespace from the beginning and end of a string
Input : a string
	jpw 08/09/2010 removed a comma from the processreport function (after comments.value)
	jpw 08/18/2010 added the checkitemNumber function for validation on config/main.php
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	if(textBox.value == '.') textBox.value = '0.';
	while (textBox.length > 0 && isNaN(textBox.substring(textBox.length-1, textBox.length))) {
			textBox = textBox.substring(0, textBox.length - 1)
	}
	
	textBox = trim(textBox);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/* jpw added this as a special check function because it calls a specific field
	   on the main config form of txtitemsperpage.  Forces the default of 4 if not valid.
*/
function checkitemNumber(textBox)
{
	if(textBox == '.') textBox = '0.';
	if (textBox.length > 0 && isNaN(textBox.substring(textBox))) {
		alert('This value must be numeric.');
		document.frmConfig.txtitemsperpage.value = 4;
	}
	
}



/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function process_report(){
	        $.blockUI({ message: "<h1>Progressing your report</h1>" }); 
 
            $.ajax({ 
                url: 'process-report.php', 
                cache: false, 
				type: "POST",
				dataType: "html",
				data: ({
					   name :  document.getElementById("r_name").value,
					   email :  document.getElementById("r_email").value,
					   phone :  document.getElementById("r_phone").value,
					   type :  document.getElementById("abuse_type").value,
					   shop :  document.getElementById("r_URL").value,
					   comments :  document.getElementById("r_comments").value
					   }),
                complete: function() { 
                    // unblock when remote call returns 
                    $.unblockUI(); 
                } 
			}); 	
}

function ext_check(img){
		var fl= document.getElementById(img).value;
		var brfl = fl.split('.');
		
		fl = brfl[brfl.length - 1];
		
		//alert(fl.toUpperCase());
		
		//if(( fl.toUpperCase() != 'JPG') || (fl.toUpperCase() != 'JPEG') || (fl.toUpperCase() != 'PNG') || (fl.toUpperCase() != 'GIF') || (fl.toUpperCase() != 'BMP')){
		switch(fl.toUpperCase())
		{
			case 'JPG': case 'JPEG': case 'PNG': case 'GIF': case '':
				return true;	
			break;
			default:
				$.blockUI({ message: $('#check_logo'), css: { width: '275px' } });
			  	return false
		}			
		
}


function modal_alert(msg){
document.getElementById("modal_msg").innerHTML = msg;
//$.blockUI({ message: $('#modal_alert'), css: { width: '275px' } });
$.blockUI({ message: '<span id="modal_msg">' + msg +'</span> <br/> <br/><input type="button" id="no" value="OK" onClick="ext_close()" class="box"/>', 
	            css: { width: '275px' } });	
}

function ext_close(){
	    document.getElementById("modal_msg").innerHTML = '';
	    $.unblockUI();
}

function modal_isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		modal_alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function modal_alert2()
{
	$.blockUI({ message: $('#modal_alert2'), css: { width: '275px' } });			
	}

	function ext_close(){

	    $.unblockUI();
}

function setCpicker(el, cl){
							$('#' + el ).ColorPicker({
								color: rgb2hex($('#' + el + '-div').css('backgroundColor')),
								onShow: function (colpkr) {
									$(colpkr).fadeIn(500);
									return false;
								},
								onHide: function (colpkr) {
									$(colpkr).fadeOut(500);
									return false;
								},
								onChange: function (hsb, hex, rgb) {
									//$('#' + el + ' div').css('backgroundColor', '#' + hex);
								},
								onSubmit: function (hsb, hex, rgb) {
									$('#' + el + ' div').css('backgroundColor', '#' + hex);
									$.ajax({
									   type: "POST",
									   url: "config/processConfig.php?action=modifystyle",
									   data: "element=" + cl + "&value=" + hex,
									   async: false,
									   success: function(msg){
											document.getElementById(cl).value = '#' + hex;
											document.getElementById('ifPrev').src = document.getElementById('ifPrev').src
									   }
									 });
								}								 
							});
}

function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 function hex(x) {
  return ("0" + parseInt(x).toString(16)).slice(-2);
 }
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function resetStyle(el, cl, val){
									val = val.replace("#", "")
									
									$.ajax({
									   type: "POST",
									   url: "config/processConfig.php?action=modifystyle",
									   data: "element=" + cl + "&value=" + val,
									   async: false,
									   success: function(msg){
										   	$('#' + el + '-div').css('backgroundColor', val);
											document.getElementById(cl).value = val;
											document.getElementById('ifPrev').src = document.getElementById('ifPrev').src
									   }
									 });	
}

function toggleTabindex(i)
{
	$( '#tabs' ).tabs('select', i);
}

function CKToolbarsSet(){
var t = [
	['Undo','Redo'],['Font','FontSize'],['Bold','Italic','Underline'], ['TextColor','BGColor'], ["Image"],
	['Link'], ['NumberedList','BulletedList'], ['BidiLtr', 'BidiRtl' ], ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], 
	['Source']
];
	
	return t;
}

function ChangeTheme(temp_id, temp_name, folder, thumb){
	
	document.getElementById("current_theme_img").src = "http://www.talents2work.com/src/" + folder + "/" + thumb;
	document.getElementById("current_name").innerHTML = temp_name;
	document.getElementById("template_id").value = temp_id;
}

function confirmThemeUpdate(){
	$.blockUI({ message: '<center><h3>All your customizations will be deleted when you choose a new theme</h3>Do you want to procced?</center><br/><br/><center><a href="javascript:modifyTheme.submit()" class="linkbox" > Yes </a>&nbsp&nbsp<a href="javascript:$.unblockUI();" class="linkbox" > No </a></center><br/>&nbsp&nbsp' });
}
