
/**
FUNTION SELECTALL CHECK BOXES
**/
function SelectAll(frm) {
 //alert(frm.selectall.checked);
   if(frm.selectall.checked == true) {
   
     for(i=0;i<frm.elements.length;i++) {
       if((frm.elements[i].type == "checkbox") && (frm.elements[i].name != "selectall")) {
         frm.elements[i].checked = true;
       } // if statement
     } // for loop
   }
   else if(frm.selectall.checked == false) {
    
      for(i=0;i<frm.elements.length;i++) {
         if((frm.elements[i].type == "checkbox") && (frm.elements[i].name != "selectall")) {
           frm.elements[i].checked = false;
         } // if statement
      } // for loop
   } // if - else - if condition

} // closing the function SelectAll()

/**
 FUNCTION SELECTVALIDATION(element,message)  **/
function SelectValidation(Element,Message)
{
    if(Element.value == "0")
    {
        alert("Please Select "+Message+"");
        Element.focus();
        return 0;
    }
}

function SelectValidationFree(Element,Message)
{
    if(Element.value.innerText == "Free")
    {
        alert("Member type should not be Free");
        Element.focus();
        return 0;
    }
}
/**
 FUNCTION EMAILVALIDATION(element)  **/
function EmailValidation(Element)
{
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=Element.value.match(emailPat);
    var inv="Invalid Email Address\nPlease Enter Valid EMail Address";
    
    if (null == matchArray) {
        Element.focus();
        alert(inv);
        return 0;
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    // See if "user" is valid
    if (null == user.match(userPat)) {
        // user is not valid
        Element.focus();
        alert(inv);
        return 0;
    }

    // Domain is symbolic name. Check if it's valid.
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
       }
    }

    if (checkTLD && domArr[domArr.length-1].length!=2 &&
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
        Element.focus();
        alert(inv);
        return 0;
    }

    if (2 > len) {
        Element.focus();
        alert(inv);
        return 0;
    }
    return 1;
}

/**
 FUNCTION GENVALIDATION(element,message1,size,spl)  **/
function GenValidation(Element,MessageLen0,MessageLen4,spl)			
{
    if(MessageLen0.length != 0)
    {
        if(isBlank(Element.value) || Element.value.length == 0)
        {
            alert("Please Enter the "+ MessageLen0);
            Element.focus();
            return 0;
        }
    }
    if(MessageLen4.length!=0) 
    {
	    if(Element.value.length > MessageLen4) 
	    {   
//            if(validateSize(Element,MessageLen0,MessageLen4,'') == 0)
//                return 0;
	        alert( MessageLen0 + " Should be less than " + MessageLen4 + " characters" );
	        Element.focus();
	        return 0;
	    }
    }
    if(spl.length != 0)
    {
        if(spl == "name")
        {
            if(validateName(Element,MessageLen0) == 0)
            return 0;
        }
        else if(spl == "phone")
        {
            if(validatePhone(Element,MessageLen0) == 0)
            return 0;
        }        
        else if(spl == "pwd")
        {
            if(CheckPass(Element) == 0)
            return 0;
        }
        else if(spl == "login")
        {
            if(validateLogin(Element,MessageLen0) == 0)
            return 0;
        }
        else if(spl == "mail")
        {
            if(EmailValidation(Element) == 0)
            return 0;
        }
        else if(spl == "date")
        {
            if(validateDate(Element,MessageLen0) == 0)
            return 0;
        }
        else if(spl == "num")
        {
            if(NumbersOnly(Element,MessageLen0) == 0)
            return 0;
        }
        else if(spl == "float")
        {
            if(Floating(Element,MessageLen0) == 0)
            return 0;
        }
        else if(spl == "money")
        {
            if(AmountOnly(Element,MessageLen0) == 0)
            return 0;
        }        
    }
} // closing the function GenValidation()

function validateSize(Element,Message,size,spl)
{
    if(Element.value.length > size) 
    {   
	    alert( Message + " Should be less than " + size + " characters" );
	    Element.focus();
	    return 0;
    }
    if(spl.length != 0)
    {
        if(spl == "num")
        {
            if(NumbersOnly(Element,Message) == 0)
            return 0;
        }
        else if(spl == "float")
        {
            if(Floating(Element,MessageLen0) == 0)
            return 0;
        }           
    }
}

function validateName(Val,txt)
{
    var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. ";
    for (var i=0;i<Val.value.length;i++)
    {
        temp=Val.value.substring(i,i+1);
        if (alp.indexOf(temp)==-1)
        {
            alert("No special characters in " + txt + " \nValid entries are [a-z][A-Z][0-9][space][.]");
            Val.focus();
            return 0;
        }
    } // closing the for loop
} // closing the function validateName()

function validateLogin(Val,txt)
{
    var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for (var i=0;i<Val.value.length;i++)
    {
        temp=Val.value.substring(i,i+1);
        if (alp.indexOf(temp)==-1)
        {
            alert("No special characters in " + txt + " \nValid entries are [a-z][A-Z][0-9]");
            Val.focus();
            return 0;
        }
    } // closing the for loop
} // closing the function validateLogin()

