//-------------------- A JavaScript Library by S M Humayun --------------------

js_error_phone = "Only digits,+,-,(,) and space are allowed.";
js_error_id = "1). First letter must be an alphabet or _\n2). Following letters must be an alphabet or digit or _";
js_error_alphanumeric = "Only digits, alphabets and _ are allowed.";
js_error_date = "Only digits and / are allowed. [Format : MM/DD/YYYY]";
js_error_number = "Only digits are allowed.";
js_error_email = "1). First letter must be an alphabet\n2). Following letters must be an alphabet or digit or _ or . or @\n3). Only single occurance of @ is allowed and is mandatory\n4). @ or . must not be preceded or followed by any one of them\n5). Last 2 letters must not be . or @";
js_error_number = "Only digits are allowed";
js_error_editoff = "To edit any field click EDIT button to enter in edit mode";
js_error_accessdenied = "You are not authorized to EDIT information";

function checkPhone(pn)
	{
	var str	=	pn.toString();
	checkOK	=	"1234567890+-() ";
	for(var i=0;i<str.length;i++)
		{
		for(var j=0;j<checkOK.length;j++)
			{
			if(str.charAt(i)==checkOK.charAt(j))
				break;
			else if(j==(checkOK.length-1))
				return false;
			}
		}
	return true;
	}

function checkID(tempID)
	{
	var str	=	tempID.toString();
	checkOK	=	"1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for(var i=0;i<str.length;i++)
		{
		k = 0
		if(i==0)
			k = 11;
		for(var j=k;j<checkOK.length;j++)
			{
			if(str.charAt(i)==checkOK.charAt(j))
				break;
			else if(j==(checkOK.length-1))
				return false;
			}
		}
	return true;
	}
	
function checkDate (dateValue)
	{
	if (dateValue.length < 8 || dateValue.length > 10) return false;
	da = dateValue.split("/");
	if ( da.length != 3 ) return false;
	d = da[0];
	m = da[1];
	y = da[2];
	if ( m.length < 1 || m.length > 2 || !checkNumber(m)) return false;
	if ( d.length < 1 || d.length > 2 || !checkNumber(d)) return false;
	if ( y.length < 4 || y.length > 4 || !checkNumber(y)) return false;
	if ( m < 1 || m > 12 ) return false;
	if ( d < 1 || d > 31 ) return false;
	if ( y < 0 || y > 9999 ) return false;
	if ( m == 4 || m == 6 || m == 9 || m == 11 )
		{
		if ( d == 31 ) return false;
		}
	if ( m == 2 )
		{
		var g = parseInt(y/4);
		if ( isNaN(g) ) return false;
		if ( d > 29 ) return false;
		if ( d == 29 && ( ( y / 4 ) != parseInt ( y / 4 ) ) ) return false;
		}
	return true;
	}

function checkDateTime (dateValue)
	{
	if (dateValue.length < 8 || dateValue.length > 22) return false;
	da = dateValue.split("/");
	if ( da.length != 3 ) return false;
	d = da[0];
	m = da[1];
	y = da[2];
	hh = '0';
	mm = '0';
	ss = '0';
	ap = 'AM';
	if (y.length > 4)
		{
		ta = y.split(" ");
		if ( ta.length != 3 ) return false;
		y = ta[0];
		ap = ta[2];
		ta2 = ta[1].split(":");
		if ( ta2.length != 3 ) return false;
		hh = ta2[0];
		mm = ta2[1];
		ss = ta2[2];
		}
	if ( m.length < 1 || m.length > 2 || !checkNumber(m)) return false;
	if ( d.length < 1 || d.length > 2 || !checkNumber(d)) return false;
	if ( y.length < 4 || y.length > 4 || !checkNumber(y)) return false;
	if ( m < 1 || m > 12 ) return false;
	if ( d < 1 || d > 31 ) return false;
	if ( y < 0 || y > 9999 ) return false;
	if ( m == 4 || m == 6 || m == 9 || m == 11 )
		{
		if ( d == 31 ) return false;
		}
	if ( m == 2 )
		{
		var g = parseInt(y/4);
		if ( isNaN(g) ) return false;
		if ( d > 29 ) return false;
		if ( d == 29 && ( ( y / 4 ) != parseInt ( y / 4 ) ) ) return false;
		}
	if ( da[2].length > 4)
		{
		if ( hh.length < 1 || hh.length > 2 || !checkNumber(hh)) return false;
		if ( mm.length < 1 || mm.length > 2 || !checkNumber(hh)) return false;
		if ( ss.length < 1 || ss.length > 2 || !checkNumber(hh)) return false;
		hh = parseInt(hh);
		mm = parseInt(mm);
		ss = parseInt(ss);
		if ( hh < 0 || hh > 12 ) return false;
		if ( mm < 0 || mm > 59 ) return false;
		if ( ss < 0 || ss > 59 ) return false;
		ap = ap.toUpperCase();
		if ( ap != 'AM' && ap != 'PM' ) return false;
		}
	return true;
	}

