//================================================
// Librairie des validations et filtres Javascript
// Modifié le 2006-08-09
//================================================

//_____________________________________________
//Préserve les touches importante dans mozilla
//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//Flèches
//Home
//End
//Shift
//Space
//Enter
//Backspace
function pwPreserveKey(key)
{
    if ( key == '35' || key == '45' || key == '13' || key == '37' ||
         key == '39' || key == '9' || key == '189' || key == '16' || key == '8' ||
         key == '46'|| key == '36' || key == '35' || key == '17')
    {
        return true;
    }

    return false;
}

function pwHexColor(event, el)
{
    var key = event.keyCode;
    if( key == '0' ) key = event.which;

    if( !( el.value.indexOf('#') >= 0 && key == '35' ) )
    {
        if(((key >= '48' && key <= '57') || (key >= '65' && key <= '70') ||
           (key >= '97' && key <= '102') || key == '13' || pwPreserveKey(key))
           &&  key <= '128' )
        {
            return true;
        }
    }
    return false;
}

function pwOnKeyPressPostalCode(event,fkcountries)
{
    key = event.keyCode;
    if( key == '0' ) key = event.which;

    if( key )
    {
        switch(fkcountries)
        {
            case '61' : //France
            case '183' : //États-Unies
                if( (((key >= '48' && key <= '57') || key == '45' || key == '13') &&  key < '128') || pwPreserveKey(key) )
                {
                    return true;
                }
                break;
            case '32' : //Canada
            default:
                if(((key >= '48' && key <= '57') || (key >= '65' && key <= '90') ||
                   (key >= '97' && key <= '122') || key == '32' || key == '13' || pwPreserveKey(key))
                   && ( key != '35' && key < '128' && key != '45') )
                {
                    return true;
                }
                break;
        }
    }

    return false;
}

function pwOnKeyPressPhone(event, fkcountries)
{
    var key = event.keyCode;
    if( key == '0' ) key = event.which;

	switch(fkcountries)
	{
        default:
        case '32':
        case '183' :
    		if( (key >= '48' && key <= '57') || key == '32' ||
                 key == '35' || key == '45' || key == '13' || key == '37' ||
                 key == '39' || key == '9' || key == '189' || key == '38' ||
                 key == '40' || key == '41' || key == '16' || key == '222' || key == '8' ||
                 key == '46' || pwPreserveKey(key))
            {
                return true;
            }
            break;
        case 'others':
            break;
	}

    return false;
}


function pwOnKeyPressEmail(event)
{
    var key = event.keyCode;
    if( key == '0' ) key = event.which;
    if( ((key >= '48' && key <= '57') || key == '45' || (key >= '64' && key <= '90') || (key >= '128' && key <= '255') ||
         key == '95'|| (key >= '97' && key <= '122') || pwPreserveKey(key)) && ( key != '35' && key < '128' ))
    {
        return true;
    }

    return false;
}

