﻿// JScript File

function ApplyMaxLength(element,maxchars) {
  if (element.value.length >= maxchars) { element.value = element.value.substring(0,maxchars); }
}

function MakeSizeAdjustment(thisTextArea)
{
     while (thisTextArea.scrollHeight > thisTextArea.offsetHeight)
     {
          thisTextArea.rows++;
     }
}
function SetTextArea(thisTextArea,maxchars)
{
    ApplyMaxLength(thisTextArea,maxchars);
    MakeSizeAdjustment(thisTextArea);
}

function MoveNext() 
   {
    var popup = $find( "bidModalPopup" ); 
     if (popup) {

          if ( Page_ClientValidate("popup") ) { 
               // Data is valid, you can close the popup.
               popup.hide();
          }
     }
     rbl = document.getElementById('ctl00_cphMain_confirmoptions')
     rbl0 = document.getElementById('ctl00_cphMain_confirmoptions_0')
     rbl1 = document.getElementById('ctl00_cphMain_confirmoptions_1')
     rbl2 = document.getElementById('ctl00_cphMain_confirmoptions_2')
     if (rbl0.checked) 
     {
        rbl0.checked = false;
     }
     if (rbl1.checked) 
     {
        rbl1.checked=false;
        var tabBehavior = $get('ctl00_cphMain_tcTabs').control; 
        NextTab = document.getElementById('ctl00_cphMain_NextTab')
        tabBehavior.set_activeTabIndex(parseInt(NextTab.value)); 
       
     }
     if (rbl2.checked)
     {
       NextPage = document.getElementById('ctl00_cphMain_NextPage')
       rbl2.checked=false;
       window.location = NextPage.value;
     }
    }
    
function FirstTab()
{
    var tabBehavior = $get('ctl00_cphMain_tcTabs').control; 
    tabBehavior.set_activeTabIndex(0); 
}
    
//1230;12:30;2430;24:30
function Validate_Time (txtTime)
{
//strTime = txtTime.value.replace(/|[b-o]|[q-z]|[B-O]|[Q-Z]|\./g, "")
strTime = txtTime.value.replace(/ |[b-o]|[q-z]|[B-O]|[Q-Z]|\./g, "")
strMessage = ''
strOutTime = ''
strHours = ''
strMinutes = ''
blnPM = false

    if (strTime.length == 0) {
    txtTime.value = ""
    return true
    }
    
    if(/[pP]/.test(strTime)){
        blnPM = true
    }
        strTime = strTime.replace(/[aApP]/, "")
        //Military w/:
        if(/\:/.test(strTime)){
            aryParts = strTime.split(/\:/)
            strHours = parseInt(aryParts[0], 10)
            
            if (blnPM == true && strHours < 12  && strHours > 0 ) 
{strHours = strHours + 12}
            if (blnPM == false && strHours == 12) {strHours=0}
            
            strMinutes = parseInt(aryParts[1],10)
            if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 0 || 
strHours > 24 || strMinutes < 0 || strMinutes > 59){
                strMessage = 'Invalid time format'
            }
            
        }
        else {
        //military w/out :
            if (strTime.length==3){
                strMinutes = parseInt(strTime.substr(1,2),10)
                strHours = parseInt(strTime.substr(0,1),10)
                
                if (blnPM == true && strHours < 12 && strHours > 0 ) 
{strHours = strHours + 12}
                if (blnPM == false && strHours == 12) {strHours=0}
                
                if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 
0 || strHours > 24 || strMinutes < 0 || strMinutes > 59){
                    strMessage = 'Invalid time format'
                }
            }
            else if (strTime.length==4){
                strMinutes = parseInt(strTime.substr(2,2),10) 
//caution:  parseint needs to be told ,10 (base ten) or 08 and 09 fail.
                strHours = parseInt(strTime.substr(0,2),10)
                //alert(parseInt(strTime.substr(0,2),10))
                
                
                if (blnPM == true && strHours < 12  && strHours > 0 ) 
{strHours = strHours + 12}
                if (blnPM == false && strHours == 12) {strHours=0}
                
                if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 
0 || strHours > 24 || strMinutes < 0 || strMinutes > 59){
                    strMessage = 'Invalid time format'
                }
            }
            else{
                strMessage = 'Invalid time format'
            }
        
    }
    
    if (strMessage.length > 0){
        alert(strMessage)
        txtTime.selected = true
        txtTime.focus
        return false
    }
    strHours = strHours.toString()
    if (strHours.length==1) {strHours = '0' + strHours }
    strMinutes = strMinutes.toString()
    if (strMinutes.length==1) {strMinutes = '0' + strMinutes }
    txtTime.value = strHours + ':' + strMinutes
}

