﻿//Function for Validating Email
function validateEmail(email) {
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) return false;
    if (splitted[1] != null) {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) return false;
    }
    if (splitted[2] != null) {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null) {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) return false;
        } // if
        return true;
    }
    return false;
}

//Function for Testing Email
function TestEmail(objValue, strError) {
    var ret = true;
    if (objValue.value.length > 0 && !validateEmail(objValue.value)) {
        alert(strError);
        objValue.value = "";
        objValue.focus();
        objValue.style.backgroundColor = "#FF0000";
        ret = false;
    } //if 
    return ret;
}

//Function for Testing InputType
function TestInputType(objValue, strRegExp, strError) {
    var ret = true;

    var charpos = objValue.value.search(strRegExp);
    if (objValue.value.length > 0 && charpos >= 0) {
        alert(strError);
        objValue.value = "";
        objValue.focus();
        objValue.style.backgroundColor = "#FF0000";
        ret = false;
    }
    else {
        objValue.style.backgroundColor = "#ffffa0";
    }
    return ret;
}

function validateEmail(strValue) {
    /************************************************
    DESCRIPTION: Validates that a string contains a 
    valid email pattern. 
  
 PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
   
    REMARKS: Accounts for email with country appended
    does not validate that email contains valid URL
    type (.com, .gov, etc.) or valid country suffix.
    *************************************************/
    var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;

    //check for valid email
    return objRegExp.test(strValue);
}

function validateUSPhone(strValue) {
    /************************************************
    DESCRIPTION: Validates that a string contains valid
    US phone pattern. 
    Ex. (999) 999-9999 or (999)999-9999
  
PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
    *************************************************/
    var objRegExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

    //check for valid us phone with or without space between 
    //area code
    return objRegExp.test(strValue);
}

function validateNumeric(strValue) {
    /******************************************************************************
    DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
    ******************************************************************************/
    var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

    //check for numeric characters 
    return objRegExp.test(strValue);
}

function validateInteger(strValue, strError) {
    /************************************************
    DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
    PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
    ******************************************************************************/
    var objRegExp = /(^-?\d\d*$)/;

    //check for integer characters
    if (objRegExp.test(strValue.value)) {
        return true;
    }
    alert(strError);
    strValue.value = "";
    strValue.focus();
    strValue.style.backgroundColor = "#FF0000";
    return false;
}

function validateNotEmpty(strValue, strError) {
    /************************************************
    DESCRIPTION: Validates that a string is not all
    blank (whitespace) characters.
    
    PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
    *************************************************/
    var strTemp = strValue.value;
    strTemp = trimAll(strTemp);
    if (strTemp.length > 0) {
        return true;
    }
    alert(strError);
    strValue.value = "";
    strValue.focus();
    strValue.style.backgroundColor = "#FF0000";
    return false;
}

function validateNotSelected(strValue, strError) {
    /************************************************
    DESCRIPTION: Validates that a string is not all
    blank (whitespace) characters.
    
    PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
    *************************************************/
    if (strValue.selectedIndex == 0) {
        alert(strError);
        strValue.focus();
        //strValue.focus();
        strValue.style.backgroundColor = "#FF0000";
        return false;
    }
}

function validateUSZip(strValue) {
    /************************************************
    DESCRIPTION: Validates that a string a United
    States zip code in 5 digit format or zip+4
    format. 99999 or 99999-9999
    
    PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.

*************************************************/
    var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

    //check for valid US Zipcode
    return objRegExp.test(strValue);
}

