
//------------------------------------------------------------
//	Data Validations - Copyright 2005-2009 - Abhijit Choudhari.  All rights reserved.
//	Coded by: Abhijit Choudhari (abhijit_v_choudhari@yahoo.com)
//	
//	Retain this copyright notice in the script.
//	License is granted to user to reuse this code on 
//	their own website if, and only if, 
//	this entire copyright notice is included.
//
// You don't need to modify anything below this line.
//--------------------------------------------------------------

// @(#)_dataValidation.js
// @author Abhijit Choudhari
// @version 1.0
// This file contains the data validation JavaScript functions
// It is included in the HTML pages with forms that need these
// data validation routines.


// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";

//* ************************************************************** 

function onClickPopup(strLink)
{
//alert(strLink);
var targetWindow = "NewDocument";
var featuresWindow = "scrollbars=yes,resizable=yes,toolbar=no,status=no,menubar=yes,location=no,height=300,width=600";

newWindow = window.open(strLink, targetWindow, featuresWindow, true);
}

//* ************************************************************** 

function windowOpener(s) {
newWindow = window.open(s,'PRIVATE','toolbar=no,status=no,menubar=no,systemMenu=no,location=no,resizable=no,height=223,width=392',true);
}

//* ************************************************************** 

function replaceAll (s, fromStr, toStr)
{
	var new_s = s;
	for (i = 0; i < 5000 && new_s.indexOf (fromStr) != -1; i++)
	{
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

//* ************************************************************** 

// PURPOSE:  Since we are using the single tick mark as the
//	string delimiter to construct our SQL queries, a string with
//	a tick mark in it will cause a SQL error.  Therefore we replace
//	all "'" with "''", which eliminates the possibility of a SQL error.
//

function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "`");
	//new_s = replaceAll (new_s, "|", "");
	//new_s = replaceAll (new_s, "[", "");
	//new_s = replaceAll (new_s, "]", "");
	//new_s = replaceAll (new_s, "(", "");
	//new_s = replaceAll (new_s, ")", "");
	new_s = replaceAll (new_s, "\"", "`");
	//new_s = replaceAll (new_s, ":", " ");
	//new_s = replaceAll (new_s, ";", " ");
	//new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

//* ************************************************************** 

function makeSafe (i)
{
	i.value = sqlSafe (i.value);
}

//* ************************************************************** 

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0));
}

//* ************************************************************** 

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

//* ************************************************************** 

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{
	var strField = new String(objField.value);

	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName + ".");
		objField.focus();
		objField.select();
		return false;
	}

	makeSafe(objField);

	return true;
}

function ForceEntry2(objField, FieldName)
{
	var strField = new String(objField.value);
	strField = replaceAll(strField, "&nbsp;", " ");

	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName + ".");
		return false;
	}

	makeSafe(objField);
	return true;
}

function ForceEntry3(objField, FieldName)
{
	var strField = new String(objField.value);

	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName + ".");
		objField.focus();
		return false;
	}

	makeSafe(objField);

	return true;
}

function ForceEntry4(objField, strMSG)
{
	var strField = new String(objField.value);

	if (isWhitespace(strField)) {
		alert(strMSG);
		objField.focus();
		objField.select();
		return false;
	}

	makeSafe(objField);

	return true;
}

function isNumericValue(objField, FieldName)
{
	var strField = new String(objField.value);
	if ( isNaN( strField ) )
	{
		alert("The " + FieldName + " can not be non-numeric.");
		objField.focus();
		objField.value = "";
		return false;
	}
	
	return true;
}

//* ************************************************************** 

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError)
{
	alert("You have entered an invalid value for " + strError + ".");
	Field.focus();
}

//* ************************************************************** 

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (objField)
{
	var s = new String(objField.value);

    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false; //defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else
	{
		makeSafe(objField);
 		return true;
	}
}

