﻿// JScript File

function LoadElementByTagAndID(tagName,id)
{
	var tags = document.getElementsByTagName(tagName);
	for(var i = 0 ; i < tags.length; i++)
	{
		var str = tags[i].id;
		if(str != str.replace(id,''))
		{
			return tags[i];
		}
	}	
}
function InitCompatibilityFix()
{
	if(navigator.userAgent.indexOf('MSIE') != -1)//IE
	{
		var selects = document.getElementsByTagName('select');
		for(var i = 0 ; i < selects.length; i++)
		{
			selects[i].className = 'DL_IE';
		}
	}
	if(navigator.userAgent.indexOf('Safari') != -1)
	{
		var inputs = document.getElementsByTagName('input');
		for(var i = 0 ; i < inputs.length; i++)
		{
			inputs[i].style.color = '#000000';
		}
		
		var textareas = document.getElementsByTagName('textarea');
		for(var i = 0 ; i < textareas.length; i++)
		{
			textareas[i].style.color = '#000000';
			textareas[i].style.backgroundColor = '#ffffff';
		}
	}
}

function LimitTextSize(obj,limit)
{
    if(obj.value.length > limit)
    {
        var txt = obj.value.substr(0,limit);
        obj.value = txt;
    }
}
function LimitToNum(obj, defaultValue)
{
    var toremove = new Array();
    
    var txt = obj.value;
    for(var i = 0; i < txt.length ; i++)
    {
        var code = txt.charCodeAt(i);
        if(code < 48 || code > 57)
        {
            toremove[toremove.length] = txt.charAt(i);
        }
    }
    
    var corrected = txt;
    for(var i = 0; i < toremove.length ; i++)
    {
        corrected = corrected.replace(toremove[i],'');
    }
    
    if(corrected == '' && defaultValue != null)
        corrected = defaultValue;
    
    obj.value = corrected;
    
}
function LimitToDecimal(obj, precision)
{
    var toremove = new Array();
    
    var txt = obj.value;
    var decimalCount = 0;
    var currentPrecision = 0;
    for(var i = 0; i < txt.length ; i++)
    {
        var code = txt.charCodeAt(i);
        
        if(code == 46)
        {
			decimalCount++;
        }
        if((code < 48 || code > 57) && code != 46 )
        {
            toremove[toremove.length] = txt.charAt(i);
        }
        
        if(decimalCount > 0)
			currentPrecision++;
    }
    
    var corrected = txt;
    for(var i = 0; i < toremove.length ; i++)
    {
        corrected = corrected.replace(toremove[i],'');
    }
	
	if(currentPrecision > 2 || decimalCount > 1)
		corrected = parseFloat(corrected).toFixed(precision);
	
	if(corrected == '')
		corrected = 0;
	
    obj.value = corrected;
    
}
function TextCompletion(obj,ctrltofocus,x)
{
    if(obj.value.length >= x)
    {
        document.getElementById(ctrltofocus).focus();
    }
}
function ValidPostal(obj,serie)
{
    var toremove = new Array();
    var txt = obj.value.toUpperCase();
    if(serie == 1)
    {
        for(var i = 0; i < txt.length ; i++)
        {
            var code = txt.charCodeAt(i); 
            
            if(i == 0 || i == 2)
            {
                isok = false;
                
                if(code > 64 && code < 91)
                {
                    isok = true;
                }
                
                if(!isok)toremove[toremove.length] = txt.charAt(i);
                
            }
            else
            {
                isok = false;
                
                if(code > 47 && code < 58)
                {
                    isok = true;
                }
                
                if(!isok)toremove[toremove.length] = txt.charAt(i);
            }
        }
    }
    if(serie == 2)
    {
        for(var i = 0; i < txt.length ; i++)
        {
            var code = txt.charCodeAt(i);
            if(i == 1)
            {
                isok = false;
                
                if(code > 64 && code < 91)
                {
                    isok = true;
                }
                if(!isok)toremove[toremove.length] = txt.charAt(i);
            }
            else
            {
                isok = false;
                if(code > 47 && code < 58)
                {
                    isok = true;
                }
                if(!isok)toremove[toremove.length] = txt.charAt(i);
            }
        }
    }
    
    var corrected = txt;
    for(var i = 0; i < toremove.length ; i++)
    {
        corrected = corrected.replace(toremove[i],'');
    }
    
    obj.value = corrected;
}
function ValidTextSize(sender,clientside_arguments)
{
    alert(sender);
    
    if(document.getElementByID(sender).value.length != size)
    {
        return clientside_arguments.IsValid = false;
    }
}
function ValidPhone(a,b,c)
{
	var total = a.value.length + b.value.length + c.value.length;
	if(total < 10 && total > 0)
		return false;
	else
		return true;
}
function ValidNAS(a,b,c)
{
	var total = a.value.length + b.value.length + c.value.length;
	if(total < 9 && total > 0)
		return false;
	else
		return true;
}
function ValidDate(day,month,year)
{
    if(day.value == 'NULL' || month.value == 'NULL' || year.value.length != 4)
    return false;
    
    if(day.value != 'NULL' && month.value != 'NULL' && year.value.length == 4)
        return true;
    else
        return false;
}
//Gestion des jours selon les mois
var Days = new Array();
Days[0] = 31;
Days[1] = 31;
Days[2] = 29;
Days[3] = 31;
Days[4] = 30;
Days[5] = 31;
Days[6] = 30;
Days[7] = 31;
Days[8] = 31;
Days[9] = 30;
Days[10] = 31;
Days[11] = 30;
Days[12] = 31;

function LoadDays(obj,days)
{
    var index = obj.selectedIndex;
    
    days = document.getElementById(days);
    
    for(var i = 32; i > -1; i--)
    {
        if(i > Days[index] && days.options[i] != null )
        days.options[i] = null;
    }
    
    for(var i = 1; i < Days[index]+1; i++)
    {
        if(days.options[i] == null)
        {
            days.options[i] = new Option(i.toString(),i.toString());
        }
    }
}
