var alpha = /[^a-zA-Z\s&-\.]/;


function trim(str)
{
   return str.replace(/^\s+|\s+$/g, '');
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		//alert("Please enter valid email address")
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//alert("Please enter valid email address")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Please enter valid email address")
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		//alert("Please enter valid email address")
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Please enter valid email address")
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		//alert("Please enter valid email address")
		return false
	}

	if (str.indexOf(" ")!=-1){
		//alert("Please enter valid email address")
		return false
	}

	return true
}

var digits = "0123456789.";
// non-digit characters which are allowed in  numbers
var NumberDelimiters = ".,-";
// characters which are allowed in international  numbers
// (a leading + is OK)
var validWorldNumericChars = NumberDelimiters + "";
// Minimum no of digits in an international  no.
var minDigitsNumber = 1;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 			
			return false;
		
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkNumeric(strNumeric){
var bracket=3
strNumeric=trim(strNumeric)
if(strNumeric.indexOf("+")>1) return false
if(strNumeric.indexOf("-")!=-1)bracket=bracket+1
if(strNumeric.indexOf("(")!=-1 && strNumeric.indexOf("(")>bracket)return false
var brchr=strNumeric.indexOf("(")
if(strNumeric.indexOf("(")!=-1 && strNumeric.charAt(brchr+2)!=")")return false
if(strNumeric.indexOf("(")==-1 && strNumeric.indexOf(")")!=-1)return false
s=stripCharsInBag(strNumeric,validWorldNumericChars);
return (isInteger(s) && s.length >= minDigitsNumber);
}


function alphanumeric(alphane)
{	
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(hh <= 123)
		{
			
			return 0;
		}
		else	{
			
		}
	}
	return true;
}

function validateUserRegistration(frm)
{
	var nick_name = document.getElementById("nick_name").value;
	var email = trim(document.getElementById("login_email").value);
	var mobile = trim(document.getElementById("mobile").value);
	var password = trim(document.getElementById("pass").value);
	var password_confm = trim(document.getElementById("passc").value);

	if (nick_name == "" ) 
	{
			hideUserRegistrationErrors();
			document.getElementById("nick_nameEmptyError").style.display = "inline";
			document.getElementById("nick_name").select();
			document.getElementById("nick_name").focus();
			return false;
	}
	if(email == "")
	{
			hideUserRegistrationErrors();
			document.getElementById("login_emailEmptyError").style.display = "inline";
			document.getElementById("login_email").select();
			document.getElementById("login_email").focus();
			return false;
	}
	if (echeck(trim(email))==false) 
	{
		hideUserRegistrationErrors();
		document.getElementById("login_emailInvalidError").style.display = "inline";
		document.getElementById("login_email").select();
		document.getElementById("login_email").focus();
		return false;
	}
	if (mobile != "" ) 
	{
		if (echeck(trim(mobile))==false) 
		{
			hideUserRegistrationErrors();
			document.getElementById("mobileInvalidError").style.display = "inline";
			document.getElementById("mobile").select();
			document.getElementById("mobile").focus();
			return false;
		}
	}
	
	if (password == "" ) 
	{
			hideUserRegistrationErrors();
			document.getElementById("passwordEmptyError").style.display = "inline";
			document.getElementById("pass").select();
			document.getElementById("pass").focus();
			return false;
	}
	if(password.length < 5)
	{
			hideUserRegistrationErrors();
			document.getElementById("passwordLengthError").style.display = "inline";
			document.getElementById("pass").select();
			document.getElementById("pass").focus();
			return false;
	}
	if (password_confm == "" ) 
	{
			hideUserRegistrationErrors();
			document.getElementById("passwordConfmEmptyError").style.display = "inline";
			document.getElementById("passc").select();
			document.getElementById("passc").focus();
			return false;
	}
	if (password != "" && password_confm != "") 
	{
		
		if(password != password_confm )
		{
			hideUserRegistrationErrors();
			document.getElementById("passwordNotMatchWithConfmPassError").style.display = "inline";
			document.getElementById("passc").select();
			document.getElementById("passc").focus();
			return false;
		}			
	}
	frm.submit();
}


