/*
********************************************
 Purpose :	Validates the fields for number
********************************************
*/ 
function IsNumber(UserValue)
{
var lFlag  = true
if (UserValue.indexOf(",", 0) >= 0 || UserValue.indexOf("-", 0) >= 0)
	lFlag = false
else
	lFlag = true
return 	lFlag
}
/*
**********************************************************************************
 Purpose :	Validates the fields for sapces
			If all characters are space , returns false
			else returns true
***********************************************************************************
*/
function checkSpace(strValue)
{
var lflag = false;
for(i=0;i<strValue.length;i++)
{
	if (strValue.charAt(i) != " " && strValue.charAt(i) != "\t" )
	{
		lflag = true
		break
	}
}
return lflag
}
/*
**********************************************************************************
 Purpose :	Removes all spaces in a String
***********************************************************************************
*/
function TrimSpace(strText)
{
  var intLength = strText.length
  var intStartPos = 0
  var intEndPos = intLength 
  var strOut =  ""
	for (i=0;i<intEndPos;i++)
	{
		if (strText.charAt(i)!= " ")
		{
			strOut	= strOut + strText.charAt(i)
		}
	}
	return strOut
}

function Trim(s) 
{
  // spaces and carriage returns at beginnig
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // spaces and carriage returns at end

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function validate_alphanumeric_data(urstr)
{
  var i;
  var result;

  for (i=0; i<urstr.length; i++) {
    if (
      ((urstr.charAt(i) >= "A") && (urstr.charAt(i) <= "Z")) ||
      ((urstr.charAt(i) >= "a") && (urstr.charAt(i) <= "z")) ||
      ((urstr.charAt(i) >= "0") && (urstr.charAt(i) <= "9")) ||
      ((urstr.charAt(i) == "-"))
      )
      result = true;
    else 
      return false;
  }

  if (result == true)
    return true;
}

function ReplaceText(pContent, pTag, pValue) 
{
	pContent = pContent.replace(pTag, pValue);
	if(pContent.indexOf(pTag) != -1) {
		pContent = ReplaceText(pContent, pTag, pValue);
	}
	return pContent;
}

function LineBreak(pTxt, MaxChars)
{
	MaxChars--;
	pTxt = "" + pTxt;
	var strOut = "";
	var chcount = 0;
	for (var i = 0; i < pTxt.length; i++) // each char
	{
		var ch = pTxt.substring(i, i+1);

		if (ch == '\n') // if hard return
		{
			strOut += ch;
			chcount = 1;
		}
		else
		{
			//if (chcount == MaxChars) // max now
			if (chcount >= MaxChars) // max now
			{
				if (ch == ' ')
				{
					//strOut += '\n' + ch; // go to next line
					strOut += '\n'; // go to next line and don't add the space
					chcount = 1; // reset chcount
				}
				else //already hit max and found space.
				{
					strOut += ch;
					chcount++; // so add 1 to chcount				
				}	
			}
			else  // Not a newline or max characters ...
			{
				strOut += ch;
				chcount++; // so add 1 to chcount
			}
	   }
	}
	return (strOut);
}

function QSValue(pQS, pID)
{
    var rootArr = pQS.split("&");
    var strOut = "";
    for(i=0;i<rootArr.length;i++)
    {
        var tempArr =  rootArr[i].split("=");
        if(tempArr.length == 2)
        {
			if (tempArr[0] == pID)
			{
				strOut = tempArr[1];
				break;
			}
        }
    }
    return strOut;
}

function UParam(pT)
{
	var intIndex = pT.indexOf('?');
	if (intIndex != -1)
	{
		pT = pT.slice(intIndex + 1);
	}	
	//pT = escape(pT);
	return pT; 
}

function Pop_Win(pURL, pName, pW, pH, pSCR)
{
	var PWin = window.open(pURL,pName,'left=0,top=0,width=' + pW + ',height=' + pH + ',scrollbars=' + pSCR);
	PWin.focus();
}

function DatePick(pObj, pRestrict)
{
//var strObj = "frmMain." + pObj;
var strObj = pObj;
if (pRestrict == null) {pRestrict = "N";}
var objWin = window.open("/calendar.aspx?R=" + pRestrict + "&fill=" + strObj, 'caledar','width=220,height=160,left=0,top=0,scrollbars=0,resizable=0,toolbar=0,status=0');
objWin.focus();
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    var strYear = new String(year);
    switch (strYear.length)
    {
		case 1:
			strYear = "200" + strYear;
			break;
		case 2:
			strYear = "20" + strYear;
			break;
	}		
	year = parseInt(strYear); 
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr, pMsg){
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pMsg != "")
		pMsg = pMsg + "\n\n";
	else
		pMsg = "";
		
	if (!checkSpace(dtStr)){
		alert(pMsg + "Should not be empty" )
		return false
	}
	if (pos1==-1 || pos2==-1){
		alert(pMsg + "The date format should be : mm-dd-yy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert(pMsg + "Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert(pMsg + "Please enter a valid day")
		return false
	}
	if (strYear.length != 2 && strYear.length != 4)
	{
		alert("Please enter a valid 2 or 4 digit year")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert(pMsg + "Please enter a valid date")
		return false
	}
return true
}

function CDateLR(pDate)
{
	if (checkSpace(pDate))
	{
		var reg = /-/g;
		pDate = pDate.replace(reg, "/");
		var dtCh = "/";
		var pos1=pDate.indexOf(dtCh)
		var pos2=pDate.indexOf(dtCh,pos1+1)
		var strMonth=pDate.substring(0,pos1)
		var strDay=pDate.substring(pos1+1,pos2)
		var strYear=pDate.substring(pos2+1)
		if (strYear.length == 2)
			strYear = "20" + strYear;

		pDate = strMonth + "/" + strDay + "/" + strYear;
	}
	else
		pDate = ""

	return pDate;
}

function QStringParse()
{
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	var qsParm = new Array();
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	return qsParm;
}

function Go(pPage, pFrame)
{
	if (pFrame == "WF")
		window.location.href = pPage;
	else
		window.parent.location.href = pPage;
}

function GoBlank(pPage)
{
	window.location.href = "/blank.htm";
}
function getviewpoint()
{
    var ie=document.all && !window.opera
    var domclientWidth=document.documentElement && parseInt(document.documentElement.clientWidth) || 100000 //Preliminary doc width in non IE browsers
    this.standardbody = (document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
    this.scroll_top = (ie)? this.standardbody.scrollTop : window.pageYOffset
    this.scroll_left = (ie)? this.standardbody.scrollLeft : window.pageXOffset
    this.docwidth = (ie)? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(domclientWidth, window.innerWidth-16)
    this.docheight = (ie)? this.standardbody.clientHeight: window.innerHeight
    return this;
}