//* ************************************************************** 

    function Toggle(e)
    {
	if (e.checked) {
	    Highlight(e);
	}
	else {
	    Unhighlight(e);
	}
    }

    function Highlight(e)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
	    if (r.className == "roworgWhite") {
		r.className = "roworgWhiteSelected";
	    }
	    else if (r.className == "roworgLemon") {
		r.className = "roworgLemonSelected";
	    }
	}
    }

    function Unhighlight(e)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
	    if (r.className == "roworgWhiteSelected") {
		r.className = "roworgWhite";
	    }
	    else if (r.className == "roworgLemonSelected") {
		r.className = "roworgLemon";
	    }
	}
    }

	function textCounter(field, maxlimit)
 	{
		if (field.value.length > maxlimit)
 		{
			field.value = field.value.substring(0,maxlimit);
			alert("Textarea value can only be " + maxlimit + " characters in length.");
			return false;
		}
	}

	function bw_check() {
	var is_major=parseInt(navigator.appVersion);
	this.nver=is_major;
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera=window.opera?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom&&!this.opera)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom&&!this.opera)?1:0;
	this.ie4=(document.all&&!this.dom&&!this.opera)?1:0;
	this.ie3=(this.ver.indexOf("MSIE")&&(is_major<4));
	this.ie=this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom&&parseInt(this.ver)>=5)?1:0;
	this.ns4=(document.layers&&!this.dom&&!this.hotjava)?1:0;
	this.hotjava=(this.agent.toLowerCase().indexOf('hotjava')!=-1)?1:0;
	this.ns=this.ns6||this.hotjava;
	if(this.ie) { gUserClient = "ie" ;}
	if(this.ns) { gUserClient = "ns" ;}
	if(this.ns4) { gUserClient = "ns4" ;}
	}

	//NO CHARACTERS ALLOWED!
	//Allow only intergers & decimal points
	function keyCheck(eventObj, obj) {
	var keyCode
	// Check For Browser Type
	if (document.all){ keyCode=eventObj.keyCode	}
	else{ keyCode=eventObj.which }

	var str=obj.value
	if(keyCode==46){ if (str.indexOf(".")>0){ return false } }
	if((keyCode<48 || keyCode >58) && (keyCode != 46)){ // Allow only integers and decimal points
		return false }

	return true }

	//Zoom-In / Zoom-Out 
	//Specify affected tags. Add or remove from list:
	var tgs = new Array( 'div','td','tr','font');
	//Specify spectrum of different font sizes:
	var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
	var startSz = 2;
	function ts( trgt,inc ) {
		if (!document.getElementById) return
		var d = document,cEl = null,sz = startSz,i,j,cTags;
		
		sz += inc;
		if ( sz < 0 ) sz = 0;
		if ( sz > 6 ) sz = 6;
		startSz = sz;
			
		if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

		cEl.style.fontSize = szs[ sz ];
		//alert("w: " + cE1.style)

		for ( i = 0 ; i < tgs.length ; i++ ) {
			cTags = cEl.getElementsByTagName( tgs[ i ] );
			for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
		}
	}

	////// NEWLY ADDED - 2 SEP 2006	"&lt;  table &gt;"
	function stripUnwantedHTMLTags(strHTML) { 

		var r2, r2prev, re2, s2;
		s2 = strHTML;

		r2 = "";
		r2prev = "";
		for ( var loop = 0 ; loop < 5000 ; loop++ ) {
			r2prev = r2;
			r2 = s2.replace("&lt;", "<");
			if( r2prev == r2 ) break;
			s2 = r2;
			
			r2 = s2.replace("&gt;", ">");
			if( r2prev == r2 ) break;
			s2 = r2;
		}

		// For REMOVING: 
		// Input:  <table><tr><td><i>text</i></td></tr></table>
		// Output:  <i>text</i>
		re2 = /(<[/]?t((body)|(able)|d|r).*?>)/i;
		r2 = "";
		r2prev = "";
		for ( var loop = 0 ; loop < 5000 ; loop++ ) {
			r2prev = r2;
			r2 = s2.replace(re2, "");
			if( r2prev == r2 ) break;
			s2 = r2;
		}

		// For REMOVING: 
		// Input:  <i>text<!-- comments here --></i>
		// Output:  <i>text</i>
		re2 = /(<!.*?>)/i;
		r2 = "";
		r2prev = "";
		for ( var loop = 0 ; loop < 5000 ; loop++ ) {
			r2prev = r2;
			r2 = s2.replace(re2, "");
			if( r2prev == r2 ) break;
			s2 = r2;
		}

		// For REMOVING: 
		// Input:  <i>text<!-- comments here --></i>
		// Output:  <i>text comments here </i>
		r2 = "";
		r2prev = "";
		for ( var loop = 0 ; loop < 5000 ; loop++ ) {
			r2prev = r2;
			r2 = s2.replace("<!--", "");
			if( r2prev == r2 ) break;
			s2 = r2;
			
			r2 = s2.replace("-->", "");
			if( r2prev == r2 ) break;
			s2 = r2;
		}

		return s2;
	}

function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
   return false;

return true;
}

function PopupCenter(pageURL,title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 
//usage:
//<a href="javascript:void(0);" onclick="PopupCenter('http://abhijitchoudhari.com', 'myPop1',400,400);">CLICK TO OPEN POPUP</a>

//------------------------------------------------------------
// End Of Data Validations JS
//------------------------------------------------------------


