/*
Function: isValidEmail(email)
Details: Function to check the validity of the passed argument as a valid Email ID
*/
function isValidEmail(email)
{
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}


/*
Function: isValidUrl(s) 
Details: Function to check the validity of the passed argument as a valid URL
*/
function isValidUrl(url)
{
   var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	if(RegExp.test(url)){
			return true;
	} else{
			return false;
	}
} 


/*
Function: isValidString(data)
Details: Function to check a string without special chars
*/
function isValidString(data)
{
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"; 
	for (var i = 0; i < data.length; i++) {
		if (iChars.indexOf(data.charAt(i)) != -1) {
			//alert ("Your string has special characters. \nThese are not allowed.");
			return false;
		}
	}
	return true;
}


/*
Function: validateSiteSubmission
Details: Function to validate the site submission form available on frontend.
*/
function validateSiteSubmission()
{
	var errMsg = 'Sorry! Your request could not be processed due to some error(s) in your form data. Kindly resolve the following issues to procced:\n\n';
	var isError = false;
	
	if ( $.trim($("input[@name=your_name]", $("#frmSubmitSite")).val()) == '' ) {
		errMsg += '* Name cannot be left blank.\n';
		isError = true;
		$("input[@name=your_name]", $("#frmSubmitSite")).val('');
	}
	
	if ( $.trim($("input[@name=your_email]", $("#frmSubmitSite")).val()) == '' ) {
		errMsg += '* Email ID cannot be left blank.\n';
		isError = true;
		$("input[@name=your_email]", $("#frmSubmitSite")).val('');
		
	} else {
		if (!isValidEmail( $.trim($("input[@name=your_email]", $("#frmSubmitSite")).val()) )) {
			errMsg += '* Email ID format is invalid.\n';
			isError = true;			
		}
	}
	
	if ( $.trim($("input[@name=your_site_name]", $("#frmSubmitSite")).val()) == '' ) {
		errMsg += '* Site Name/Title cannot be left blank.\n';
		isError = true;
		$("input[@name=your_site_name]", $("#frmSubmitSite")).val('');
		
	} else {
		if (!isValidString( $("input[@name=your_site_name]", $("#frmSubmitSite")).val() )) {
			errMsg += '* Site Name/Title has special characters which are not allowed here.\n';
			isError = true;
		}
	}
	
	if ( $.trim($("input[@name=your_site_url]", $("#frmSubmitSite")).val()) == '' ) {
		errMsg += '* Site URL cannot be left blank.\n';
		isError = true;
		$("input[@name=your_site_url]", $("#frmSubmitSite")).val('');
		
	} else {
		// Check for valid URL  
		var protocol = $("select[@name=your_site_protocol]", $("#frmSubmitSite")).val();
		var siteURL = protocol + $("input[@name=your_site_url]", $("#frmSubmitSite")).val();
		if (!isValidUrl(siteURL)) {
			errMsg += '* Site URL is invalid.\n';
			isError = true;
		}
	}
	
	if ( $.trim($("textarea[@name=your_site_desc]", $("#frmSubmitSite")).val()) == '' ) {
		errMsg += '* Site Description cannot be left blank.\n';
		isError = true;
		$("textarea[@name=your_site_desc]", $("#frmSubmitSite")).val('');
	}
	
	if (isError) {
		alert(errMsg);
		return false;
	}
	
	return true;
}


/*
Function: validateCategorySelection
Details: Function to validate the site submission form available on frontend.
*/
function validateCategorySelection(obj)
{
	if ($("input.post_category:checked").length > 4) {			
		alert('You can select maximum 3 categories.');	
		$(obj).attr('checked', '');
	}
}

function validateContact()
{
	var isError = false;
	var errStr = 'Could not proceed further, there is/are some error(s) in your form submission. Have a look on the error(s) below:\n\n';
	
	if ($.trim($("input[name=customerName]").val()) == '') {
		errStr += '* Please enter you Name.\n';	
		isError = true;
	}
	
	if ($.trim($("input[name=customerEmailID]").val()) == '') {
		errStr += '* Please enter your Email ID.\n';	
		isError = true;
		
	} else {
		if (!isValidEmail($.trim($("input[name=customerEmailID]").val()))) {
			errStr += '* Incorrect Email ID format.\n';	
			isError = true;																 
		}
	}	
	
	if ($.trim($("input[name=customerSubject]").val()) == '') {
		errStr += '* Please enter Subject.\n';	
		isError = true;
	}
	
	if ($.trim($("textarea[name=comments]").val()) == '') {
		errStr += '* Please enter your Message.\n';	
		isError = true;
	}
	
	if ($.trim($("input[name=verificationCode]").val()) == '') {
		errStr += '* Please enter the Verification code.\n';	
		isError = true;
	}
	
	if (isError) {
		alert(errStr);
		return false;
	}
	else {
		submitForm();
		return false;
	}
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
	return true;
}

function submitForm()
{
	$.ajax({
		type: "POST",
		url: teplateUrl + "/action.php",
		data: "customerName=" + $("input[name=customerName]").val() + "&customerEmailID=" + $("input[name=customerEmailID]").val() + "customerSubject=" + $("input[name=customerSubject]").val() + "&comments=" + $("textarea[name=comments]").val() + "&verificationCode=" + $("input[name=verificationCode]").val(),
		beforeSend: function() {
			// loading code
		},
		success: function(msg){
			//alert( "" );
			var str = msg.split('^|^', 2);
			
			if (str[1] == 'Error') {
				$("#sendData").html(str[0]);
			}
			else {
				$("#sendData").html(str[0]);
				$("input[name=customerName]").val('');
				$("input[name=customerEmailID]").val('');
				$("input[name=customerSubject]").val('');
				$("textarea[name=comments]").val('');
				$("input[name=verificationCode]").val('');
			}
		}
	});
}