function checkTime(timeValue)
	{
	if (timeValue.length < 3 || timeValue.length > 5) return false;
	da = timeValue.split(":");
	if ( da.length != 2 ) return false;
	hh = da[0];
	mm = da[1];

		if ( hh.length < 1 || hh.length > 2 || !checkNumber(hh)) return false;
		if ( mm.length < 1 || mm.length > 2 || !checkNumber(mm)) return false;
		hh = parseInt(hh);
		mm = parseInt(mm);
		if ( hh < 0 || hh > 12 ) return false;
		if ( mm < 0 || mm > 59 ) return false;

	return true;
	}

function checkAlphaNumeric(str) 
	{
	var str	=	str.toString();
	checkOK	=	"asdfghjklqwertyuiopzxcvbnm1234567890_";
	for(var i=0;i<str.length;i++)
		{
		for	(var j=0;j<checkOK.length;j++)
			{
			if(str.charAt(i)==checkOK.charAt(j))
				break;
			else if(j==(checkOK.length-1))
				return false;
			}
		}
	return true;
	}
	
function checkNumber (str) 
	{
	var str	=	str.toString();
	checkOK	=	"1234567890";
	for(var i=0;i<str.length;i++)
		{
		for	(var j=0;j<checkOK.length;j++)
			{
			if(str.charAt(i)==checkOK.charAt(j))
				break;
			else if(j==(checkOK.length-1))
				return false;
			}
		}
	return true;
	}

function checkEmail(evalue)
	{
	errfnd     = 0;
	elen       = evalue.length;
	efirstchar = evalue.substring(0, 1);
	elastchar  = evalue.substring(elen-1, elen);
	
	atPos = evalue.indexOf("@",1) // there must be one "@" symbol
	if (atPos == -1) 
		errfnd = 1;
	if (evalue.indexOf("@",atPos+1) != -1) // and only one "@" symbol
	  errfnd = 1;
	periodPos = evalue.indexOf(".",atPos)
	if (periodPos == -1) // and at least one "." after the "@"
	  errfnd = 1;
	if (periodPos+3 > elen) // must be at least 2 characters after the "."
	  errfnd = 1;
	if (evalue.indexOf("..",1) != -1) // no consecutive "."
	  errfnd = 1;
	if (evalue.indexOf("@.",1) != -1) // no consecutive "@."
	  errfnd = 1;
	if (evalue.indexOf(".@",1) != -1) // no consecutive ".@"
	  errfnd = 1;
	// first character and last character must be an alphabet
	if((efirstchar<"a" || efirstchar>"z") && (efirstchar<"A" || efirstchar>"Z"))  
	  errfnd = 1;
	if((elastchar<"a" || elastchar>"z") && (elastchar<"A" || elastchar>"Z"))  
	  errfnd = 1;
	// email-id can have only alphabets, digits, underscore, @ and .
	for (var i = 1; i < elen; i++) 
		{
	  var ch = evalue.substring(i, i + 1);
	  if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_') && (ch != '-') && (ch != "@") && (ch != "."))
	     errfnd = 1;
		}
	return !errfnd;
	}
