﻿// JScript File


function ShowAllCountries(controlID, selectedVal)
{	
	// the id passed in is of the lnk so we have to remove the last three chars
	controlID = controlID.substring(0, controlID.length-2);		
			
	var pageUrl = '/SAM/Cmn/AJAX/GetCnt.ashx?id=' + controlID;
	
	var tabidx = $(controlID).getAttribute('tabindex');
	pageUrl += '&tabidx=' + tabidx;
		
	if(IsDefined(selectedVal)) pageUrl += '&val=' + selectedVal;
	
	// this will set the whole area (table) as the ddl now
	AJAXShowArea(pageUrl, (controlID+'cntr'));
	
	//link.parentNode.style.display = 'none';
	//try{$(controlID).focus();}catch(ex){}
}

function ToggleStRgn(id)
{	
	// the id passed in is of the lnk so we have to remove the last three chars
	id = id.substring(0, id.length-2);		
	
	var hidIsSt = $(id + '_hidIsSt');
		
	if(hidIsSt.value == '1')
	{
		hidIsSt.value = '0';	
		$(id + '_st').style.display = 'none';
		$(id + '_a').innerHTML = 'Select US-State';
		
		var txt = $(id);
		txt.style.display = '';			
		txt.focus();		
	}
	else
	{
		hidIsSt.value = '1';		
		$(id).style.display = 'none';			
		$(id + '_a').innerHTML = 'Enter Region';
		
		var lst = $(id + '_st');
		lst.style.display = '';		
		lst.focus();		
	}					
}

function ChngPhnNum(fldID)
{
	var phnCntr = $(fldID);
	var phnInt = $(fldID + '_PhnInt');	
	var hidPhn = $(fldID + '_HidPhn');	
	var lnk = $(fldID + '_lnk');	
	
	if(phnCntr.style.display == 'none')
	{
		phnCntr.style.display = '';					
		phnInt.style.display = 'none';
		hidPhn.value = '0';
		lnk.innerHTML = 'Enter International Number';	
		$(fldID + '_Phn0').focus();
	}
	else
	{
		phnCntr.style.display = 'none';					
		phnInt.style.display = '';
		hidPhn.value = '1';
		lnk.innerHTML = 'Enter US Number';
		$(fldID + '_PhnInt').focus();
	}		
}

function PhnKeyPress(txt, fldID, len, nextNum, e)
{
	CtrlKeyPress('Phn', txt, fldID, len, nextNum, e);
}
function SSNKeyPress(txt, fldID, len, nextNum, e)
{
	CtrlKeyPress('', txt, fldID, len, nextNum, e);
}
function CtrlKeyPress(tp, txt, fldID, len, nextNum, e)
{
	if(e == null)e = window.event;
	var keyCode = (e.charCode ? e.charCode : e.keyCode);    		
	
	// the first check is for the 0-9 and a-z and the second one is for the numpad
	if((keyCode >= 48 && keyCode <=  90) || (keyCode >= 96 && keyCode <= 105) )
	{
		var val = txt.value;
		var isFull = txt.getAttribute('isfull');
		if(isFull == null) isFull = '0';
		
		if(val.length == len && isFull == '0') 
		{
			if(nextNum != null)
			{
				/// in repeaters we can't just use the FldID, we have to get the actual ID so use the txt.id to get the base id
				
			
				//try{ $(fldID + '_Phn' + nextNum).focus(); }catch(ex){}
				try{ $(fldID + '_' + tp + nextNum).focus(); }catch(ex){}
			}
			else
			{
				txt.setAttribute('isfull', '1');
				// TO DO: Setup so that it puts the focus on the next control by using the tabindex				
			}
			
		}
		else if(val.length != len && isFull == '1') // reset the isfull atr if the txt is not full anymore
			txt.setAttribute('isfull', '0');
	}
	// when pressing the left or right button or backspace need to check with position is at, if left key or backspace and at the end then will go to the last
	else if(keyCode == 8 && nextNum != 1) // backspace (do nothing when on first input)
	{
		var val = txt.value;
		if(val == '')
		{			
			try{ $(fldID + '_' + tp + (nextNum-2)).focus(); }catch(ex){}
		}
		
	}
//	else if(keyCode == 37 && nexNum != 1) // left arrow will go to the previous input if there are no 
//	{
//		var val = txt.value;
//		if(val == ''
//	}
}



function SetHidVal(control)
{
	$('hid' + control.id).value = control.value;	
}

var dateFormat = 'mm/dd/yyyy';
function DtboxOnfocus(control)
{
	if(control.value == dateFormat)
	{
		control.value = '';
		control.className = 'txtDt';
	}		
}

