function GerRedAsterisks()
{
    var span = document.createElement("span");
    span.className = "astricks";
    var spanText = document.createTextNode("*");
    span.appendChild(spanText);
    return span;
}
function IsValidPositiveInteger(val)
{
    if(String(val).length == 0)
        return false;
        
    if(isNaN(1 * val))
    {
        return false;
    }
	
    var sVal = String(val);
    if(sVal.indexOf('.') != -1)
    {
        return false;
    }
	
    if(parseInt(val) < 1)
    {
        return false;
    }
	
    return true;
}

function IsValidPositiveDecimal(val)
{
    if(String(val).length == 0)
        return false;
        
    if(isNaN(1 * val))
    {
        return false;
    }
	
    var sVal = String(val);
    	
    if(parseInt(val) < 1)
    {
        return false;
    }
	
    return true;
}

function TrimWhiteSpaces(str)
{
    var strRetVal = '';
    var strTemp = trim_string(str);
    for(var i=0;i<strTemp.length;i++)
    {
        if(strTemp.charCodeAt(i) == 32 || strTemp.charCodeAt(i) == 160)
        {
                //do nothing
        }                   
        else
        {
            strRetVal += strTemp.charAt(i);
        }     
    }
    return strRetVal;
}

function trim_string(s)
{
    return s.replace(/^\s*|\s*$/g,"");
}

function ConfirmBox(val)
{
    return confirm(val + ' If yes, click OK. Otherwise click Cancel.');
}

function FocusSSN(item,id,iItemCount)
{
    if(iItemCount == 1 && item.value.length == 3)
    {
        $('SSN2_' + id).focus();
    }
    
    if(iItemCount == 2 && item.value.length == 2)
    {
        $('SSN3_' + id).focus();
    }
    
    
}

function generateSSN(id,SSN1,SSN2,SSN3)
{
    var Table = document.createElement('table'); 
    Table.className = "BeneficiaryTable";
    Table.cellPadding = "1";
    Table.cellSpacing = "0";
    Table.id = 'TableDate_' +  String(id);
    var TableBody = document.createElement("tbody");

    var RowSSN = document.createElement("tr");

    var CellSSN1 = document.createElement("td");
    CellSSN1.className = "";
    var TxtBox = "<input onkeypress=\"FocusSSN(this,'"+ id +"',1);\" class='SSN1' maxlength='3' value=\""+ SSN1 +"\" type=\"text\" name=\"" + 'SSN1_' +  String(id) + "\" id=\"" + 'SSN1_' +  String(id) +"\"/>";    
    CellSSN1.innerHTML = TxtBox; 
    RowSSN.appendChild(CellSSN1);
    
    var CellSSN2 = document.createElement("td");
    CellSSN2.className = "";
    var TxtBox = "<input onkeypress=\"FocusSSN(this,'"+ id +"',2);\" class='SSN2' maxlength='2' value=\""+ SSN2 +"\" type=\"text\" name=\"" + 'SSN2_' +  String(id) + "\" id=\"" + 'SSN2_' +  String(id) +"\"/>";    
    CellSSN2.innerHTML = TxtBox; 
    RowSSN.appendChild(CellSSN2);
    
    var CellSSN3 = document.createElement("td");
    CellSSN3.className = "";
    var TxtBox = "<input class='SSN3' maxlength='4' value=\""+ SSN3 +"\" type=\"text\" name=\"" + 'SSN3_' +  String(id) + "\" id=\"" + 'SSN3_' +  String(id) +"\"/>";    
    CellSSN3.innerHTML = TxtBox; 
    RowSSN.appendChild(CellSSN3);
    
    TableBody.appendChild(RowSSN);
    Table.appendChild(TableBody);
    return Table;
}

var MonthNames = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec");


function generateDateCtrl(id,startyear,endyear,txtDay,txtMonth,txtYear)
{
    if(txtDay == 0 && txtMonth == 0 && txtYear == 0)
        dtSelectedDate = null;
    else        
        dtSelectedDate = new Date(txtMonth + "/" + txtDay + "/" + txtYear);
        
    var Table = document.createElement('table'); 
    Table.className = "BeneficiaryTable";
    Table.cellPadding = "3";
    Table.cellSpacing = "0";
    Table.style.width = "100%";
    Table.id = 'TableDate_' +  String(id);
    var TableBody = document.createElement("tbody");

    var RowDate = document.createElement("tr");

    var CellDay = document.createElement("td");
    CellDay.className = "";



    var DDSel = "<select name=\"" + 'Day_' +  String(id) + "\" id=\"" + 'Day_' +  String(id) +"\">";
    DDSel += "<option value=''>DD</option>";
    for(var i=1;i<=31;i++)
    {
        if(dtSelectedDate != null && dtSelectedDate.getDate() == i)
        {
            DDSel += "<option selected='selected' value='"+ i +"'>"+ i +"</option>";
        }
        else
        {
            DDSel += "<option value='"+ i +"'>"+ i +"</option>";
        }
    }
    DDSel += "</select>";

    CellDay.innerHTML = DDSel; 
    
    
    var CellMonth = document.createElement("td");
    CellMonth.className = "";

    var DDSel = "<select name=\"" + 'Month_' +  String(id) + "\" id=\"" + 'Month_' +  String(id) +"\">";
    DDSel += "<option value=''>MM</option>";
    for(var i=1;i<=12;i++)
    {
        if(dtSelectedDate != null && (dtSelectedDate.getMonth() + 1) == i)
        {
            DDSel += "<option selected='selected' value='"+ i +"'>"+ MonthNames[i-1] +"</option>";
        }
        else
        {
            DDSel += "<option value='"+ i +"'>"+ MonthNames[i-1] +"</option>";
        }
    }
    DDSel += "</select>";

    CellMonth.innerHTML = DDSel; 
    RowDate.appendChild(CellMonth);
    RowDate.appendChild(CellDay);
    
    var CellYear = document.createElement("td");
    CellYear.className = "";

    var DDSel = "<select name=\"" + 'Year_' +  String(id) + "\" id=\"" + 'Year_' +  String(id) +"\">";
    DDSel += "<option value=''>YYYY</option>";
    for(var i=startyear;i<=endyear;i++)
    {
        if(dtSelectedDate != null && dtSelectedDate.getFullYear() == i)
        {
            DDSel += "<option selected='selected' value='"+ i +"'>"+ i +"</option>";
        }
        else
        {
            DDSel += "<option value='"+ i +"'>"+ i +"</option>";
        }
    }
    DDSel += "</select>";

    CellYear.innerHTML = DDSel; 
    RowDate.appendChild(CellYear);
    
    TableBody.appendChild(RowDate);
    Table.appendChild(TableBody);
    return Table;
}

/*Date Validation*/

//used only in date validation, will return true if the value passed is 3.4 or -3 for e.g
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.
    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
}
var dtCh = "/";
minYear = 1930;
maxYear = 2050;
function isDate(dtStr)
{
    //displayName = 'Arun';
	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)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	
	if (pos1==-1 || pos2==-1){
		//alert("The "+ displayName +" format should be : dd/mm/yyyy");
		return false;
	}
	
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day for "+ displayName);
		return false;
	}
	
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month for "+ displayName);
		return false;
	}
	
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear +" for "+ displayName)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid "+ displayName);
		return false;
	}
	
    return true;
}
/*End date Validation*/

 function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
function OnShowHideText(id,k)
{
    if(k)
    {
        document.getElementById(id).style.display = "block";
    }
    else
    {
        document.getElementById(id).style.display = "none";
    }
    return false;
}