var notOk = 0;
var ok = 1;
var partOk = 2;

function isNotEmpty(id)
{
	return isTextNotEmpty(document.getElementById(id).value);
}

function isTextNotEmpty(text)
{
	return text.trim().length>0;
}

function areEqual(idPass, idConfirm)
{
    return areValuesEqual(document.getElementById(idPass).value,document.getElementById(idConfirm).value);
}

function areValuesEqual(v1,v2)
{
	return v1==v2;
}

function is(id,v)
{
	return isText(document.getElementById(id).value,v);
}

function isText(v1,v2)
{
	return eval(String(v1)+String(v2));
}

function isDateValid(id,fmt)
{
	return isTextDateValid(document.getElementById(id).value,fmt);
}

function isTextDateValid(dt,fmt)
{
	if(fmt=="")
		fmt="mm/dd/yyyy";

	a=dt.replace(/\s/,"").toLowerCase().match(/(\d+).(\d+).(\d+)/);
	b=fmt.replace(/\s/,"").toLowerCase().match(/(\w+).(\w+).(\w+)/);
	
	if(!a || !b || a.length!=4 || b.length!=4)
		return 0;
		
	for(i=1;i<4;i++)
	{
		if(b[i].length!=1 && a[i].length!=b[i].length)
			return 0;

		if(b[i]=="d" || b[i]=="dd")
			d=a[i];
		else if(b[i]=="m" || b[i]=="mm")
			m=a[i];
		else if(b[i]=="y" || b[i]=="yy" || b[i]=="yyyy")
			y=a[i];
	}
	
	dd=new Date(y,m-1,d);
	if(dd.getDate()!=d || dd.getMonth()!=m-1 || (dd.getFullYear()!=y) && dd.getYear()!=y)
		return 0;

	return 1;
}
function isEmailValid(id)
{
	return isTextEmailValid(document.getElementById(id).value);
}

function isTextEmailValid(email)
{
    emailRegEx = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;    
	return !(email.match(emailRegEx)==null);
}

function isNumber(id)
{
	return isTextNumber(document.getElementById(id).value);
}

function isTextNumber(text)
{
	numberRegEx = /^[0-9]+$/;	
	return !(text.match(numberRegEx)==null);
}

function isFileValid(id,ext)
{
	return isTextFileValid(document.getElementById(id).value,ext);
}

function isTextFileValid(text,ext)
{
	text = text.rtrim();
	text = text.replaceSlash();
	
	i = text.lastIndexOf(".")+1;
	j = text.lastIndexOf("/");
	
	t=ext.split(" ");
	if(i < text.length && i-2>j && i!=0)
	{
		type = text.substr(i, text.length-i);
		type = type.toLowerCase();
		
		for(k=0;k<t.length;k++)
			if(type==t[k])
				return 1;
	}
	return 0;
}

function isZipValid(id)
{
	return isTextZipValid(document.getElementById(id).value);

}

function isTextZipValid(text)
{
	zipRegEx = /^\d{5}(-\d{4})?$/;	
	return !(text.match(zipRegEx)==null);
}

function isPhoneValid(id)
{
	return isTextPhoneValid(document.getElementById(id).value);
}

function isTextPhoneValid(text)
{
	phoneRegEx = /^((\(\d{3}\)?)|(\d{3}-))?\d{3}-\d{4}$/;
	return !(text.match(phoneRegEx)==null);
}

function isSelected(id)
{
	return !(document.getElementById(id).value == "");
}

function isItemNotSelected(id, val)
{
	return document.getElementById(id).value != val;
}

function isGroupSelected(g,err,msg)
{
	e=0;

	for(i=0;i<g.length;i++)
		if(g[i].checked)
			e=1;

	if(msg.length!=0)
		document.getElementById(err).innerHTML=msg;
			
	if(e==0)
		document.getElementById(err).style.visibility='visible';
	else
		document.getElementById(err).style.visibility='hidden';
	
	return e;	
}

String.prototype.trim = function () 
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.ltrim = function () 
{
    return this.replace(/^\s*/, "");
}

String.prototype.rtrim = function () 
{
    return this.replace(/\s*$/, "");
}

String.prototype.replaceSlash = function () 
{
    return this.replace(/\\/, "/");
}

function validate(s,m)
{
	a=s.replace(/,{2}/g,",").split(",");
	if(a.length<3)
		return 1;
	
	id=a[0];
	msg=a[1];
	fn=a[2];
	if(a.length>3)
		x=","+"'"+a[3].trim()+"')";
	else
		x=")";

	if(eval(fn+"('"+id+"'"+x+";")==0 )
	{
		if(m.length!=0)
			document.getElementById(msg).innerHTML=m;
		document.getElementById(msg).style.visibility='visible';
		return 0;
	}
	else
	{
		document.getElementById(msg).style.visibility='hidden';
		return 1;
	}	
}

