﻿// JScript File

//' -----------------------------------------------------------------------------------------------
    //' File Name   : JScriptValidations.js
    //' Written By  : Ramesh Dodla
    //' DOC         : 10/Dec/2008 
    //' Purpose     : Validating the fields
    //' -----------------------------------------------------------------------------------------------
    //' 
    //'
    //' Functions/methods Available: isEmpty,isNumeric,isAlphabet,isAlphanumeric,lengthRestriction,emailValidator
    //'                               isSpace,  keypressIsNotNumeric,keypressIsNumeric,allowUnderscoreAndDot,keypressIsAlphaNumeric
    //' -----------------------------------------------------------------------------------------------


//Print Function
function CallPrint(strid)
{    
    var prtContent = document.getElementById(strid);       
    var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=0');
    WinPrint.document.write( prtContent.innerHTML );
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();
    //prtContent.innerHTML=strOldOne;      
}



// this function is used to restrict space

function isSpace(e)
{
            
            if(([e.keyCode||e.which]==32)) 
                {
                    e.preventDefault? e.preventDefault() : e.returnValue = false;
                }
}




//this function will allow < > * # @)
function allowLessAndGreaterthanEtc(e)
{
    if(([e.keyCode||e.which]==60) || ([e.keyCode||e.which]==62) || ([e.keyCode||e.which]==42) || ([e.keyCode||e.which]==35) || ([e.keyCode||e.which]==64) || ([e.keyCode||e.which]==41)) 
                {
                    e.preventDefault? e.preventDefault() : e.returnValue = true;
                }

}


//this function will allow . and _
function allowUnderscoreAndDot(e)
{
    if(([e.keyCode||e.which]==46) || ([e.keyCode||e.which]==95)) 
                {
                    e.preventDefault? e.preventDefault() : e.returnValue = true;
                }

}

//this function will allow . and -
function allowMinusAndDot(e)
{
    if(([e.keyCode||e.which]==46) || ([e.keyCode||e.which]==45)) 
                {
                    e.preventDefault? e.preventDefault() : e.returnValue = true;
                }

}

//this function is used to allow only Alpha-Numeric values

function keypressIsAlphaNumeric(e)
    {
   
        if ([e.keyCode||e.which]==8) //this is to allow backspace
            {
                return true;
            }
            //event.keyCode=(!((c>=65 && c<=90) ||(c>=97 && c<=122) || (c==32)|| (c>=48 && c<=57) ))?0:event.keyCode;
            //event.keyCode=(!(([e.keyCode||e.which]>=65 && [e.keyCode||e.which]<=90) ||([e.keyCode||e.which]>=97 && [e.keyCode||e.which]<=122) || ([e.keyCode||e.which]==32)|| ([e.keyCode||e.which]>=48 && [e.keyCode||e.which]<=57) ))?0:event.keyCode;
        
        if (!(([e.keyCode||e.which]>=65 && [e.keyCode||e.which]<=90) ||([e.keyCode||e.which]>=97 && [e.keyCode||e.which]<=122) || ([e.keyCode||e.which]==32)|| ([e.keyCode||e.which]>=48 && [e.keyCode||e.which]<=57)))
            {
                e.preventDefault? e.preventDefault() : e.returnValue = false;
            }
        

}

//this function will allow only charecters and space
function KeyPressIsAlphaChars(e)
{    
if ([e.keyCode||e.which]==8) //this is to allow backspace
            {
                return true;
            }
            
        
        if (!(([e.keyCode||e.which]>=65 && [e.keyCode||e.which]<=90) ||([e.keyCode||e.which]>=97 && [e.keyCode||e.which]<=122) || ([e.keyCode||e.which]==32) ))
            {
                e.preventDefault? e.preventDefault() : e.returnValue = false;
            }

}






// this function is used to allow non-numeric values
function keypressIsNotNumeric(e)
    {
        if ([e.keyCode||e.which]==8) //this is to allow backspace
            {
                return true;
            }
        
        if ([e.keyCode||e.which] >= 48 && [e.keyCode||e.which] <= 57)
            {
                e.preventDefault? e.preventDefault() : e.returnValue = false;
            }
}

