﻿/*************************************************************************************************************
Name : Anil Veeraghattapu.

Subject : Validations For User Controls.

Organization : SiegSoft.

Designation : Software Developer.

Place : Hyderabad.

Date Of Creation : 29 - Aug - 2008
************************************************************************************************************/


//Function to trim a string
String.prototype.trim = function ()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
/************************************************************************************************************/

//Function to get Value
function getValue(s){return document.getElementById(s).value}

/************************************************************************************************************/

//Function to show Message
function showMessage(obj,message)
{
    document.getElementById(obj).innerHTML = message;
}

/************************************************************************************************************/
//Function to set Focus on user control
function setFocus(obj)
{
    document.getElementById(obj).focus();
}

/************************************************************************************************************/
//Function to clear Textbox
function clearTextBox(obj)
{
    document.getElementById(obj).value = "";
}

/************************************************************************************************************/
//Function to get Object
function getObject(id)
{
    return document.getElementById(id);
}
/************************************************************************************************************/
//Function to create XML HTTP Object
function createxmlhttpobject()
{  
    var xmlHttpobj= false;
      
    if(window.XMLHttpRequest)
    {
        xmlHttpobj=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        xmlHttpobj=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlHttpobj;
}
/*************************************************************************************************************/


//Create a Class for Validation
function Validate()
{
    this.isEmpty = validateIsEmpty;
    this.isValidName = validateName;
    this.isCharacters = validateCharacters;
    this.isMobile = validateMobile;
    this.isEmail = validateEmail;
    this.isLandLine = validateLandLine;
    this.isValidDate = validateDateTime;
    this.isCharOnly = validateAlphabets;
    this.isNoSplChars = validateSplCharacters;
     this.isMaxCharacters = validateMaxCharacters;
}

//Method to check the string is empty
function validateIsEmpty(param)
{
    if(param != "" && param != null) return true;
}


//Method to check the length of the string
function validateName(name,min,max)
{
    if(name.length < min || name.length > max) return false;
    else return true;
   
}

//Method to check string contains any specila symbols
function validateCharacters(name)
{
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
    var isError = true;
    for(var i = 0 ; i < name.length ; i++)
    {
        if (iChars.indexOf(name.charAt(i)) != -1) isError = false;
        
    }
    if(!isNaN(name.charAt(0))) isError = false;
    
    return isError;
}

//Method to check the mobile number
function validateMobile(mobile)
{
    var phonenumber = /\d{10}/;
    if(!phonenumber.test(mobile)) return 1;
    if(mobile.length != 10) return 2;
    if(mobile.charAt(0) != "9") return 3;
    return 0;
}

//Method to check email address
function validateEmail(email)
{
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    
    if(filter.test(email)) return true;
     else
        return false;
}

//Method to check Land Line numbers
//This method allows land line number of 11 digits and a separator between the city code and phone number

function validateLandLine(landline)
{
     var phoneFormat = /^(\+)?([-\._\(\) ]?[\d]{3,20}[-\._\(\) ]?){2,10}$/;
     if(!phoneFormat.test(landline) || landline.length != 12)
        return false;
     else return true;
}

//Method to validate Date Time (mm/dd/yyyy)

function validateDateTime(datevalue)
{
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
    if(!validformat.test(datevalue)) return 1;
    
    var monthfield = datevalue.split("/")[0];
	var dayfield = datevalue.split("/")[1];
	var yearfield = datevalue.split("/")[2];
    
    var dt = new Date(datevalue);
    if(dt.getDate()!= dayfield ) return 2;
    if(dt.getMonth()!= monthfield*1 - 1 ) return 3;
    if(dt.getFullYear()!= yearfield*1 ) return 4;
    
    return 0;

}

//Method to validate Names which contains characters only

function validateAlphabets(name)
{
     var r  =/^[A-Za-z]+$/;
     if(!r.test(name))
        return false
     else return true;
}


//Method to validate No Special Characters

function validateSplCharacters(name)
{
    var iChars = "!@#$%^&*+=[]\\\';,.{}|\":<>?";
    var isError = true;
    for(var i = 0 ; i < name.length ; i++)
    {
        if (iChars.indexOf(name.charAt(i)) != -1) isError = false;
        
    }
       
    return isError;
}

//Method to restrict the Max length in textarea

function validateMaxCharacters(textboxname,limt)
{
    //var element = document.getElementById(textboxname);
    if (textboxname.value.length > limt)
    textboxname.value = textboxname.value.substring(0,limt-1);
}