//1230;12:30;2430;24:30
function Validate_Time_WithSecs (txtTime)
{
//strTime = txtTime.value.replace(/|[b-o]|[q-z]|[B-O]|[Q-Z]|\./g, "")
strTime = txtTime.value.replace(/ |[b-o]|[q-z]|[B-O]|[Q-Z]|\./g, "")
strMessage = ''
strOutTime = ''
strHours = ''
strMinutes = ''
strSeconds = ''
blnPM = false

    if (strTime.length == 0) {
    txtTime.value = ""
    return true
    }
    
    if(/[pP]/.test(strTime)){
        blnPM = true
    }
        strTime = strTime.replace(/[aApP]/, "")
        //Military w/:
        if(/\:/.test(strTime)){
            aryParts = strTime.split(/\:/)
            strHours = parseInt(aryParts[0], 10)
            
            if (blnPM == true && strHours < 12  && strHours > 0 ) 
{strHours = strHours + 12}
            if (blnPM == false && strHours == 12) {strHours=0}
            
            strMinutes = parseInt(aryParts[1],10)
            if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 0 || 
strHours > 24 || strMinutes < 0 || strMinutes > 59){
                strMessage = 'Invalid time format'
            }
            
            strSeconds = parseInt(aryParts[2],10)
            if(!isNaN(strSeconds))
            {
                if(strSeconds < 0 || strSeconds > 59)
                {
                    strMessage = 'Invalid time format'
                }
                strSeconds = aryParts[2]
            }
            else
            {
                strSeconds = "00"
            }
        }
        else {
        //military w/out :
            if (strTime.length==3){
                strMinutes = parseInt(strTime.substr(1,2),10)
                strHours = parseInt(strTime.substr(0,1),10)
                
                if (blnPM == true && strHours < 12 && strHours > 0 ) 
{strHours = strHours + 12}
                if (blnPM == false && strHours == 12) {strHours=0}
                
                if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 
0 || strHours > 24 || strMinutes < 0 || strMinutes > 59){
                    strMessage = 'Invalid time format'
                }
            }
            else if (strTime.length==4){
                strMinutes = parseInt(strTime.substr(2,2),10) 
//caution:  parseint needs to be told ,10 (base ten) or 08 and 09 fail.
                strHours = parseInt(strTime.substr(0,2),10)
                //alert(parseInt(strTime.substr(0,2),10))
                
                
                if (blnPM == true && strHours < 12  && strHours > 0 ) 
{strHours = strHours + 12}
                if (blnPM == false && strHours == 12) {strHours=0}
                
                if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 
0 || strHours > 24 || strMinutes < 0 || strMinutes > 59){
                    strMessage = 'Invalid time format'
                }
            }
            else if (strTime.length ==5 || strTime.length ==6)
            {
                strMinutes = parseInt(strTime.substr(2,2),10) 
//caution:  parseint needs to be told ,10 (base ten) or 08 and 09 fail.
                strHours = parseInt(strTime.substr(0,2),10)
                //alert(parseInt(strTime.substr(0,2),10))
                
                
                if (blnPM == true && strHours < 12  && strHours > 0 ) 
{strHours = strHours + 12}
                if (blnPM == false && strHours == 12) {strHours=0}
                if(strTime.length == 5)
                {
                    strSeconds =parseInt(strTime.substr(5,1),10)
                }
                else
                {
                    strSeconds =parseInt(strTime.substr(5,2),10)
                }
                
                if (isNaN(strMinutes) || isNaN(strHours) || strHours  < 
0 || strHours > 24 || strMinutes < 0 || strMinutes > 59){
                    strMessage = 'Invalid time format'
                }
                
                if(!isNaN(strSeconds))
                {
                    if(strSeconds < 0 || strSeconds > 59)
                    {
                        strMessage = 'Invalid time format'
                    }
                }
            }
            else{
                strMessage = 'Invalid time format'
            }
        
    }
    
    if (strMessage.length > 0){
        alert(strMessage)
        txtTime.selected = true
        txtTime.focus
        return false
    }
    strHours = strHours.toString()
    if (strHours.length==1) {strHours = '0' + strHours }
    strMinutes = strMinutes.toString()
    if (strMinutes.length==1) {strMinutes = '0' + strMinutes }
    strSeconds = strSeconds.toString()
    if(strSeconds.length < 2)
    {
        strSeconds = strSeconds + '0';
    }
    txtTime.value = strHours + ':' + strMinutes + ':' + strSeconds;
}


