﻿//=====================================================================================================
//  Common client side script.
//=====================================================================================================
function SetTabLocation(p_ctlID)
{
    try	
    {	
        if(document.getElementById(p_ctlID).type.substring(0,6) != "select")   // Ignore focus for dropdown lists.
        {
            document.getElementById(p_ctlID).focus();
            if(document.getElementById(p_ctlID).type == "text") document.getElementById(p_ctlID).select();
        }
    }								
    catch(e) {}					
}

function GetControlIdPrefix(p_ctlTagID)
{
    if(p_ctlTagID != null) return 'ctl00_MainContent_' + p_ctlTagID + '_';
    return 'ctl00_MainContent_';
}
        
function OpenWindow(p_url,p_opt,p_winHandle)
{
	if(p_opt == null)
		p_opt = 'height=350,width=500,scrollbars=yes,status=yes,resizable=yes';
		
	if(p_winHandle == null)
	{
		var randomNumber=Math.floor(Math.random()*100)		// Calculate random number 0-100
		p_winHandle = 'win' + randomNumber;
	}		
		
	var win = window.open(p_url,p_winHandle,p_opt);
	win.focus();	
}

/* --------------------------------------------------------------------
    Validation Routines
----------------------------------------------------------------------- */
function IsEmpty(p_s)
{
    if(p_s.replace(/\s/g,'').length == 0) return true;      // Empty string after removing all whitespaces.
    return false;
}

function IsEmail(p_emailAddr)
{
    var arValidTopLevelExt = new Array(12);                             

    arValidTopLevelExt[0] = "com";                                                                       
    arValidTopLevelExt[1] = "net";                                       
    arValidTopLevelExt[2] = "org";                                      
    arValidTopLevelExt[3] = "museum";                                   
    arValidTopLevelExt[4] = "edu";                                      
    arValidTopLevelExt[5] = "us";                                       
    arValidTopLevelExt[6] = "aero";                                     
    arValidTopLevelExt[7] = "arpa";                                     
    arValidTopLevelExt[8] = "biz";                                      
    arValidTopLevelExt[9] = "coop";                                     
    arValidTopLevelExt[10] = "info";                                    
    arValidTopLevelExt[11] = "int";                                     

    if(p_emailAddr == null) return false;
        
    var s = p_emailAddr.toString().toLowerCase();                         
    if(s.indexOf("..") > 0) return false;                                                     
    var posComma = s.indexOf(",");
    var posSpace = s.indexOf(" ");
    if(posComma >= 0 || posSpace >= 0) return false;    

    var posAt = s.indexOf("@");
    var posAddAt = s.lastIndexOf("@");
    var posFirstDot = s.indexOf(".", posAt);
    var posLastDot = s.lastIndexOf(".") + 1;
    if(posAt <= 0 || posFirstDot <= 0  || posAt != posAddAt || (posFirstDot - posAt) == 1 || posLastDot == 0) 
        return false;
        
    var extLength = s.length-posLastDot; 
    for(i=0; i < arValidTopLevelExt.length; i++)                        
    {                                                                                    
        if(s.substr(posLastDot,extLength) == arValidTopLevelExt[i]) return true;                                                   
    }                                                                   
    return false;
}

function IsPhone(p_s)			
{
    if (p_s == null) return false;

	var matchArray = p_s.match(/^(\d{3})(\.)(\d{3})(\.)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: 999.999.9999?
		
	matchArray = p_s.match(/^(\d{3})(\.)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: 999.9999?

	matchArray = p_s.match(/^(\()(\d{3})(\))(\ )(\d{3})(\.)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: (999) 999.9999?

	matchArray = p_s.match(/^(\()(\d{3})(\))(\d{3})(\.)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: (999)999.9999?
		
	matchArray = p_s.match(/^(\d{3})(\-)(\d{3})(\-)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: 999-999-9999?
		
	matchArray = p_s.match(/^(\d{3})(\-)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: 999-9999?

	matchArray = p_s.match(/^(\()(\d{3})(\))(\ )(\d{3})(\-)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: (999) 999-9999?

	matchArray = p_s.match(/^(\()(\d{3})(\))(\d{3})(\-)(\d{4})$/); 
	if(matchArray != null) return true;					//Formated phone: (999)999-9999?
		
	matchArray = p_s.match(/^(\d{10})$/);		
	if(matchArray != null) return true;					//Unformated 10 digit phone with area code?
		
	matchArray = p_s.match(/^(\d{7})$/);		
	if(matchArray != null) return true;					//Unformated 7 digit phone without area code?
	return false;
}
