// checks that an input string looks like a valid email address.
var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
var isPhone_re       = /^[0-9\s\.()\-+\,]{5,16}$/;
var ck_username=/^[a-zA-Z0-9_\-]+$/;
function isEmail (s)
{
   return String(s).search (isEmail_re) != -1;
}

function isPhone (s)
{
   return String(s).search (isPhone_re) != -1;
}

function isInteger(s)
{
	return (s.toString().search(/^[0-9]+$/) == 0);
}

function stripSlashes(str)
{
	if(str){
        return str.replace(/\\/g, '');
	}
}


// Check if string is non-blank
var isNonblank_re    = /\S/;
function isNonblank (s)
{
	return String (s).search (isNonblank_re) != -1
}

// Check if string is a whole number(digits only).
var isWhole_re       = /^\s*\d+\s*$/;
function isWhole (s)
{
	return String(s).search (isWhole_re) != -1
}

// check 0-9 digit
function regIsDigit(fData)
{
	var reg = new RegExp("^[0-9]$");
	return (reg.test(fData));
}


function keyValid(e, validchars)
{ 
	var key='', keychar='';
	key = getKeyCode(e);
	if(key == null)
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)
	  return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	  return true;
	return false;
}
function keyRestrict(e, validchars)
{ 
	var key='', keychar='';
	key = getKeyCode(e);
	if(key == null)
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) == -1)
	  return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	  return true;
	return false;
}

function getKeyCode(e)
{
	if(window.event)
		return window.event.keyCode;
	else if(e)
		return e.which;
	else
		return null;
}

function isValidUsername(username){
	if(!ck_username.test(username)){
		return false;
	}else{
		return true;	
	}
}

function URL_validatio(val)
{
    if((/^[A-Za-z]+:\/\/[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$/).exec(val) == null){
    return true;
    }else{
    return false;
    }
}
function validURL(val)
{
	var regx = "/https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*/";
	var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	if(jQuery.trim(val)!="")
	{
		if(regexp.test(val) == false)
		{
			return false;
		}
	}
}


function dateFormat(date){
	if(date != '0000-00-00 00:00:00'){
		var month=new Array(12);
		month[0]="January";	month[1]="February";month[2]="March";month[3]="April";month[4]="May";month[5]="June";month[6]="July";month[7]="August";month[8]="September";month[9]="October";month[10]="November";month[11]="December";
		if(date!=''){
		date = date.split(' ');
		var dateObj = new Date(date[0]);
		var time = date[1].split(':');
		var hour = dateObj.setHours(time[0]);		
		var minute = parseInt(dateObj.setMinutes(time[1]));
		var seconds = parseInt(dateObj.setSeconds(time[2]));
		/*date = date.split('-');
		year = date[0];month=date[1];
		fetchdateandtime=date[2].split(' ');
		fetchdate = fetchdateandtime[0];
		time = fetchdateandtime[1];*/
		var formatdate = dateObj.getDate();
		formatdate+= " " +month[dateObj.getMonth()];
		formatdate+= " " +dateObj.getFullYear();		
		if(time[0] > '12' && time[0] < '23'){
			formatdate+=" At "+dateObj.getHours() - 12;	
		}else{
			formatdate+=" At "+dateObj.getHours();
		}
		if(time[1]!=00){
			formatdate+=":"+dateObj.getMinutes();
			if(hour > 12 && hour < 23){
				formatdate+=" PM ";	
			}else{
				formatdate+=" AM ";
			}
			if(seconds!=00){
				//formatdate+=":"+dateObj.getSeconds();
			}
		}
		
		alert(formatdate);
		return formatdate;		
		}
	}	
}


function changeCityHtml(country){
	var country_id = jQuery.trim(country);
	var stateName = '';
	var htmlContent = '';
	if(country_id != ''){
		$('select[name="state"]').html('');
		$.ajax({
          url: root_path+"ajax-php/ajax-select-city.php",
          type: "POST",
          data: "country_id="+country_id,
          dataType: "json",
          async:false,
          success: function(resp){
                 if(resp.ErrorCode==0){
					 htmlContent +='<option value="0">-- Select Region/State --</option>';
					 for(var i=0; i<resp.AllStates.length; i++){
						 stateName = stripSlashes(resp.AllStates[i].name);
						 htmlContent +='<option value="'+resp.AllStates[i].zone_id+'">'+stateName+'</option>';
					 }
					 
                        $('select[name="state"]').html(htmlContent);
                 }else{
                        $('select[name="state"]').html('');
                 }
          }
        });		
	}
}

function addNewsSubscription(){
	jQuery('#success_subscribe_div').hide();
	jQuery('#err_subscribe_div').hide();
	jQuery('#ajax_loading_img').hide();
	var message_txt=null;
    var element='';
	var subscribe_email = jQuery.trim($('#news_subscribe_email').val());
	
	var root_path = 'http://www.yogicameron.com/';
	
	if(subscribe_email =='' || subscribe_email=='Enter Your Email Address'){
		message_txt = 'Please enter your email address.';
        element=jQuery('#news_subscribe_email');
	}else if(subscribe_email!='' && isEmail(subscribe_email)==false){
		message_txt = 'Please enter your valid email address.';
		element=jQuery('#news_subscribe_email');
	}
	
	if(message_txt != null){
        jQuery('#success_subscribe_div').animate({height: 'hide', width: 'hide', opacity: 'hide'}, 300);
        jQuery('#err_subscribe_div').animate({height: 'show', width: 'show', opacity: 'show'}, 300);
        jQuery('#err_subscribe_div').html(message_txt);
        element.focus();
        return false;
    }else{
		$('#ajax_loading_img').html('<img src="'+root_path+'images/loader.gif" alt="loading..." />').show();
		$.ajax({
          url: root_path+"ajax-php/ajax-subscribe-newsletter.php",
          type: "POST",
          data: "subscribe_email="+subscribe_email,
          dataType: "json",
          async:false,
          success: function(resp){
                 if(resp.ErrorCode==0){
					 	jQuery('#ajax_loading_img').hide();
					  	jQuery('#err_subscribe_div').animate({height: 'hide', width: 'hide', opacity: 'hide'}, 300);
        			  	jQuery('#success_subscribe_div').animate({height: 'show', width: 'show', opacity: 'show'}, 300);
        			  	jQuery('#success_subscribe_div').html(resp.ErrorMessage);
                 }else{
					 	jQuery('#ajax_loading_img').hide();
                        jQuery('#success_subscribe_div').animate({height: 'hide', width: 'hide', opacity: 'hide'}, 300);
						jQuery('#err_subscribe_div').animate({height: 'show', width: 'show', opacity: 'show'}, 300);
						jQuery('#err_subscribe_div').html(resp.ErrorMessage);
                 }				
				setTimeout("jQuery('#success_subscribe_div').hide();",5000);

          }
        });
		return false;
	}
}