function DtboxOnblur(control)
{
	var val = control.value.replace(' ', '');
	if(val == '')
	{
		control.value = dateFormat;
		control.className = 'txtDt inptHint';
	}
	else
	{		
		var validformat = /^\d{1,2}\/\d{1,2}$/; 
		var dashesFormatNoYear = /^\d{1,2}\-\d{1,2}$/;
		var dashesFormatWithTwoDigitYear = /^\d{1,2}\-\d{1,2}\-\d{2}$/; 
		var dashesFormatWithFourDigitYear = /^\d{1,2}\-\d{1,2}\-\d{4}$/; 
		var twoDigitYearFormat = /^\d{1,2}\/\d{1,2}\/\d{1,2}$/; 
		
		if (validformat.test(val))
		{
		    //alert('matches regex');
			var currentDate = new Date();
			var currentYear = currentDate.getFullYear();
			control.value = val + "/" + currentYear;
		}
		else if(dashesFormatNoYear.test(val))
		{
		    val = val.replace("-", "/");
		    var dt = new Date();			
		    val = val + "/" + dt.getFullYear();
		    control.value = val;
		}
		else if(dashesFormatWithTwoDigitYear.test(val))
		{
		    val = val.replace("-", "/");
		    var dt = new Date();			
		    
		    var year = val.substr(val.lastIndexOf("/")+1);	        
		    
		    /// If the year is less than 20 years into the future then we assume that it is for this century	            
            if(year.charAt(0) == '0' || Number(year) < Number(dt.getFullYear().toString().substr(2,2)) + 20)
                year = "20" + year;
            else
                year = "19" + year
		    
		    control.value = val.substr(0, val.lastIndexOf("/") + 1) + year;
		}
		else if(dashesFormatWithFourDigitYear.test(val))
		{
		
		    val = val.replace("-", "/").replace("-", "/");;
		    
		    control.value = val;
		    
		}
		else if(twoDigitYearFormat.test(val))
	    {
	        var year = val.substr(val.lastIndexOf("/")+1);	        
	        if(year.length == 1) // only entered one digit
	        {
	            year = "200" + year;	            
	        }
	        else // when two digits for year
	        {
	            var dt = new Date();
	            // If the year is less than 20 years into the future then we assume that it is for this century	            
	            if(year.charAt(0) == '0' || Number(year) < Number(dt.getFullYear().toString().substr(2,2)) + 20)
	                year = "20" + year;
	            else
	                year = "19" + year
	        }
	        control.value = val.substr(0, val.lastIndexOf("/") + 1) + year;
	        
	    }
		else if(val.length == 8 && !isNaN(val)) // when entered with no forward slashes
		{			
			control.value = val.substr(0, 2) + "/" + val.substr(2, 2) + "/" + val.substr(4, 4);
		}
		else
		{
			//var dt = new Date(Date.parse(val));
			var dt = new Date();
			dt.setTime(Date.parse(val));
			//alert(dt);
			if(dt != 'NaN' && dt != 'Invalid Date')
				control.value =  dt.getMonth() + 1 + "/" + dt.getDate() + "/" + dt.getFullYear();
		}
			
	}	
		
}
function SetDate(date)
{	
	calIframeCntr.style.display = 'none';	
	dtTxt.value = date;
	dtTxt.className = 'txtDt';
}
/// =============== Calendar Frame =============
var isCalIframe = false;
var calIframeID = "ifrmCal";
var calIframeCntr;
var dtTxt;

// have to pass in the calID because of how rptrs are setup so can't just pass in the ctrlID
function OpenCal(calID)
{
	var controlID = calID.replace('_Cal', '');
	dtTxt = $(controlID);
	var dt = dtTxt.value;
	if(dt == dateFormat) dt = '';
	
	if(!isCalIframe) CreateCalFrame();			
	else 
	{ 
		calIframeCntr.style.display = '';				
	}	
	
	var calIframe = $(calIframeID);
	calIframe.src = '/SAM/Ctrl/Calendar.aspx?dt=' + dt;		
}

function CreateCalFrame()
{
	calIframeCntr = document.createElement("div");
	document.body.appendChild(calIframeCntr);
		
	calIframeCntr.className = "dvCal";	
	calIframeCntr.id = "calFrame";
	isCalIframe = true;

	// when opened from the Edit Fld popup the cal is cutof so the opener needs to be increased in width
	if(location.href.toLowerCase().indexOf('edtfldval.aspx'))
	{
		calIframeCntr.style.width = '80%';
		calIframeCntr.style.height = '80%';	
		calIframeCntr.style.left = '10%';
		calIframeCntr.style.top = '10%';		
	}

	CreateMyIFrame(calIframeCntr, calIframeID, '');		
}





