//-----------------------------------------------------------------------------------------------------
//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;
}

//Checks URL against pattern
function chkURL(tmpStr)
{
	var url_pat = /^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w-\.\/?%&amp;,=#@\/:]*)?/;
	return(url_pat.test(tmpStr));
}

//Checks coupon code
function chkCoupon(tmpStr)
{
	var coupon_pat = /^[A-Z0-9]+$/;
	return(coupon_pat.test(tmpStr));
}

function showWindow(fName)
{
	window.open(fName, '', 'width=880,height=600,left=50,scrollbars=1,top=50,toolbar=0,status=1,resizable=1,menubar=1,location=0');
}

function validateSearch()
{
	var obj = document.frmSearch;
	obj.keywords.value = strTrim(obj.keywords.value);
	if(obj.keywords.value == '' || obj.keywords.value == 'Enter your Keywords')
	{
		alert("Please enter your Keywords.");
		obj.keywords.focus();
		return false;
	}
	return true;
}

function validateSubscribe()
{
	var obj = document.frmNewsletter;
	if(obj.newsletter_email.value == '')
	{
		alert("Please enter your Email Address.");
		obj.newsletter_email.focus();
		return;
	}
	if(!chkEmail(obj.newsletter_email.value))
	{
		alert("Please enter a valid Email Address.");
		obj.newsletter_email.focus();
		obj.newsletter_email.select();
		return;
	}
	$('#newsletter_loader')[0].innerHTML = '<img src="img/ajax-loader1.gif" alt="Loader" style="vertical-align:middle"> <span style="color:#B3B3B3;font-size:7pt;">Please wait</span>';
	params = 'email='+ obj.newsletter_email.value;
	params+= '&hash='+Math.floor(Math.random()*11);
	 $.ajax({
	   type: "GET",
	   data: params,
	   url: rewriteBase+"subscribe_newsletter.php",
	   success: function(retVal){
			switch(retVal)
			{
				case "SUCCESS":
					subscribeMsg = 'Thank you for subscribing to the Spiller Furniture 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 Spiller Furniture 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 = "Error occured.";
					break;
			}
			$('#newsletter_msg')[0].innerHTML = subscribeMsg;
			$('#newsletter_msg').slideDown();
			$('#newsletter_loader')[0].innerHTML = '<input type="button" value="Submit" class="btn" onclick="javascript:validateSubscribe()">'
			obj.newsletter_email.value = "";
	   }
   });
}


$(document).ready(function(){
$('#lnk_gallery').hover(function(){
			$('#mnu_blk').fadeIn(700);
		},
		function(){
		}
		);

$('#mnu_blk').hover(function(){
			$('#sub_menu').fadeIn();
		},
		function(){
			$('#mnu_blk').fadeOut();
		}
		);

$('#lnk_credit').hover(function(){
			$('#mnu_blk').fadeOut();
		},
		function(){
		}
		);
$('#keywords').hover(function(){
			$('#mnu_blk').fadeOut();
		},
		function(){
		}
		);

$('#search').hover(function(){
			$('#mnu_blk').fadeOut();
		},
		function(){
		}
		);

});

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 validateCreditApplicantExport()
{
	if(confirm('Are you sure you want to export the Credit Applicants?'))
	{
		alert("Please wait a few seconds while the list is exported.");
		parent.frames['fraCAExport'].location.href = 'export_csv.php?opt=credit_applicant';
	}
}

function validateApplicantAccess()
{
	var accessPassword = prompt("Please enter the Password to access Credit Applicants", "");
	if(accessPassword)
	{
		params = 'access_password='+ accessPassword;
		params+= '&hash='+Math.floor(Math.random()*11);
		 $.ajax({
		   type: "GET",
		   data: params,
		   url: "validate_credit_applicant.php",
		   success: function(retVal){
				switch(retVal)
				{
					case "SUCCESS":
						self.location = 'applicants.php';
						break;
					default:
						alert("Invalid Password. Access Denied.");
						break;
				}
		   }
	   });
	}
}
