//##############################################################
// pes validator_libs
// requires JavaScript1.2 - uses string.match()

function word_count(str)
//##############################################################
{
	var wordsarray = str.split(/\b\W+\b/);
	return wordsarray.length;
}

function is_valid_url(url)
//##############################################################
{
	// 31-MAY-2006 SI - do not allow non-ascii chars in a URL
	var ok_charset = is_ok_charset(url);
	if (ok_charset != true)
	{	return false;
	}

	var URL = url.toUpperCase();

	var protocol = ['HTTP://', 'HTTPS://', 'FTP://'];
	var protocol_ok = false;

	for (var i=0; i<protocol.length; i++ )
	{	var p = protocol[i];
		var n = p.length;

		// do the first n charcters of the URL match a protocol?
		if (URL.substring(0,n) == p)
		{	protocol_ok = true;
			break;
		}
	}

	if (protocol_ok != true)
	{	return false;
	}

	// (stuff.)+stuff(optional:80)slash
	var match1 = url.match(/(\w+[.])+(\w\w+)(:\d+)?\//);

	// (stuff.)+stuff(optional:80)EOL
	var match2 = url.match(/(\w+[.])+(\w\w+)(:\d+)?\s*$/);

	if ((match1 == null) && (match2 == null))
	{	return false;
	}

	return true;
}

function is_password_bad(pw)
//##############################################################
{
	return pw.match(/[^A-Za-z0-9[!#$%=+|~`:;,{}<>\/*()'?"&\]]/);
}


function is_email_ok(email)
//##############################################################
{	// foo@bar.(maybemore.)xx or foo@bar.(maybemore.)xxx
	var ok_format  = email.match("^[^@ ]+@([^@. ]+[.])+[^@. ][^@. ][^@. ]?$");
	var ok_format2 = email.match("^[^@ ]+@([^@. ]+[.])+info$");

	// 07-JUN-2005 SI - do not allow non-ascii chars in an email address
	var ok_charset = is_ok_charset(email);

	//debug("ok_format="+ok_format);
	//debug("ok_charset="+ok_charset);
	return (ok_format || ok_format2) && ok_charset;

}

function is_ok_charset(txt)
//##############################################################
{
	// 31-MAY-2006 SI - moved this from is_email_ok so it can be shared

	var ok_charset = true;
	for(var i=0; i<txt.length; i++)
	{
		var unicode = txt.charCodeAt(i);
		//debug("i="+i+" unicode="+unicode);
		if (unicode > 255)
		{
			ok_charset = false;
			break;
		}
	}

	return ok_charset;
}

function getNameAfterDot(str)
//##############################################################
{
	var a=str.lastIndexOf(".");
	if (a==-1) return str;
	str=str.substring(a+1,str.length);
	return str;
}

function validator(array)
//##############################################################
// AUG-12-2004 SI -
// can now accept an array of names,
// and will validate only those named fields.
{
	var is_form_element;

	if (! validator_on)
	{	alert("You have disabled JS validation.  To reenable, add ?jsv=true to the URL");
		return;
	}

	if (array.constructor == Array)
	{
		is_form_element = false;
	}
	else
	{
		// assume we were passed the Form
		is_form_element = true;
		array = array.elements;
	}

	var have_seen = [];
	var len = array.length;
	for(i=0; i<len; i++)
	{
		var obj = array[i];
		var name0 = (is_form_element) ? obj.name : obj;
                
                //6610389::GEM Login- Not showing error for required field if user does not select a CDP before submitting                
                //Misteriously some undefined form fields are coming, so second condition will take care of it..
                //if (name0==null) continue;
                //if (name0==null || name0==undefined) continue;
		if ( is_empty(name0) ) continue;

		var name = getNameAfterDot(name0);

		// skip appinfo hidden tags 
		if ( is_app_info(name0) ) { continue; }


		// skip Jato 2.1.4's checkbox peer hidden fields 
		if (name == "jato_boolean") { continue; }

		// prevent duplicate error messages being
		// generated for the same tag
		if (have_seen[name] == true) continue;

		value = get_pes_val(name);
		//value = value.replace(/^\s+/,"");
		//value = value.replace(/\s+/,"");

		validate_field(name, value);

		have_seen[name] = true;
	}
}