function validateDate(strValue, strError) {
    /************************************************
    DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
    
    PARAMETERS:
    strValue - String to be tested for validity
   
    RETURNS:
    True if valid, otherwise false.
   
    REMARKS:
    Avoids some of the limitations of the Date.parse()
    method such as the date separator character.
    *************************************************/
    var objRegExp = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/
    //var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

    //check to see if in correct format
    if (!objRegExp.test(strValue.value)) {
        alert(strError);
        strValue.value = "";
        strValue.focus();
        strValue.style.backgroundColor = "#FF0000";
        return false; //doesn't match pattern, bad date
    }
    else {
        var strSeparator = strValue.value.substring(2, 3) //find date separator
        var arrayDate = strValue.value.split(strSeparator); //split date into month, day, year
        //create a lookup for months not equal to Feb.
        var arrayLookup = { '01': 31, '03': 31, '04': 30, '05': 31, '06': 30, '07': 31,
            '08': 31, '09': 30, '10': 31, '11': 30, '12': 31
        }
        var intDay = parseInt(arrayDate[0], 10);

        //check if month value and day value agree
        if (arrayLookup[arrayDate[1]] != null) {
            if (intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
                return true; //found in lookup table, good date
        }

        //check for February (bugfix 20050322)
        //bugfix  for parseInt kevin
        //bugfix  biss year  O.Jp Voutat
        var intMonth = parseInt(arrayDate[1], 10);
        if (intMonth == 2) {
            var intYear = parseInt(arrayDate[2]);
            if (intDay > 0 && intDay < 29) {
                return true;
            }
            else if (intDay == 29) {
                if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
             (intYear % 400 == 0)) {
                    // year div by 4 and ((not div by 100) or div by 400) ->ok
                    return true;
                }
            }
        }
    }
    alert(strError);
    strValue.value = "";
    strValue.focus();
    strValue.style.backgroundColor = "#FF0000";
    return false; //any other values, bad date
}

function validateValue(strValue, strMatchPattern) {
    /************************************************
    DESCRIPTION: Validates that a string a matches
    a valid regular expression value.
    
    PARAMETERS:
    strValue - String to be tested for validity
    strMatchPattern - String containing a valid
    regular expression match pattern.
      
    RETURNS:
    True if valid, otherwise false.
    *************************************************/
    var objRegExp = new RegExp(strMatchPattern);

    //check if string matches pattern
    return objRegExp.test(strValue);
}


function rightTrim(strValue) {
    /************************************************
    DESCRIPTION: Trims trailing whitespace chars.
    
    PARAMETERS:
    strValue - String to be trimmed.  
      
    RETURNS:
    Source string with right whitespaces removed.
    *************************************************/
    var objRegExp = /^([\w\W]*)(\b\s*)$/;

    if (objRegExp.test(strValue)) {
        //remove trailing a whitespace characters
        strValue = strValue.replace(objRegExp, '$1');
    }
    return strValue;
}