// this function is used to allow numeric values only

function keypressIsNumeric(e)
    {
        if ([e.keyCode||e.which]==8) //this is to allow backspace
            {
                return true;
            }
        if ([e.keyCode||e.which] < 48 || [e.keyCode||e.which] > 57)
            {
                e.preventDefault? e.preventDefault() : e.returnValue = false;
            }
}






// If the length of the element's string is 0 then display helper message
//onclick="isEmpty(document.getElementById('req1'), 'Please Enter a Value')"
function isEmpty(obj,helperMsg)
{
    if(obj.value.length==0)
        {
            alert(helperMsg);
            obj.focus();
            return true;
        }
            return false;
}


// If the element's string matches the regular expression it is all numbers
//onclick="isNumeric(document.getElementById('numbers'), 'Numbers Only Please')"
function isNumeric(elem, helperMsg)
{
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}



// If the element's string matches the regular expression it is all letters
//onclick="isAlphabet(document.getElementById('letters'), 'Letters Only Please')"

function isAlphabet(elem, helperMsg)
{
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function isValidNumAlpha()
    {	var c= event.keyCode;	
    event.keyCode=(!((c>=65 && c<=90) ||(c>=97 && c<=122) || (c==32)|| (c>=48 && c<=57) ))?0:event.keyCode;
    }
    


// If the element's string matches the regular expression it is numbers and letters
function isAlphanumeric(elem, helperMsg)
{
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

//lengthRestriction
//onclick="lengthRestriction(document.getElementById('restrict'), 6, 8)"
function lengthRestriction(elem, min, max)
{
	var uInput = elem.value;
	
	if(uInput.length >0)
	{
	
	if(uInput.length >= min && uInput.length <= max)
	{
		return true;
	}
	else
	{
		alert("Please enter" + max+ " digits");
		elem.focus();
		return false;
	}
	}
}

//Email Validation
//onclick="emailValidator1(document.getElementById('emailer'), 'Not a Valid Email')"
function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


//' -----------------------------------------------------------------------------------------------
    //' File Name   : JScriptValidations.js
    //' Written By  : Prem Boyapati
    //' DOC         : 01/Jan/2009 
    //' Purpose     : Validating the fields
    //' -----------------------------------------------------------------------------------------------
    //' 
    //'
    //' Functions/methods Available: SSNCheck,EmailCheck,OnKeyPress
    //'
    //' -----------------------------------------------------------------------------------------------


// If the length of the element's string is 0 then display helper message
//onclick="SSNCheck('7565453trtwrt43');
function SSNCheck(ssn) 
{
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	if (matchArr == null || numDashes == 1) 
	{
		alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
		msg = "does not appear to be valid";
	}
	else if (parseInt(matchArr[1],10)==0) 
	{
		alert("Invalid SSN: SSN's can't start with 000.");
		msg = "does not appear to be valid";
	}
	else 
	{
		msg = "appears to be valid";
		alert(ssn + "\r\n\r\n" + msg + " Social Security Number.");
	}
}

/*
 * Email validation script.
 */
function EmailCheck(str) 
{

	var at = "@"
	var dot = "."
	var locAt = str.indexOf(at)
	var lenStr = str.length
	var locDot = str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lenStr)
	{
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lenStr)
	{
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at,(locAt+1))!=-1)
	{
		alert("Invalid E-mail ID")
		return false
	}
	if (str.substring(locAt-1,locAt)==dot || str.substring(locAt+1,locAt+2)==dot)
	{
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot,(locAt+2))==-1)
	{
		alert("Invalid E-mail ID")
		return false
	}
	
	if (str.indexOf(" ")!=-1)
	{
		alert("Invalid E-mail ID")
		return false
	}

	return true					
}

function OnKeyPress(field, event)
{
	if (event.keyCode == 13) 
	{
		for (i = 0; i < field.form.elements.length; i++)
			if (field.form.elements[i].tabIndex == field.tabIndex+1) 
			{
				field.form.elements[i].focus();
				if (field.form.elements[i].type == "text")
					field.form.elements[i].select();
				break;
			}
		return false;
	}
	return true;
}
