﻿
/// Only function which need to be on the public forms should be in this File!

var ie = document.all
var dom = document.getElementById

function $(id){return document.getElementById(id);}      
function Control(controlName){return (dom) ? document.getElementById(controlName) : ie ? document.all(controlName) : document(controlName);}

function DisableFalseSumbit(e)
{            
    if(e) // this will be null in ei sometimes
	{
		var charCode = (e.charCode ? e.charCode : e.keyCode);    
		if(charCode == 13)// && (e.target && e.target.attributes && e.target.attributes.getNamedItem(attrName) && e.target.attributes.getNamedItem(attrName).value != "1"))        
		{        			
			if(GetControlAttribute(e.target, "issubmit") != "1" && GetControlAttribute(e.target, "type") != "submit")
			{        
				e.preventDefault();
				e.stopPropagation();			
			}
			
		}
	}
}

function GetControlAttribute(thisControl, attrName)
{	
	if(thisControl && thisControl.attributes && thisControl.attributes.getNamedItem(attrName)) return thisControl.attributes.getNamedItem(attrName).value;
	else return '';
}

function GetTableRowsByTagNAttr(table, attrName, attrValue)
{	
	var els = table.rows;	
	if(els.length == 0) return null;
	var arr = new Array();	
	for(var i = 0; i < els.length; i++) 
	{		
	    if(els[i].attributes && els[i].attributes.getNamedItem(attrName) && els[i].attributes.getNamedItem(attrName).value == attrValue) arr.push(els[i]);
	}
	return arr;
}

function ShowSisterTextBoxWhenAddNew(ddl, sisterTextboxName, sisterImgName, editDDLImageName) // for onchange on DDL
{    
	if(ddl.value == "++")
    {
        ddl.style.display = "none";                        
        Control(sisterTextboxName).style.display = "";        
        Control(sisterTextboxName).focus();        
        Control(sisterImgName).style.display = "";                        
		return false; // to cancel any server events 
    }    
}

function ShowSisterDDL(img, txtID, sisterDDLName, editDDLImageName, hidOldValueID) // for onclick on img back btn left of txt
{    
    // first make all the visible controls disappear then show the previously unvisible control
    var sisterDDL = Control(sisterDDLName);
    var hidOldValue = Control(hidOldValueID);    
    var txt = Control(txtID);
    txt.style.display = "none";    
    txt.vale = "";
    img.style.display = "none";            
    sisterDDL.style.display = "";       
    sisterDDL.selectedIndex = 0;    
    if(hidOldValue) hidOldValue.value = ""; // makes sure that the value that was enterd when the edit option button was pressed is now gone      
    
}

var PHONE_FORMAT = '555-555-5555';

function TxtPhoneOnfocus(txt)
{
	if(txt.value == PHONE_FORMAT)
	{ 
		txt.value = ''; 
		txt.className = 'TxtPhone';		
	}	
}

function TxtPhoneOnblur(txt)
{
	if(txt.value == '' || txt.value.replace(' ', '') == '')
	{
		txt.value = PHONE_FORMAT; 
		txt.className = 'TxtPhone TxtHint';		
	}
}

function TxtWHintOnfocus(txt, hint)
{
	if(txt.value == hint)
	{ 
		txt.value = ''; 
		txt.className = '';
	}	
}

function TxtWHintOnblur(txt, hint)
{		
	if(txt.value == '' || txt.value.replace(' ', '') == '')
	{				
		txt.value = hint; 
		txt.className = 'TxtHint';
	}
}

function GetElementsByTagNAttr(tagName, attrName, attrValue) 
{
    var els = document.getElementsByTagName(tagName);
	if(els.length == 0) return null;
	var arr = new Array();
	for(var i = 0; i < els.length; i++) 
		if(els[i].attributes && els[i].attributes.getNamedItem(attrName) && els[i].attributes.getNamedItem(attrName).value == attrValue) arr.push(els[i]);	    
	return arr;
}

