//-----------------------------------------------------------------------------------------------------
//gen functions
//-----------------------------------------------------------------------------------------------------
function strTrim(tmpStr)
{
	tmpStr = tmpStr.replace(/^\s+/,"");//remove leading
	tmpStr = tmpStr.replace(/\s+$/,"");//remove trailing
	return tmpStr;
}
//-----------------------------------------------------------------------------------------------------
function trimFields()
{
	for(var i=0; i < obj.elements.length; i++)
	{
		if(obj.elements[i].type == "text" || obj.elements[i].type == "textarea" || obj.elements[i].type == "password")
		{
			obj.elements[i].value = strTrim(obj.elements[i].value);
		}
	}
}
//-----------------------------------------------------------------------------------------------------
function chkEmail(tmpStr)
{
	var email_pat = /^[a-z][a-z0-9_\.\-]*[a-z0-9]@[a-z0-9]+[a-z0-9\.\-_]*\.[a-z]+$/i;
	return(email_pat.test(tmpStr));
}
function refreshCaptcha(imgid)
{
	var img = new Image();
	img.src = 'captcha/show_captcha.php?hash='+parseInt(Math.random() * 10000000000);
	document.getElementById(imgid).src = img.src;
}
function validateSubscribe()
{
	var obj = document.getElementById('frmSubscriber');
	if(obj.subscriber_email.value == '')
	{
		alert("Please enter your Email Address.");
		obj.subscriber_email.focus();
		return;
	}
	if(!chkEmail(obj.subscriber_email.value))
	{
		alert("Please enter a valid Email Address.");
		obj.subscriber_email.focus();
		obj.subscriber_email.select();
		return;
	}
	$('#newsletter_loader')[0].innerHTML = '<img src="img/ajax-loader.gif" alt="Loader" style="vertical-align:middle;padding-left:10px;">';
	params = 'email='+ obj.subscriber_email.value;
	params+= '&hash='+Math.floor(Math.random()*11);
	 $.ajax({
	   type: "GET",
	   data: params,
	   url: "subscribe_newsletter.php",
	   success: function(retVal){
			switch(retVal)
			{

				case "SUCCESS":
					subscribeMsg = 'Thank you for subscribing to the Red Diamond Monthly Newsletter.\nPlease check your email and click the link given in your mail to confirm your subscription.';
					break;
				case "EXISTING":
					subscribeMsg = 'You are already subscribed for the Red Diamond Monthly Newsletter.';
					break;
				case "NEED_ACTIVATION":
					subscribeMsg = 'You need to activate your subscription.\nWe are sending you another mail incase you have deleted our mail received earlier.\nPlease click on the link specified in your mail to activate your subscription.';
					break;
				default:
					subscribeMsg = "You have been disabled for Newsletter Subscription. Please contact Administator";
					break;
			}
			alert(subscribeMsg);
			$('#newsletter_loader')[0].innerHTML = '<input type="text" name="search" class="tbox_search" style="margin-left:-10px;margin-right:4px;" />&nbsp;<img src="img/btn_join.jpg" alt="JOIN" title="JOIN" onclick="javascript:validateSubscribe();" style="vertical-align:top" />';
			obj.subscriber_email.value = "";
	   }
   });
}

function validateSubscriberExport()
{
	if(confirm('Are you sure you want to export the Subscribers?\nYou will only get the list of active Subscribers'))
	{
		alert("Please wait a few seconds while the list is exported.");
		parent.frames['fraSubExport'].location.href = 'export_csv.php?opt=subscribers';
	}
}

function changeImage(imagePath)
{
	document.getElementById('commercial_blk').src = imagePath;
}

function validateSearch()
{
	obj = objSearch;
	trimFields();
	if(obj.keywords.value == '' || obj.keywords.value =='Enter Keywords')
	{
		alert("Please enter keywords to search.");
		obj.keywords.focus();
		return false;
	}
	//All fine
	obj.action = 'search_result.html?keywords=' + obj.keywords.value;
	return true;
}