// for the EdtFldVal.aspx page because we need to have the lst text for updating the opener
function SaveLstText(control)
{
	var hidLstText = $('hidLstText');
	if(hidLstText != null) hidLstText.value = control.options[control.selectedIndex].text;
}
function OverrideSavedText(control) // especially for the Regionbox control to override the lst value
{
	var hidLstText = $('hidLstText');
	if(hidLstText != null) hidLstText.value = control.value;
}
// For EdtFldVal.aspx to return to the opener, for oneToMany fld tp, need to loop through all the ddls and put their text separated by a br tag in the hidLstText 
function SaveOneToManyLstText()
{
	var fldOneToMany = GetControlValue('hidFldOneToMany');	
	if(fldOneToMany && fldOneToMany != '') // have to check for this because this is also called from an main edit page
	{
		var arr = fldOneToMany.split(',');
		var lstText = '';
		
		for(var i = 0; i < arr.length; i++)
		{		
			var ddl = $(arr[i]);			
			var val = ddl.options[ddl.selectedIndex].text;
			if(val != '' && lstText != '') lstText += '<br/>';
			lstText += val;
		}		
		var hidLstText = $('hidLstText');
		if(hidLstText != null) hidLstText.value = lstText;
	}
}
function SaveChkLstText()
{
    var fldOneToMany = GetControlValue('hidFldOneToMany');	
	if(fldOneToMany && fldOneToMany != '') // have to check for this because this is also called from an main edit page
	{
		var arr = fldOneToMany.split(',');
		var lstText = '';		
		for(var i = 0; i < arr.length; i++)
		{		
		    var chk = $(arr[i]);
		    if(chk.checked)
		    {
			    var val = $(arr[i] + '_l').innerHTML;						
			    if(val != '' && lstText != '') lstText += '<br/>';
			    lstText += val;
		    }
		}		
		//alert(lstText);
		var hidLstText = $('hidLstText');
		if(hidLstText != null) hidLstText.value = lstText;
	}
}

function OverrideNameFromID(tagName, fldTpID)
{
	var arr = GetElementsByTagNAttr(tagName, 'tp', fldTpID);
	for(var i = 0; i < arr.length; i++)
	{
		arr.name = arr.id;
	}
}

function SaveSelectText(control, hidControlID)
{
	var hid = $(hidControlID);
	if(hid == null) hid = GetControlByMyID('input', 'hidDocTp');
	if(hid != null) hid.value = control.options[control.selectedIndex].text;
}

// SaveDocType is neeed because of the quirkiness with editing an existing record (have to make sure to save the doc type from the doc type ddl even when the user does not change it)
function SaveDocType(ctrlID)
{
	var ctrlDocType = GetElementsByTagNAttr('select', 'onchange', 'SaveSelectText(this,\'hidDocTp\');')[0];
	if(ctrlDocType != null)
	{
		var hidDocTp = $('hidDocTp');
		if(hidDocTp != null) hidDocTp.value = ctrlDocType.options[ctrlDocType.selectedIndex].text;		
	}
}

// for when the document has already been uploaded and the user wants to re-upload a new one
function ShowUploadControl(id)
{	
	var cntr = $(id + '_cntr');
	var lnkCntr = $(id + '_lnkCntr');
	
	lnkCntr.style.display = 'none';
	cntr.style.display = 'block';
	
	SaveDocType(ctrlID);
}


// (like the doc-upload control) for when the password has already been uploaded and the user wants to re-upload a new one
function ShowPasControl(id)
{				
	var trConfirm = $(id + '_TrConfirm');
	var trHint = $(id + '_TrHint');
	var cntr = $(id + '_cntr');
	var lnkCntr = $(id + '_lnkCntr');
	var ctrlPas = $(id);
	
	trConfirm.style.display = '';		
	trHint.style.display = '';		
	lnkCntr.style.display = 'none';
	cntr.style.display = '';
	ctrlPas.focus();
}

function ForgotPass()
{
	var qs = GetQueryString();
	var frmUrl = UrlEncode(window.location);
	window.location = '/SAM/Misc/Ent_Pas_Rst_Eml_Send_Edt.aspx?' + qs + '&frmurl=' + frmUrl;
}

function PayCtrlOnchange(ddl)
{
    var option = ddl.options[ddl.selectedIndex];
	var msg = option.getAttribute('msg');	
	var payTpID = option.getAttribute('payTpID');	
    var trGCheckout	= GetControlByMyID('tr', 'trGCheckout'); /// this might not be there
    var tblCC = GetControlByMyID('table', 'tblCC');
    var trPayCtrlMsg = GetControlByMyID('tr', 'trPayCtrlMsg');
    var tdPayCtrlMsg = GetControlByMyID('td', 'tdPayCtrlMsg');
    
    if(payTpID == '1') // CC
    {
        if(trGCheckout) HideControl(trGCheckout.id); 
        ShowControl(tblCC.id);
    }
    else if(payTpID == '2') // GCheckout
    {
        if(trGCheckout) ShowControl(trGCheckout.id); 
        HideControl(tblCC.id);
    }
    else
    {
        /// When other type of pay method then we hide both (like for check)
        if(trGCheckout) HideControl(trGCheckout.id); 
        HideControl(tblCC.id);
    }
	
	/// This will show the special Msg setup for this option
    if(msg && msg != '')
    {        
        tdPayCtrlMsg.innerHTML = msg;
        ShowControl(trPayCtrlMsg.id);
    }	
    else
    {
        HideControl(trPayCtrlMsg.id);
        tdPayCtrlMsg.innerHTML = '&nbsp;';
    }
	
}