function validateLogin() {
	if(trim(document.frmLogin.Nickname.value).length==0)	{
		alert("Nickname should not be blank");
		document.frmLogin.reset();
		document.frmLogin.Nickname.focus();
		return false;
	} else {
		var sErrMsg = checkRegNameValid();
		if(sErrMsg!="") {
			alert("Following error(s) encountered.\n"+sErrMsg);
			document.frmLogin.Nickname.focus();
			return false;
		}
	}
	if(trim(document.frmLogin.Password.value).length==0)	{
		alert("Password should not be blank");
		document.frmLogin.Password.focus();
		return false;
	}
	document.frmLogin.username.value = trim(document.frmLogin.Nickname.value);
	document.frmLogin.password.value = trim(document.frmLogin.Password.value);
	var timeToKeep = 3600000*24*365; // one minute
	var expires = new Date();
	expires.setTime(expires.getTime() + timeToKeep);
	set_cookie('gcheck','Y',expires,'paltalk.com');
	
}
function trim(inputString) {
	 var retValue = inputString;
	 var ch = retValue.substring(0, 1);
	 while (ch == " ") {
			retValue = retValue.substring(1, retValue.length);
			ch = retValue.substring(0, 1);
	 }
	 ch = retValue.substring(retValue.length-1, retValue.length);
	 while (ch == " ") {
			retValue = retValue.substring(0, retValue.length-1);
			ch = retValue.substring(retValue.length-1, retValue.length);
	 }
	 return retValue;
}

function checkRegNameValid(){
	var str = "";
	str = str+checkRegInvalidChars();
	str = str+checkRegInvalidOne();
	str = str+checkLoginNameValid();
	return str;
}

function checkRegInvalidChars() {
        var ci = 0, last_ci = 0, last = 0;
        var sep = 0, repeater = 0;
		var arr_userChars = new Array();
		
		for(var k=0;k<document.frmLogin.Nickname.value.length;k++) {
			arr_userChars[k]=document.frmLogin.Nickname.value.substring(k,k+1).toLowerCase();
		}

        if (arr_userChars.length < 2 || arr_userChars.length > 25){
			return "Nickname cannot be less than 2 chars or greater than 25 chars\n";
        }
        if (arr_userChars[0] == " " || arr_userChars[0] == "-" || arr_userChars[0] == "_"){
			return "Nickname should not start with space or - or _\n";
        }
		
        for (var i = 0; i < arr_userChars.length; i++)
        {
            if(document.frmLogin.Nickname.value.charCodeAt(i) >= 1 && document.frmLogin.Nickname.value.charCodeAt(i) <= 31){
				return "invalid characters in Nickname not allowed\n";
            }
            if (document.frmLogin.Nickname.value.charCodeAt(i) > 127) {
				return "weird chars in Nickname not allowed\n";
            }
            ci = arr_userChars[i];
            if (ci == last){
				repeater=repeater+1;
                if (repeater > 2) {return "one char repeats more than 3 times is not allowed\n";}else{}
            }else {
				repeater = 0;
                last = ci;
            }
            if (ci == " " && last_ci == " "){ return "two consecutive spaces in Nickname is not allowed\n";}
            if (ci == "_" && (last_ci == "_" || last_ci == "-")) { return "two consecutive underbars in Nickname is not allowed\n";} //two consecutive underbars
            if (ci == "-" && (last_ci == "_" || last_ci == "-")) { return "two consecutive dashes in Nickname is not allowed\n";} //two consecutive dashes
            if (ci == "_" || ci == "-" || ci == "  ") sep=sep+1;
            if (isLetter(ci) || !isNaN(ci) || ci == "_" || ci == "-" || ci == " " || ci=="(" || ci==")" )
                last_ci = ci;
            else
                return "invalid chars in Nickname\n";
        }

        if (sep > 4) { 
			return "a max of only 4 separator chars may be used in a Nickname\n";
		}
        return "";
        }
        
function isLetter(sChar) {
        //var inputStr = strText;
        //for (var intCounter = 0; intCounter < inputStr.length; intCounter++) {
            //var oneChar = inputStr.substring(intCounter, intCounter + 1)
            if (sChar.toUpperCase() < "A" || sChar.toUpperCase() > "Z") {
                return false;
            }
        //}
        return true;
 }
 
function checkRegInvalidOne() {
	var arr_userChars = new Array();
	for(var k=0;k<document.frmLogin.Nickname.value.length;k++) {
		arr_userChars[k]=document.frmLogin.Nickname.value.substring(k,k+1);
	}
    var ci = 0, prev_ci = 0, next_ci = 0;

    for (var i = 0; i < arr_userChars.length; i++)
    {
        ci = arr_userChars[i];
        if (ci == "1") 
        {
            if (i > 0) prev_ci = arr_userChars[i-1]; else prev_ci = "-";
            if (i+1 < arr_userChars.length) next_ci = arr_userChars[i+1]; else next_ci = "-";
            if (isLetter(prev_ci) && isLetter(next_ci))
                return "invalid char in Nickname is not allowed\n";
        }
		if (ci == "I")
        {
            if (i > 0) prev_ci = arr_userChars[i-1]; else prev_ci = "-";
            if (i+1 < arr_userChars.length) next_ci = arr_userChars[i+1]; else next_ci = "-";
            if (prev_ci >= "a" && prev_ci <= "z")
                return "invalid char in Nickname is not allowed\n";
        }
		if (ci == "I" || ci == "l")
        {
            if (i > 0) prev_ci = arr_userChars[i-1]; else prev_ci = "-";
            if (i+1 < arr_userChars.length) next_ci = arr_userChars[i+1]; else next_ci = "-";
            if (isSeparator(prev_ci) && isSeparator(next_ci) && i == arr_userChars.length-1)
                return "invalid char in Nickname is not allowed\n";
        }
    }
    return "";
}

function isSeparator(sChar) {
    if (sChar == " " || sChar == "-" || sChar == "_") return true;
    return false;
}

function checkLoginNameValid(){
	var str = "";
	var sNick = document.frmLogin.Nickname.value;
	if(sNick.indexOf("[")>=0 || sNick.indexOf("]")>=0) 
		str = "Invalid char in Nickname is not allowed";
	return str;
	
}