function OpenCalendar(objClientID, date, objType, isNonFuture)
{
    var winUrl = "/CWA_DB/Controls/Calendar.aspx?Obj=" + objClientID + AddNotNullItem("&Date", date) + AddNotNullItem("&" + objType, "True") + AddNotNullItem("&IsNonFuture", isNonFuture);
    var windowProperties = "status=yes, resizable=yes, scrollbars=yes, titlebar=yes, menubar=no, width=240, height=246";
    window.open(winUrl, "calendar", windowProperties);        
    
}

function AddNotNullItem(querystringName, querystringValue)
{
    if(querystringValue != undefined && querystringValue != null && querystringValue != "")    
        return querystringName + "=" + escape(querystringValue);    
    else
        return "";
}

function BtnSubmitClick(btnSubmit)
{
	btnSubmit.style.display = 'none';
	var parent = btnSubmit.parentNode;	
	var span = document.createElement('span');
	parent.appendChild(span);
	span.id = 'spanSubmitting';
	span.innerHTML = 'Submitting...';
}
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)
			{
				//try{ document.getElementById(fldID + '_Phn' + nextNum).focus(); }catch(ex){}
				try{ document.getElementById(fldID + '_' + tp + nextNum).focus(); }catch(ex){}
			}
			txt.setAttribute('isfull', '1');
		}
		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{ document.getElementById(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 ChngPhnNum(fldID)
{
	var cntrPhnUS = document.getElementById(fldID + '_cntrPhnUS');
	var cntrPhnInt = document.getElementById(fldID + '_cntrPhnInt');	
	var hidPhn = document.getElementById(fldID + '_HidPhn');	
	var lnk = document.getElementById(fldID + '_lnk');	
	
	if(cntrPhnUS.style.display == 'none')
	{
		cntrPhnUS.style.display = '';					
		cntrPhnInt.style.display = 'none';
		hidPhn.value = '0';
		lnk.innerHTML = 'Enter International Number';	
		document.getElementById(fldID + '_Phn0').focus();
	}
	else
	{
		cntrPhnUS.style.display = 'none';					
		cntrPhnInt.style.display = '';
		hidPhn.value = '1';
		lnk.innerHTML = 'Enter US Number';
		document.getElementById(fldID + '_PhnInt').focus();
	}		
}

function GetElementsByTagNAttr(tagName, attrName, attrValue) 
{
    var els = document.getElementsByTagName(tagName);
	if(els.length == 0) return null;
	var arr = new Array();
	for(var i = 0; i < els.length; i++) 
		if(els[i].attributes && els[i].attributes.getNamedItem(attrName) && els[i].attributes.getNamedItem(attrName).value == attrValue) arr.push(els[i]);	    
	return arr;
}

/// Same in other system so updated in both places
function IsEmpty(id, fldTpID)
{
    //alert('IsEmpty - fldTpID = ' + fldTpID);
   
    if(fldTpID == '3') // Radio buttons
    {
        var ctrls = GetElementsByTagNAttr('input', 'name', id);
        
        var isChk = false;
        for(var i = 0; i < ctrls.length;i++)
        {
            if(ctrls[i].checked) isChk = true;
        }
        //alert('ctrls.length = ' + ctrls.length + ' - isChk = ' + isChk);
        return !isChk;
    }
    else
    {
        var ctrl = $(id);
        if(ctrl.offsetWidth == 0) return false; // means that it is hidden
        if(ctrl.value == '') return true;
        else return false;
    }
}

function addEvent( obj, type, fn )
{ 
   if (obj.addEventListener)
   { 
      obj.addEventListener(type, fn, false);
   }
   else if (obj.attachEvent)
   { 
		obj["e"+type+fn] = fn; obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); }; obj.attachEvent( "on"+type, obj[type+fn] ); } 
} 

function PostBack()
{
    document.forms[0].submit();
}

function ControlServerClick(controlID)
{
    var control = $(controlID);    
    control.click();
    return false;
}

function TshirtNumChange(num)
{
    var trTshirt_Hdr = $('trTshirt_Hdr');
    trTshirt_Hdr.style.display = (num > 0 ? '' : 'none');
    
    for(var i = 0; i < num; i++)
    {
        $('trTshirt_' + i).style.display = '';
    }
    
    for(var i = num; i < 20; i++)
    {
        $('trTshirt_' + i).style.display = 'none';
    }
}

