﻿function ValidateDDl(p_source, p_args)
{
    if (window.CustomValidateDDl)
        window.CustomValidateDDl(p_source, p_args);
    else
    {                
        p_args.IsValid = true;
        if (p_args.Value == 0)
            p_args.IsValid = false;            
    }
}
        
function ValidateCity(p_source, p_args)
{
    p_args.IsValid = (p_args.Value != '' && p_args.Value);
}
        
function ValidateNumericTextBox(p_source, p_args)
{            
    var oControl = document.getElementById(p_source.controltovalidate);
    p_args.IsValid = false;
    if (oControl.value != '')
    {
        var iValue = parseInt(oControl.value.replace(',', ''));
        p_args.IsValid = !isNaN(iValue) && iValue > 0;
    }
}
        
function ValidatePay(p_source, p_args)
{
    p_args.IsValid = false;   
    var oControl = document.getElementById(p_source.controltovalidate);
    if (oControl.disabled)
        p_args.IsValid = true;
    else if (p_args.Value != '')
    {
        var iValue = parseInt(p_args.Value);
        p_args.IsValid = !isNaN(iValue) && iValue > 0;
    }
}

function ValidatePayValue(p_Control) 
{
    if (p_Control==null || p_Control.disabled)
        return true;
    else
     {
        var iValue = parseInt(p_Control.value);
        return (!isNaN(iValue) && iValue > 0);
    }
}
        
function ValidateNumericTextBoxValue(p_Control)
{
    if (p_Control.value != '')
    {
        var iValue = parseInt(p_Control.value.replace(',', ''));
        return !isNaN(iValue) && iValue > 0;
    }
    return false;
}

function PhoneValidationSettings(p_CountryCtlID, p_CityCtlID, p_PhoneCtlID, p_bRequired)
{
    this._countryCtlID = p_CountryCtlID;
    this._cityCtlID = p_CityCtlID;
    this._phoneCtlID = p_PhoneCtlID;
    this.IsRequired = p_bRequired;    
    this.GetKey = function() {return this._phoneCtlID;};
    this.CountryControl = function() {return document.getElementById(this._countryCtlID);};
    this.CityControl = function() {return document.getElementById(this._cityCtlID);};
    this.PhoneControl = function() {return document.getElementById(this._phoneCtlID);};    
    this.ValidationExpressions = ["[\\d]{1,3}", "[\\d]{1,5}", "([\\d]+(-[\\d]+)*[\\d]+){0,9}"];
}

var Phones = [];

function GetPhoneSettings(p_sKey)
{
    for(var i = 0; i < Phones.length; ++i)
        if (Phones[i] != null &&
            Phones[i].GetKey != null &&
            Phones[i].GetKey() == p_sKey)        
            return Phones[i];
    return null;            
}

function ValidatePhone(p_source, p_args)
{
    var sett = GetPhoneSettings(p_source.controltovalidate);    
    if (sett == null)
        throw ("Control " + p_source.controltovalidate + " is not registered as phone control.");
    p_args.IsValid = true;
    if (sett.IsRequired)    
        p_args.IsValid = sett.CountryControl().value != '' && sett.CityControl().value != '' && sett.PhoneControl().value != '';
    else 
    {
        if (sett.CountryControl().value == '' && sett.CityControl().value == '' && sett.PhoneControl().value == '')
            return;
        else
            p_args.IsValid = p_args.IsValid = sett.CountryControl().value != '' && sett.CityControl().value != '' && sett.PhoneControl().value != '';
    }
    if (!p_args.IsValid)
        return;
    var aCtls = [sett.CountryControl(), sett.CityControl(), sett.PhoneControl()];
    for (var i = 0; i < aCtls.length; ++i)
    {
        var rx = new RegExp(sett.ValidationExpressions[i]);
        var matches = rx.exec(aCtls[i].value);
        p_args.IsValid = (matches != null && aCtls[i].value == matches[0]);
        if (!p_args.IsValid)
            return;
    }
}

function ValidateEMail(p_source, p_args) {
  var value = document.getElementById(p_source.controltovalidate).value;
  p_args.IsValid = value != '';
  if (p_args.IsValid) {
    var rx = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    var matches = rx.exec(value);
    p_args.IsValid = (matches != null && value == matches[0]);
  }
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();