function leftTrim(strValue) {
    /************************************************
    DESCRIPTION: Trims leading whitespace chars.
    
    PARAMETERS:
    strValue - String to be trimmed
   
    RETURNS:
    Source string with left whitespaces removed.
    *************************************************/
    var objRegExp = /^(\s*)(\b[\w\W]*)$/;

    if (objRegExp.test(strValue)) {
        //remove leading a whitespace characters
        strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}

function trimAll(strValue) {
    /************************************************
    DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
    be removed;

RETURNS: Source string with whitespaces removed.
    *************************************************/
    var objRegExp = /^(\s*)$/;

    //check for all spaces
    if (objRegExp.test(strValue)) {
        strValue = strValue.replace(objRegExp, '');
        if (strValue.length == 0)
            return strValue;
    }

    //check for leading & trailing spaces
    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (objRegExp.test(strValue)) {
        //remove leading and trailing whitespace characters
        strValue = strValue.replace(objRegExp, '$2');
    }
    return strValue;
}

function removeCurrency(strValue) {
    /************************************************
    DESCRIPTION: Removes currency formatting from 
    source string.
  
PARAMETERS: 
    strValue - Source string from which currency formatting
    will be removed;

RETURNS: Source string with commas removed.
    *************************************************/
    var objRegExp = /\(/;
    var strMinus = '';

    //check if negative
    if (objRegExp.test(strValue)) {
        strMinus = '-';
    }

    objRegExp = /\)|\(|[,]/g;
    strValue = strValue.replace(objRegExp, '');
    if (strValue.indexOf('$') >= 0) {
        strValue = strValue.substring(1, strValue.length);
    }
    return strMinus + strValue;
}

function addCurrency(strValue) {
    /************************************************
    DESCRIPTION: Formats a number as currency.

PARAMETERS: 
    strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid 
    numeric value in the rounded to 2 decimal 
    places.  If not, returns original value.
    *************************************************/
    var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

    if (objRegExp.test(strValue)) {
        objRegExp.compile('^-');
        strValue = addCommas(strValue);
        if (objRegExp.test(strValue)) {
            strValue = '(' + strValue.replace(objRegExp, '') + ')';
        }
        return '$' + strValue;
    }
    else
        return strValue;
}

function removeCommas(strValue) {
    /************************************************
    DESCRIPTION: Removes commas from source string.

PARAMETERS: 
    strValue - Source string from which commas will 
    be removed;

RETURNS: Source string with commas removed.
    *************************************************/
    var objRegExp = /,/g; //search for commas globally

    //replace all matches with empty strings
    return strValue.replace(objRegExp, '');
}

function addCommas(strValue) {
    /************************************************
    DESCRIPTION: Inserts commas into numeric string.

PARAMETERS: 
    strValue - source string containing commas.
  
RETURNS: String modified with comma grouping if
    source was all numeric, otherwise source is 
    returned.
  
REMARKS: Used with integers or numbers with
    2 or less decimal places.
    *************************************************/
    var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');

    //check for match to search criteria
    while (objRegExp.test(strValue)) {
        //replace original string with first group match, 
        //a comma, then second group match
        strValue = strValue.replace(objRegExp, '$1,$2');
    }
    return strValue;
}

function removeCharacters(strValue, strMatchPattern) {
    /************************************************
    DESCRIPTION: Removes characters from a source string
    based upon matches of the supplied pattern.

PARAMETERS: 
    strValue - source string containing number.
  
RETURNS: String modified with characters
    matching search pattern removed
  
USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd', 
    '\s*')
    *************************************************/
    var objRegExp = new RegExp(strMatchPattern, 'gi');

    //replace passed pattern matches with blanks
    return strValue.replace(objRegExp, '');
}



// JScript File
function ValidateForm() {

    var incidentAddress = document.getElementById(GetClientId("txt_incident_address"));
    var incidentState = document.getElementById(GetClientId("ddl_incident_State"));
    var incidentDistrict = document.getElementById(GetClientId("ddl_incident_district"));
    var incidentCity = document.getElementById(GetClientId("txt_incident_city"));
    var policeStation = document.getElementById(GetClientId("txt_policestation"));
    var incidentPin = document.getElementById(GetClientId("txt_incident_pin"));
    var incidentDate = document.getElementById(GetClientId("cmb_incidentdate"));
    var incidentMonth = document.getElementById(GetClientId("cmb_incidentmonth"));
    var incidentYear = document.getElementById(GetClientId("cmb_incidentyear"));
    var FIRLodged = document.getElementById(GetClientId("FIRLodged"));
    var psCode = document.getElementById(GetClientId("txt_ps_code"));
    var firNo = document.getElementById(GetClientId("txt_fir_no"));
    var firDate = document.getElementById(GetClientId("txt_fir_date"));
    var natureofComplaint = document.getElementById(GetClientId("ddl_natureofComplaint"));
    var gist = document.getElementById(GetClientId("txt_gist"));
    var complainantName = document.getElementById(GetClientId("txt_complainant_name"));
    var complainantSex = document.getElementById(GetClientId("ddl_complainant_sex"));
    var complainantAddress = document.getElementById(GetClientId("txt_complainant_address"));
    var complainantState = document.getElementById(GetClientId("ddl_complainant_state"));
    var complainantDistrict = document.getElementById(GetClientId("ddl_complainant_district"));
    var complainantCity = document.getElementById(GetClientId("txt_complainant_city"));
    var complainantPin = document.getElementById(GetClientId("txt_complainant_pin"));
    //var complainantEmail = document.getElementById(GetClientId("txt_complainant_email"));
    var complainantRelationship = document.getElementById(GetClientId("txt_complainant_relationship"));
    var victimName = document.getElementById(GetClientId("txt_victim_name"));
    var victimType = document.getElementById(GetClientId("ddl_victim_type"));
    var victimAddress = document.getElementById(GetClientId("txt_victim_address"));
    var victimState = document.getElementById(GetClientId("ddl_victim_state"));
    var victimDistrict = document.getElementById(GetClientId("ddl_victim_district"));
    var victimCity = document.getElementById(GetClientId("txt_victim_city"));
    var victimPin = document.getElementById(GetClientId("txt_victim_pin"));
    var victimSex = document.getElementById(GetClientId("ddl_victim_sex"));
    var victimAge = document.getElementById(GetClientId("ddl_victim_age"));
    var ComplaintPending = document.getElementById(GetClientId("ddl_Complaint_Pending"));
    var chkAgree = document.getElementById(GetClientId("chkAgree"));

    if (!validateNotEmpty(incidentAddress, "Incident Address is required.")) {
        return false;
    }

    if (incidentState.selectedIndex == 0) {
        alert('Incident State is required.');
        incidentState.focus();
        incidentState.style.backgroundColor = "#FF0000";
        return false;
    }
    
    if (incidentDistrict.selectedIndex == 0) {
        alert('Incident District is required.');
        incidentDistrict.focus();
        incidentDistrict.style.backgroundColor = "#FF0000";
        return false;
    }

    if (!validateNotEmpty(incidentCity, "Incident City is required.")) {
        return false;
    }
    else {
        if (TestInputType(incidentCity, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Incident City.") == false) {
            return false;
        }
    }

    if (!validateNotEmpty(policeStation, "Nearest Police Station is required.")) {
        return false;
    }
    else {
        if (TestInputType(policeStation, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Police Station.") == false) {
            return false;
        }
    }    
    if (!validateNotEmpty(incidentPin, "Pin is required.")) {
        return false;
    }
    else {
        if (!validateInteger(incidentPin, "Please enter valid Pin.")) {
            return false;
        }
        else {                
                if (incidentPin.value.length != 6) {
                    alert('Please enter your 6 digit Pin.');
                    incidentPin.focus();
                    incidentPin.style.backgroundColor = "#FF0000";
                    return false;
                }
            }
    }

    if (incidentDate.selectedIndex == 0) {
        alert('Incident Date is required.');
        incidentDate.focus();
        incidentDate.style.backgroundColor = "#FF0000";
        return false;
    }

    if (incidentMonth.selectedIndex == 0) {
        alert('Incident Month is required.');
        incidentMonth.focus();
        incidentMonth.style.backgroundColor = "#FF0000";
        return false;
    }

    if (incidentYear.selectedIndex == 0) {
        alert('Incident Year is required.');
        incidentYear.focus();
        incidentYear.style.backgroundColor = "#FF0000";
        return false;
    }

    if (FIRLodged.selectedIndex == 0) {
        alert('Whether FIR Lodged is required.');
        FIRLodged.focus();
        FIRLodged.style.backgroundColor = "#FF0000";
        return false;
    }
    else {
        if (FIRLodged.selectedIndex == 1) {
            if (!validateNotEmpty(psCode, "Police Station Code is required.")) {
                return false;
            }

            if (!validateNotEmpty(firNo, "GD / FIR / DD No. is required.")) {
                return false;
            }

            if (!validateNotEmpty(firDate, "FIR / DD Date is required.")) {
                return false;
            }
            else {
                if (!validateDate(firDate, "Please enter valid FIR / DD Date.")) {
                    return false;
                }
            }
        }
    }

    if (!validateNotEmpty(victimName, "Victim Name is required.")) {
        return false;
    }
    else {
        if (TestInputType(victimName, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Victim.") == false) {
            return false;
        }
    }

    if (victimType.selectedIndex == 0) {
        alert('Type of Victim is required.');
        victimType.focus();
        victimType.style.backgroundColor = "#FF0000";
        return false;
    }

    if (!validateNotEmpty(victimAddress, "Victim Address is required.")) {
        return false;
    }

    if (victimState.selectedIndex == 0) {
        alert('Victim State is required.');
        victimState.focus();
        victimState.style.backgroundColor = "#FF0000";
        return false;
    }

    if (victimDistrict.selectedIndex == 0) {
        alert('Victim District is required.');
        victimDistrict.focus();
        victimDistrict.style.backgroundColor = "#FF0000";
        return false;
    }

    if (!validateNotEmpty(victimCity, "Victim City is required.")) {
        return false;
    }
    else {
        if (TestInputType(victimCity, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Victim City.") == false) {
            return false;
        }
    }

    if (!validateNotEmpty(victimPin, "Pin is required.")) {
        return false;
    }
    else {
        if (!validateInteger(victimPin, "Please enter valid Pin.")) {
            return false;
        }
        else {
            if (victimPin.value.length != 6) {
                alert('Please enter your 6 digit Pin.');
                victimPin.focus();
                victimPin.style.backgroundColor = "#FF0000";
                return false;
            }
        }
    }

    if (victimSex.selectedIndex == 0) {
        alert('Gender of Victim is required.');
        victimSex.focus();
        victimSex.style.backgroundColor = "#FF0000";
        return false;
    }

    if (victimAge.selectedIndex == 0) {
        alert('Age of Victim is required.');
        victimAge.focus();
        victimAge.style.backgroundColor = "#FF0000";
        return false;
    }

    if (natureofComplaint.selectedIndex == 0) {
        alert('Nature of Complaint is required.');
        natureofComplaint.focus();
        natureofComplaint.style.backgroundColor = "#FF0000";
        return false;
    }

    if (!validateNotEmpty(gist, "Gist of Complaint is required.")) {
        return false;
    }

    if (!validateNotEmpty(complainantName, "Complainant Name is required.")) {
        return false;
    }
    else {
        if (TestInputType(complainantName, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Complainant.") == false) {
            return false;
        }
    }

//    if (complainantSex.selectedIndex == 0) {
//        alert('Gender of Complainant is required.');
//        complainantSex.focus();
//        complainantSex.style.backgroundColor = "#FF0000";
//        return false;
//    }

    if (!validateNotEmpty(complainantAddress, "Complainant Address is required.")) {
        return false;
    }

    if (complainantState.selectedIndex == 0) {
        alert('Complainant State is required.');
        complainantState.focus();
        complainantState.style.backgroundColor = "#FF0000";
        return false;
    }

    if (complainantDistrict.selectedIndex == 0) {
        alert('Complainant District is required.');
        complainantDistrict.focus();
        complainantDistrict.style.backgroundColor = "#FF0000";
        return false;
    }

    if (!validateNotEmpty(complainantCity, "Complainant City is required.")) {
        return false;
    }
    else {
        if (TestInputType(complainantCity, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Complainant City.") == false) {
            return false;
        }
    }

    if (!validateNotEmpty(complainantPin, "Pin is required.")) {
        return false;
    }
    else {
        if (!validateInteger(complainantPin, "Please enter valid Pin.")) {
            return false;
        }
        else {
            if (complainantPin.value.length != 6) {
                alert('Please enter your 6 digit Pin.');
                complainantPin.focus();
                complainantPin.style.backgroundColor = "#FF0000";
                return false;
            }
        }
    }
    
//    if (!validateNotEmpty(complainantEmail, "Complainant Email is required.")) {
//        return false;
//    }
//    else {
//        if (TestInputType(complainantEmail, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Complainant City.") == false) {
//            return false;
//        }
//    }

    if (!validateNotEmpty(complainantRelationship, "Complainant relationship with Victim is required.")) {
        return false;
    }
    else {
        if (TestInputType(complainantRelationship, "[^a-zA-Z0-9#&() ,.\/-]", "Please enter valid Name for Complainant relationship with Victim.") == false) {
            return false;
        }
    }

    
         
    if (ComplaintPending.selectedIndex == 0) {
        alert('Whether complaint has been sent to any other Commission or is pending before a Court is required.');
        ComplaintPending.focus();
        ComplaintPending.style.backgroundColor = "#FF0000";
        return false;
    }
    
    if(!chkAgree.checked)
    {
        alert("Please accept the Terms and Conditions");
        chkAgree.focus();
        chkAgree.style.backgroundColor = "#FF0000"; 
        return false; 
    } 
}