function formatSSN(fieldname)
{
     var theCount = 0;
     var theString = fieldname.value;
     var newString = "";
     var myString = theString;
     var theLen = myString.length;
     for ( var i = 0 ; i < theLen ; i++ )
     {
     // Character codes for ints 1 - 9 are 48 - 57
          if ( (myString.charCodeAt(i) >= 48 ) && (myString.charCodeAt(i) <= 57) )
          newString = newString + myString.charAt(i);   
     }
// Now the validation to determine that the remaining string is 9 characters.
     if (newString.length == 9 )
     {
// Now the string has been stripped of other chars it can be reformatted to ###-##-#### 
          var newLen = newString.length;
          var newSSN = "";
          for ( var i = 0 ; i < newLen ; i++ )
          {
               if ( ( i == 2 ) || ( i == 4 ) )
               {
                    newSSN = newSSN + newString.charAt(i) + "-";
               }else{
                    newSSN = newSSN + newString.charAt(i);
               }
          }
          fieldname.value = newSSN;
          return true;
     }else{
          if (theLen > 0)
          {
              alert("The Social Security Number you entered "+newString+" does not contain the correct number of digits");
              fieldname.focus();
              return false;
          }else{
            return true;
          }
     }
}

function formatPhone(field,validationgroup){
	var start;
	var regexp1 = /\(/g;
	var regexp2 = /\)/g;
	var regexp3 = /-/g;
	var regexp4 = /\./g;
	var regexp5 = /\s/g;
	var newstring;
	var areacode;
	var prefix;
	var suffix;
	start = field.value;
	newstring = start.replace(regexp1,'');
	newstring = newstring.replace(regexp2,'');
	newstring = newstring.replace(regexp3,'');
	newstring = newstring.replace(regexp4,'');
	newstring = newstring.replace(regexp5,'');
	if ((newstring.length) == 10) { 
        areacode = newstring.substr(0,3)
		prefix = newstring.substr(3,3)
		suffix = newstring.substr(6,4)
		field.value = '' + areacode + '-' + prefix + '-' + suffix;
		if(validationgroup != "")
		{
		    Page_ClientValidate(validationgroup)
		}
		return true;
	} else {
		return false;
	}
}

function formatDate(field,validationgroup){
	var start;
	//var regexp1 = /\(/g;
	//var regx1 = /[0-9]*;
	start = field.value;
	/*start = field.value;
	newstring = start.replace(/^\s+|\s+$/g,"");
	newstring = newstring.replace(/\s\s/g,' ');
	newstring = newstring.replace(/\s/g,'/');
	*/
	//var regx2 = /[0-9]*/; ///\d{1,3}.\d{1,3}\.\d{1,4};
	//var cur = regx2.exec(start);
	//cur[0] cur[1] cur[2]
	//month = newstring.search(
	//newstring = dateFormat(field.value,"m/dd/yyyy")
	var regxT = /[^0-9]+/g;
	start = start.replace(regxT,"/");
	
	//field.value=cur[0];//"test2";
	//field.value="h2m";
	/*var string;
	if (cur.length > 0) {
		for (var i = 0; i < cur.length; i++) {
			string = string + cur[i] + " - ";
		}
	}*/
	//alert(string);
	field.value = start
    if(validationgroup != "")
		{
		    Page_ClientValidate(validationgroup)
		}
	return true;
	
}

function age(bDay){//getDate, getMonth, getFullYear
 var now = new Date();
 var born = new Date(bDay.value);
 
 var years = now.getFullYear() - born.getFullYear();
 if(born.getMonth() > now.getMonth() || (born.getMonth() == now.getMonth() && born.getDate() > now.getDate()))
 {
    years = years - 1;
 }
 
 //var years = Math.floor((now.getTime() - born.getTime()) / (365 * 24 * 60 * 60 * 1000));
 return years;
}

function SetDayOfWeek(datefield,ddlname)
{
    var myDays=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    var strValue = datefield.value;
    if(isDate(strValue))
    {
        var actualDate = new Date(strValue);
        var index = actualDate.getDay();
        var strSetValue = myDays[index];
        var ddl = document.getElementById(ddlname);
        if (ddl != null){
          ddl.selectedvalue = strSetValue;
          ddl.selectedIndex = index+1;
        }
    }
}

function SetControl(field, controlName)
{
     var strValue = field.value;
     var ctl = document.getElementById(controlName);
     if (ctl.value == "" || ctl.value == "...") {
       ctl.value = strValue;
     }
}

function SetLabel(field, controlName)
{
     var strValue = field.value;
     var ctl = document.getElementById(controlName);
     ctl.innerHTML = strValue;
}



function SetDDLControl(field, controlName)
{
     var strValue = field.value;
     var srcIndex = field.sourceIndex;
     var ddl = document.getElementById(controlName);
     if (ddl != null) {
        if (ddl.value == -1){
          ddl.value = field.value;
       }
     }
}

function isDate(sDate) {
var scratch = new Date(sDate);
if (scratch.toString() == "NaN" || scratch.toString() == "Invalid Date") {
alert("Not a Date");
return false;
} else {
return true;

}
}
