// TRIM
String.prototype.trim = function(){
	return this.replace(/(?:^\s+|\s+$)/g, "");
}

// redirection dans le d-CMS depuis les listing
function redirect(sect,act,id,mec,step){
	window.location.href = "index.php?pages="+sect+"&act="+act+"&id="+id+"&mec="+mec+"&step="+step;
}

function deleteItem(table,id){
	if (id > 0) {
		$('tr[id="item'+id+'"] td').css({
			'backgroundImage'	: 'none',
			'backgroundColor'	: 'white'
		});
		$('tr[id="item'+id+'"] td').animate({
			'backgroundColor'	: '#ff8888',
			'color'				: '#941010'
		}, 300);
		$.get('includes/deleteItem.php', {
			idsup	:id,  //id � supprimer
			table	:table //table sur laquelle on va appliquer la suppression
		}, function(data){
			alert(data);
			if (data == '1') {
				$('tr[id="item'+id+'"]').fadeTo("slow", 0, function(){
					$(this).hide();
				});
			}else{
				alert('Probl�me de connexion � la base de donn�e');
			}
		});
	}
}

function array_search (needle, haystack, argStrict) {
    var strict = !!argStrict;
    var key = '';
    for (key in haystack) {
    	if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            return key;
        }
    }
    return false;
}


function clearForm(form) {
	$(':input', form).each(function() {
		var type = this.type;
		var tag = this.tagName.toLowerCase(); // normalize case
		if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = "";
		else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
		else if (tag == 'select')
		this.selectedIndex = 0;
	});
};


function is_numeric (mixed_var) {
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}


var seconds_to_wait = 2;
function pause(){
	var timer = setTimeout(function(){
		seconds_to_wait--;
		if(seconds_to_wait > 0){
			pause();
		}else{
			//SOMETHING TO DO
		};
	},1000);
};pause();

function isInt(x) {
	var y=parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
}

//	CHECK DATE + FORMATAGE :: fld = champs
function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);
        alert(testDate)
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
    }
    return false;
}