function pwIsValidID(el, message)
{
    if(el)
    {
        try{
        var myform = el.form;
        var reg = new RegExp;
        el.value = el.value.replace(/^\s*|\s*|\s*$/g,''); //Trim.
        reg = /[\W\à\ä\â\À\Ä\Â\é\è\ë\ê\É\È\Ë\Ê\ì\î\ï\Ì\Ï\Î\ò\ô\ö\Ò\Ô\Ö\ù\û\ü\Ù\Û\Ü\*#\.\-\=\+\;\:\{\}\[\]\¯\!\/\$\%"?\&\(\)\<\>\«\»]+/gm; /*"*/
        if(reg.test(el.value))
        {
        	if(message!='')
        	{
	            alert(message);
	            setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
        }catch(er){}
    }
    return true;
}

function pwChangeStates(myfield,prefix)
{
    pwHideObject(prefix+'canada');
    pwHideObject(prefix+'usa');

    switch( myfield.value )
    {
        case '32' :
            pwShowObject(prefix + 'canada');
            break;
        case '183' :
            pwShowObject(prefix + 'usa');
            break;
    }

    return true;
}

function pwIsEmptyField(el, message)
{
    if( el && el.form )
    {
        try{
        var myform = el.form;
        el.value = el.value.replace(/^\s*|\s*$/g,'');
        if( !(el.value == "" || el.value.length == 0) )
        {
        	if(message!='')
        	{
	            alert(message);
	            setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
        }catch(er){}
    }
    return true;
}

function pwNotEmptyField(el, message)
{
    if( el && el.form )
    {
        try{
        var myform = el.form;
        el.value = el.value.replace(/^\s*|\s*$/g,'');
        if( el.value == "" || el.value.length == 0 )
        {
        	if(message!='')
        	{
	            alert(message);
	            setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
        }catch(er){}
    }
    return true;
}

function pwNotEmptyState(fkcountries, message)
{
    var id = 'fkstates_';

    switch(fkcountries)
    {
        case '32' :
            id = id + 'canada';
            break;
        case '61' :
            id = id + 'france';
            break;
        case '183' :
            id = id + 'usa';
            break;
    }
    if( document.getElementById(id).selectedIndex <= 0 )
    {
    	if(message!='')
    	{
        	alert(message);
		}
        return false;
    }

    return true;
}

function pwCheckMaxLength(el, max, message)
{
    if( el )
    {
        var myform = el.form;
        if( el.value.length > max )
        {
        	if(message!='')
        	{
            	alert(message);
            	setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
    }
    return true;
}

function pwCheckMinLength(el, min, message)
{
    if( el )
    {
        var myform = el.form;

        if( el.value.length < min )
        {
        	if(message!='')
        	{
	            alert(message);
	            setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
    }
    return true;
}

function pwCompareField(el1, el2, message)
{
    if( el1 && el2 )
    {
        if( el1.form == el2.form )
        {
            var myform = el1.form;
        }
        if( myform )
        {
            if( el1.value != el2.value )
            {
            	if(message!='')
            	{
	                alert(message);
	                setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el2.name +'");', 1);
				}
                return false;
            }
        }
    }
    return true;
}

function pwNotEmptyCombo(el, message)
{
    if( el )
    {
        var myform = el.form;

        if( el.selectedIndex <= 0 )
        {
        	if(message!='')
        	{
	            alert(message);
	            setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
			}
            return false;
        }
    }

    return true;
}

function pwValidateEmail(el, message)
{
    if( el )
    {
        if( el.value.length > 0 )
        {
            var reg = new RegExp;
            var email = el.value;

            var myform = el.form;

            email = email.replace(/^\s*|\s*|\s*$/g,''); // Trim.

            if( email.length > 0 )
            {
                reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i;

                if( !reg.exec(email) )
                {
                	if(message!='')
                	{
	                    alert(message);
	                    setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
					}
                    return false;
                }
                else
                {
                    el.value = email.toLowerCase();
                }
            }
        }
    }

    return true;
}

function pwValidatePostalCode(el, message, fkcountries)
{
    if( el )
    {
        if( el.value.length > 0 )
        {
            var reg = new RegExp;
            var code = el.value;
            var myform = el.form;

            code = code.replace(/^\s*|\s*|\s*$/g,''); //Trim.

            if( code.length > 0 )
            {
                switch( fkcountries )
                {
                    case '32' : // Canada
                        reg = /^([a-z]\d){3}$/i;
                        break;
                    case '61':
                    case '183': // France - États-Unis
                        reg = /^\d{4,10}$/;
                        break;
                    default:
                        reg = /^.{0,10}$/;
                        break;
                }

                if( reg.exec(code) )
                {
                    //Formatage du code postal.
                    if( fkcountries == '32' )
                    {
                        code = code.toUpperCase();
                        code = code.substring(0,3) + ' ' + code.substring(3,6);
                        el.value = code;
                    }
                }
                else
                {
                	if(message!='')
                	{
	                    alert(message);
	                    setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
					}
                    return false;
                }
            }
        }
    }

    return true;
}

function pwValidatePhone(el, message, fkcountries)
{
    if( el )
    {
        if( el.value.length > 0 )
        {
            var reg = new RegExp;
            var number = el.value;
            var myform = el.form;

            number = number.replace(/^\s*|\s*|\s*$/g,''); //Trim.

            if( number.length > 0 )
            {
                switch(fkcountries)
                {
                    case '32': //Canada
                    case '183': //Etats-Unies
                        reg = /^\d?[\-\s]?\(?\d{3}\)?[\-\s]?\d{3}[\-\s]?\d{4}\s?([\s\-\#]?\d{1,5})?$/;
                        break;
                    case '61':
                     	//reg = /^\d?[\-\s]?[\d{2}[\-\s\.]]{5}$/;
                       	// break;
                    default:
                    	break;
                }

                if( !reg.exec(number) )
                {
                	if(message!='')
                	{
	                    alert(message);
						setTimeout('pwSetFocus("'+ myform.attributes.getNamedItem('name').value + '","'+ el.name +'");', 1);
					}
                    return false;
                }
            }
        }
    }
    return true;
}

function pwSetFocus(formname, elname)
{
    if( formname != '' && elname != ''  )
    {
    	try{
    	document.forms[formname].elements[elname].focus();
		}catch(e){}
    }
}