function hideUserRegistrationErrors(){	
	document.getElementById("nick_nameEmptyError").style.display = "none";
	document.getElementById("login_emailEmptyError").style.display = "none";
	document.getElementById("login_emailInvalidError").style.display = "none";	
	document.getElementById("mobileInvalidError").style.display = "none";
	document.getElementById("passwordEmptyError").style.display = "none";
	document.getElementById("passwordLengthError").style.display = "none";
	document.getElementById("passwordConfmEmptyError").style.display = "none";
	document.getElementById("passwordNotMatchWithConfmPassError").style.display = "none";
}

 function hideSubscribeErrors()
 {	
	document.getElementById("emailEmptyError").style.display = "none";
	document.getElementById("emailInvalidError").style.display = "none";
 }

 function validateSubscribe()
{

	var email = trim(document.getElementById("sub_email").value);
    if (email == "") {
	var email = trim(document.getElementById("sub_email1").value);
	var focus = "sub_email1";
    } else {
	var focus = "sub_email";
    }
	var subloc = document.getElementById("sub_loc");
	var sublocl = subloc.length;
	
	if(email == "")
	{
		hideSubscribeErrors();
		document.getElementById("emailEmptyError").style.display = "inline";
	    // document.getElementById("sub_email").focus();
		document.getElementById(focus).focus();
		return false;
	}
	
	if (email != "" ) 
	{
		if (echeck(trim(email))==false) 
		{
			hideSubscribeErrors();
			document.getElementById("emailInvalidError").style.display = "inline";
		    // document.getElementById("sub_email").focus();
			document.getElementById(focus).focus();
			return false;
		}
	}

	if(sublocl>0)
	{
		for(var l=0;l<sublocl;l++)
		{
			if(subloc[l].value!="" && subloc[l].selected==true)
			{
				return true;
			}
		}
		document.getElementById("locationEmptyError").style.display = "inline";
	}
	return false;
}	

function validateEdit(frm)
{
	var numeric = /^[0-9]+/;
	var not_numeric = /^\d+$/;
	var alphabet = /[^a-zA-Z‚ -‚ñƒA-ƒ“\s&-\.]/;
	var space = /\s+/;
	var japanese = /[^a-zA-Z0-9]/;

	document.getElementById("nicknameEmptyError").style.display = "none";
//	document.getElementById("lastnameNoIntError").style.display = "none";
//	document.getElementById("firstnameNoIntError").style.display = "none";
	document.getElementById("emptyEmailError").style.display = "none";
	document.getElementById("validateEmailError").style.display = "none";
	document.getElementById("validatePassCharError").style.display = "none";
	document.getElementById("validateMobileEmailError").style.display = "none";
	document.getElementById("birthDayYearDigError").style.display = "none";
	document.getElementById("birthDayYearNumError").style.display = "none";
	document.getElementById("birthDayMonNumError").style.display = "none";
	document.getElementById("birthDayDayNumError").style.display = "none";

		if(frm.username.value == "")
		{
			document.getElementById("nicknameEmptyError").style.display = "block";
			frm.username.focus();
			return false;
		}
	if(frm.username.value!="")
	{
		var username = frm.username;

		/*if(space.test(username.value))
		{
			document.getElementById("nicknameNoSpaceError").style.display = "block";
			username.focus();
			return false;
		}
		
		if(japanese.test(username.value))
		{
			document.getElementById("nicknameNoJapaneseError").style.display = "block";
			username.focus();
			return false;
		}*/
	}

	/*if(frm.lastname.value!="")
	{
		var lastname = frm.lastname;

		if(alphabet.test(lastname.value))
		{
			document.getElementById("lastnameNoIntError").style.display = "block";
			lastname.focus();
			return false;
		}

	}
	
	if(frm.firstname.value!="")
	{
		var firstname = frm.firstname;
		
		if(alphabet.test(firstname.value))
		{
			document.getElementById("firstnameNoIntError").style.display = "block";
			firstname.focus();
			return false;
		}

	}
	*/

	if(frm.email.value=="")
	{
		document.getElementById("emptyEmailError").style.display = "block";
		frm.email.focus();
		return false;
	}

	if(frm.email.value!="")
	{
		var email = frm.email;

		if(!echeck(email.value))
		{
			document.getElementById("validateEmailError").style.display = "block";
			email.focus();
			return false;
		}

	}

	if(frm.pass.value!="")
	{
		var pass = frm.pass;

		if(pass.value.length<5)
		{
			document.getElementById("validatePassCharError").style.display = "block";
			pass.focus();
			return false;
		}

	}



	if(frm.mobile_email.value!="")
	{
		var mobile_email = frm.mobile_email;

		if(!echeck(mobile_email.value))
		{
			document.getElementById("validateMobileEmailError").style.display = "block";
			mobile_email.focus();
			return false;
		}

	}

	var byear = frm.byear;
	var bmonth = frm.bmonth;
	var bday = frm.bday;
	if(byear.value!="" || bmonth.value!="" || bday.value!="")
	{
		if(byear.value.length<4)
		{
			document.getElementById("birthDayYearDigError").style.display = "block";
			byear.focus();
			return false;
		}

		if(isNaN(byear.value))
		{
			document.getElementById("birthDayYearNumError").style.display = "block";
			byear.focus();
			return false;
		}

		if(isNaN(bmonth.value))
		{
			document.getElementById("birthDayMonNumError").style.display = "block";
			bmonth.focus();
			return false;
		}

		if(bmonth.value<10 && bmonth.value.indexOf("0")==-1)
		{
			bmonth.value = "0" + bmonth.value;
		}

		if(isNaN(bday.value))
		{
			document.getElementById("birthDayDayNumError").style.display = "block";
			bday.focus();
			return false;
		}

		if(bday.value<10 && bday.value.indexOf("0")==-1)
		{
			bday.value = "0" + bday.value;
		}
	}

	frm.submit();
	return true;
}

function sendBack(frm)
{
	frm.mode.value = "back";
	frm.submit();
	return true;
}