function validateDate(Val,txt)
{
    var checkStr = Val.value;
    if (checkStr.match(/\d{1,2}\/\d{1,2}\/\d{2,4}/))
    {
        var dayfield=Val.value.split("/")[0]
        var monthfield=Val.value.split("/")[1]
        var yearfield=Val.value.split("/")[2]
        var dayobj = new Date(yearfield, monthfield-1, dayfield)
        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
        {
            alert('Invalid Day/Month/Year range detected. Please Correct.')
            return 0;
        }
        else
            return 1;
    }
    else
    {
        alert("The Date Of Birth format should be: dd/mm/yyyy");
        //document.EntryForm.Dob.focus();
        return 0;
    }
} // closing the function validateDate()

function validatePhone(Element,txt)
{
    if(Element.value.length > 12) 
    {   
	    alert( txt + " Should be less than 12 digits" );
	    Element.focus();
	    return 0;
    }
    else
    {
        var alp = "0123456789";
        for (var i=0;i<Element.value.length;i++)
        {
            temp=Element.value.substring(i,i+1);
            if (alp.indexOf(temp)==-1)
            {
                alert("No special characters in " + txt + "\nValid entries are [0-9]");
                Element.focus();
                return 0;
            }
        } // closing the for loop
    }
} // closing the function validatePhone()

/**
 FUNCTION NUMBERSONLY(element)  **/
function NumbersOnly(Val,txt)
{
    var alp = "0123456789";
    for (var i=0;i<Val.value.length;i++)
    {
        temp=Val.value.substring(i,i+1);
        if (alp.indexOf(temp)==-1)
        {
            alert("Valid entries are [0-9] in "+txt+"");
            Val.focus();
            return 0;
        }
    } // closing the for loop   
} // closing the function NumbersOnly()

function Floating(Val,txt)
{
    if(isNaN(Val.value))
    {
        alert("Please enter only Numbers in "+ txt);
        Val.focus();
        return 0;
    }    
} // closing the function Floating()

/**
 FUNCTION AMOUNTONLY(element)  **/
function AmountOnly(Val,txt)
{
    var alp = "0123456789.,";
    for (var i=0;i<Val.value.length;i++)
    {
        temp=Val.value.substring(i,i+1);
        if (alp.indexOf(temp)==-1)
        {
            alert("Valid entries are [0-9][.][,] in "+txt+"");
            Val.focus();
            return 0;
        }
    } // closing the for loop   
} // closing the function AmountOnly()

//Function Password Verification for Alphanumeric Values
function CheckPass(password)
{
    if(password.value.length < 6) 
    {   
        alert( "Password Should be more than 5 characters" );
        password.focus();
        return 0;
    }
    else
    {
        var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.";
        for (var i=0;i<password.value.length;i++)
        {
            temp=password.value.substring(i,i+1);
            if (alp.indexOf(temp)==-1)
            {
                alert("No special characters in Password \nValid entries are [a-z][A-Z][0-9][.]");
                password.focus();
                return 0;
            }
        }
	}
}

/**
 FUNCTION PASSVALIDATION(element1,element2)  **/
function PassValidation(Element1,Element2)
{
    if(Element1.value != Element2.value)
    {
        alert("Confirm Password doesn't match");
        Element2.focus();
        return 0;
    }
} // closing the function PassValidation()

/**
 FUNCTION FOR CHECKING THE FIELD CONTAINS BLANK VALUES ISBLANK(Element.value)  **/
//To check if trim(value) is blank
function isBlank(txt)
{
    if( txt.length == getCountOf(' ', txt) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

//This can be used for any character validation.
//For example in a valid date the count of - or / should not be more than 2
//Likewise in a valid numer there should be only one .
function getCountOf(vChr, txt)
{
    var i = 0;
    var iCount = 0;
    for( i=0; i < txt.length; i++ )
    {
        if( txt.charAt(i) == vChr )
        {
            iCount++;
        }
    }
    return iCount;
}

function sizeCompage(Element1,Element2,Msg1,Msg2)
{
    var e1=parseInt(Element1.value);
    var e2=parseInt(Element2.value);
    if(e1 > e2) 
    {
	    alert( Msg1 + " Should be less than " + Msg2 );
	    Element1.focus();
	    return 0;
    }
}

//*****  STRING TRIM FUNCTION**********//////
function trim(str)
{
var i=0,p = str.length-1;
while(str.charAt(i)==' ')i++;
while(str.charAt(p)==' ') p--;
if(i>p) return '';
return str.substring(i,p+1);
}
//*****  END OF STRING TRIM FUNCTION******************//////

/**
 FUNCTION onkeyup="javascript:changeToUpper(this.form,this)  **/
function changeToUpper(objFrm,objTxt)
{	
	var objFrmName=objFrm.name;
	var objTxtName=objTxt.name;
	document.forms(objFrmName).elements(objTxtName).value=document.forms(objFrmName).elements(objTxtName).value.toUpperCase(); 
}