var phone = new Array(3,6,10);
var ssn = new Array(3,5,9);
var showtime;
var prevColor;
var prevWidth;
var newColor = "red"
var newWidth="3px";

function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}



function initialize()
{
	//eval("document.forms[0].onsubmit = function foo() { alert(globalError); return !globalError; }");
	//alert(document.forms[0]);
	
   	var theRules = new Array();
   	//dumpProps(document.styleSheets[2].rules);
	if (document.styleSheets[2].cssRules)
		theRules = document.styleSheets[1].cssRules
	else if (document.styleSheets[2].rules)
		theRules = document.styleSheets[2].rules
	for (i=0;i<theRules.length;i++)
	{
	   // alert(theRules[i].selectorText.indexOf(".tbPhone"));
	    if (theRules[i].selectorText.indexOf(".tbPhone") >= 0)
		{
		    //alert("in if:"+theRules[i].style.borderColor);
		    prevColor = theRules[i].style.borderColor;
		    prevWidth = theRules[i].style.borderWidth;
		}
	}
	//alert(prevWidth+","+prevColor);
}

initialize();

function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}


function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}


function passValidation(fieldValue)
{
	for(var i=0;i<fieldValue.length;i++)
	{
		if(!isDigit(fieldValue.charAt(i)) && fieldValue.charAt(i) != "-")
		{
			return false;
		}
	}
	return true;
}

var startedobject;

function doBlink(object)
{	
	startedobject=object.id;
	if (showtime)
	{
		clearInterval(showtime);
	}
	object.focus();
	showtime = setInterval("flashit('"+object.id+"')", speed)
}


function ParseNumber(fieldName)
{
var FmtStr="";
var index = 0;
var arr = new Array();
var LimitCheck;
var checkPassed=false;
var fieldValue=fieldName.value;
var fieldType=fieldName.format;
LimitCheck = fieldValue.length;
	if (fieldValue.length < 1)
	{
		if(startedobject==fieldName.id)
		{
			fieldName.style.borderWidth=prevWidth;
			fieldName.style.borderColor=prevColor;
			clearInterval(showtime);
		}
		FmtStr = "";
		return FmtStr;
	}

	if (fieldType=='ph')
	{
		arr = phone;
	}
	else if (fieldType=='ssn')
	{
		arr=ssn;
	}


	while (index != LimitCheck)
	{
		if (isNaN(parseInt(fieldValue.charAt(index))))
		{ 
		
		}
		else
		{ 
			FmtStr = FmtStr + fieldValue.charAt(index); 
		}
		index = index + 1;
	}

	
	
	if (FmtStr.length>0 && passValidation(fieldValue))
	{
		FmtStr = FmtStr.substring(0,arr[0]) + "-" + FmtStr.substring(arr[0],arr[1]) + "-" + FmtStr.substring(arr[1],arr[2]);
		checkPassed=true;
	}
	else
	{
		//FmtStr = fieldValue;
		FmtStr = "";
		checkPassed=false;
	}
		
	
	
	if (checkPassed)
	{
		if (FmtStr.length>0 && FmtStr.length!=arr[2]+2)
		{
			checkPassed = false;
			doBlink(fieldName);
		}
		else
		{
			if(startedobject==fieldName.id)
			{
				fieldName.style.borderWidth = prevWidth;
				fieldName.style.borderColor = prevColor;
				clearInterval(showtime);
			}
		}
	}
	else
	{
		doBlink(fieldName);
		//event.returnValue = false;
	}

	return FmtStr;
}


var speed=500;
 

function flashit(obj){
    var crosstable = document.getElementById(obj);
	if (crosstable){
		if (crosstable.style.borderWidth==prevWidth)
		{
			crosstable.style.borderWidth=newWidth;
			crosstable.style.borderColor=newColor;
		}
		else
		{
			crosstable.style.borderWidth=prevWidth;
			crosstable.style.borderColor=prevColor;
		}
	}
}

function ParseNumberReturnBoolean(fieldName)
{
var FmtStr="";
var index = 0;
var arr = new Array();
var LimitCheck;
var checkPassed=false;
var fieldValue=fieldName.value;
var fieldType=fieldName.format;
LimitCheck = fieldValue.length;
	if (fieldValue.length < 1)
	{
		FmtStr = "";
		return true;
	}

	if (fieldType=='ph')
	{
		arr = phone;
	}
	else if (fieldType=='ssn')
	{
		arr=ssn;
	}


	while (index != LimitCheck)
	{
		if (isNaN(parseInt(fieldValue.charAt(index))))
		{ 
		
		}
		else
		{ 
			FmtStr = FmtStr + fieldValue.charAt(index); 
		}
		index = index + 1;
	}

	
	
	if (FmtStr.length>0 && passValidation(fieldValue))
	{
		FmtStr = FmtStr.substring(0,arr[0]) + "-" + FmtStr.substring(arr[0],arr[1]) + "-" + FmtStr.substring(arr[1],arr[2]);
		checkPassed=true;
	}
	else
	{
		FmtStr = fieldValue;
		//FmtStr = "";
		checkPassed=false;
	}
		
	
	
	if (checkPassed)
	{
		if (FmtStr.length>0 && FmtStr.length!=arr[2]+2)
		{
			checkPassed = false;
			//doBlink(fieldName);
		}
		else
		{
			checkPassed = true;
		}
	}
	else
	{
		checkPassed = false;
	}

	return checkPassed;
}
