﻿var badPasswordArray = new Array("password") ;


//***********************************************************************
//  Rad Windows Actions
//***********************************************************************
function GetRadWindow()
{
	var oWindow = null;
	if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
	else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;//IE (and Moz az well)				
	return oWindow;
}

function CloseOnReload()
{
	GetRadWindow().Close();				
}

function CloseAndRebind(arg)  
{  
    GetRadWindow().Close();
    GetRadWindow().BrowserWindow.RefreshGrid(arg);   
} 

function CloseAndMessage(arg)  
{  
    GetRadWindow().Close();
    GetRadWindow().BrowserWindow.showSuccess(arg);   
} 


function CloseAndAlert(arg)
{
    GetRadWindow().Close();
    GetRadWindow().BrowserWindow.showAlert(arg);   
}

function CancelEdit()  
{  
    GetRadWindow().Close();       
}  

function RefreshParentPage()
{
	GetRadWindow().BrowserWindow.location.reload();
}

function RedirectParentPage(newUrl)
{
	GetRadWindow().BrowserWindow.document.location.href = newUrl;
}

function CallFunctionOnParentPage(fnName)
{
	var oWindow = GetRadWindow();
	if (oWindow.BrowserWindow[fnName] && typeof(oWindow.BrowserWindow[fnName]) == "function")
	{
		oWindow.BrowserWindow[fnName](oWindow);
	}
}

function showAlert(InfoString)
{
    radalert(InfoString,500, 200);
}

function showSuccess(InfoString)
{
    radprompt(InfoString,'',500,200);
}


function showSuccessAndRedirectParentWindow(InfoString,RedirectUrl)
{
   window.location.href =  RedirectUrl; 
//    var callBackFn = function(arg)
//        {
//            if (arg)
//            {
//                //alert(RedirectUrl);
//                GetRadWindow().BrowserWindow.document.location.href =  RedirectUrl; 
//                //window.location.href =  RedirectUrl;
//            }
//        }
//     //radprompt(InfoString,callBackFn,500,200);
}


//***********************************************************************
//  Rad Progess Bar
//***********************************************************************
function CheckUploadedFilesSize(progressArea, args)
{
    //progressArea.Confirmed is a custom variable
    if (!progressArea.Confirmed && args.ProgressData.RadUpload.RequestSize > 800000)
    {
        alert("The size of the selected files is more than the limit."); 
        progressArea.CancelRequest();
    }
    else
    {
        progressArea.Confirmed = "confirmed";
    }
}


function BeforeImageFileUploadNoCaptionHandler(uploadInput)
{

    // for mac/linux, else assume windows
    if (navigator.appVersion.indexOf('Mac') != -1 || navigator.appVersion.indexOf('Linux') != -1)
        var fileSplit = '/';
    else
        var fileSplit = '\\';

    var fileTypes     = new Array('.gif', '.jpg','.jpeg','.GIF','.JPG','JPEG'); // valid filetypes
    var fileName      = document.getElementById(uploadInput).value; // current value
    if (fileName==null || fileName=='')
    {
        //alert("Invalid file type!");
        return true;
    }
    
    var extension     = fileName.substr(fileName.lastIndexOf('.'), fileName.length);
    var valid = 0;
   
    for(var i in fileTypes)
    {
        if(fileTypes[i] == extension)
        {
            valid = 1;
            break;  
        }
   
    }
   
    if(valid == 1)
        return true;
    else
    {
        alert("Invalid file type!");
        return false;
    }
}

function BeforeDocFileUploadNoCaptionHandler(uploadInput)
{

    // for mac/linux, else assume windows
    if (navigator.appVersion.indexOf('Mac') != -1 || navigator.appVersion.indexOf('Linux') != -1)
        var fileSplit = '/';
    else
        var fileSplit = '\\';

    var fileTypes     = new Array('.doc', '.txt','.pdf','.DOC','.TXT','PDF'); // valid filetypes
    var fileName      = document.getElementById(uploadInput).value; // current value
    if (fileName==null || fileName=='')
    {
        //alert("Invalid file type!");
        return true;
    }
    
    var extension     = fileName.substr(fileName.lastIndexOf('.'), fileName.length);
    var valid = 0;
   
    for(var i in fileTypes)
    {
        if(fileTypes[i] == extension)
        {
            valid = 1;
            break;  
        }
   
    }
   
    if(valid == 1)
        return true;
    else
    {
        alert("Invalid file type!");
        return false;
    }
}



//***********************************************************************
//  Rad Windows Actions
//***********************************************************************

function retrievePosition(e){
    if (e!= null){
	    setTimeout(function(){e.focus();e.select();} ,1);
	}
}

function retrievePositionById(id) {
    if (id != null) {
        if (document.getElementById(id) != null) {
            setTimeout(function() { document.getElementById(id).focus(); document.getElementById(id).select(); }, 1);
        }
        
    }
	
}


function CheckLength(TargetObject, MaxLenth)
{
    LenString = TargetObject.value.length;
    if (LenString > MaxLenth){
        TargetObject.value = TargetObject.value.substring(0,MaxLenth);
    }
}
    
function CountDownLength(TargetObject, MaxLenth, DisplayObject)
{
    LenString = TargetObject.value.length;
    if (LenString > MaxLenth){
        TargetObject.value = TargetObject.value.substring(0,MaxLenth);
    }
    //Display how many characters remaining
    LenString = TargetObject.value.length;
    if ((MaxLenth - LenString) > 0 )
    {
		document.getElementById(DisplayObject).innerHTML = eval(MaxLenth - LenString) + " Character(s) Remaining"
    } else {
		document.getElementById(DisplayObject).innerHTML = "";
    }
}    

function checkImageFileType(inputName)
{
	var upload= document.getElementById(inputName)
	var fullName = upload.value;
	var shortName = fullName.match(/[^\/\\]+$/);
	var splitName = fullName.split(".");
	var fileType = splitName[1];
	fileType = fileType.toLowerCase();
	if (fileType == 'jpg' || fileType == 'jpeg' || fileType=='gif')
	{
		return true;
	} else {
		alert("Please select a JPEG or Gif file no larger than 150kb!");
		return false;
	}
}       	

function DateSelectOnChange(sDay,sMonth,sYear,sFocused,sPrevious)
{
    var ddlDay = document.getElementById(sDay);
    var ddlMonth = document.getElementById(sMonth);
    var ddlYear = document.getElementById(sYear);
    var ddlSelected = document.getElementById(sFocused);  
    var dateToValidate = ddlMonth.value + "/" + ddlDay.value+ "/" + ddlYear.value;    
    if (!validateDate(dateToValidate))
    {
        alert("Invalid date");
        ddlSelected.value =  document.getElementById(sPrevious).value;
        return false;
    }    
    else
    {
        document.getElementById(sPrevious).value = ddlSelected.value;
    }
    return true;
}

function validateDate(dataValue) 
{
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if (dataValue.match(RegExPattern)) {
        return true; 
    } else {
       return false;
    } 
}


function checkInteger(e){ 
	var num=e.value; 
	if (num.length==0) {
	} else {
		re=/^(([1-9]\d*)|(0))$/; 
		if(re.test(num)) 
		{ 
		} 
		else 
		{ 
			alert("This field must be numeric with no leading zero"); 
			e.value="";
			retrievePosition(e);
		} 
	}
} 

function retrievePosition(e){
	setTimeout(function(){e.focus();e.select();} ,1);
}

//***********************************************************************
//  Checkbox listing functions
//***********************************************************************


function checkAllDay(arg,day,containerId)
{
    var i = day;
    var maxTime = i + 23;
    var cbxTimeId;
    
    for (i;i<=maxTime;i++)
    {
        cbxTimeId = containerId + '_ctl' + formateInt(i) + "_cbxTime";
        if (document.getElementById(cbxTimeId)!= null  )
        {
            document.getElementById(cbxTimeId).checked = arg.checked; 
        }
    }
        
 
}

function checkAllTimeSlot(arg, timeslot, containerId)
{
    var i = timeslot ;
    var maxTime = i + 144  ;
    for (i ; i <= maxTime; i=i+24)
    {
        cbxTimeId = containerId + '_ctl' + formateInt(i) + "_cbxTime";
        if (document.getElementById(cbxTimeId)!= null  )
        {
            document.getElementById(cbxTimeId).checked = arg.checked; 
        }
    }
}

function formateInt(arg)
{
    if (arg <= 9)
        return ('0' + arg.toString()); 
    else
        return arg.toString(); 
    
}

//***********************************************************************
//  Rad Ajax Actions
//***********************************************************************

var timeoutId = null;  
function OnRequestStart(sender, args)  
{  
    if(args.EventTarget.indexOf("Export") >0 )
    {
        //Stop Ajax Request for Exproting
        args.EnableAjax = false;
    }
    else
    { 
        // set your desired timeout value here in milliseconds  
        var connectionTimeout = 20000; // 20sec  
          
        // get the XMLHttpRequest object  
        var requestInProgress = args.XMLHttpRequest;  
                
        // abort the request and hide the loading panel if the Ajax Request has not returned prior   
        // to the evaluation of the expression (that occurs after the specified number of milliseconds)  
        timeoutId = window.setTimeout(function()  
        {  
            requestInProgress.abort();
            if (requestInProgress != null) {
			    requestInProgress.abort(); 
		    }
            alert("Connection timeout, this page will be reset");  
            window.location.reload(); 
        }, connectionTimeout);  
    }

}  


  
function OnResponseEnd(sender, args)  
{  
    // clear the timeout if the request returns successfully  
    window.clearTimeout(timeoutId);  
}


function popupMasterInfo() {
    var newwindow = window.open('master-info.aspx', 'PictureView', 'width=300,height=300,left=200,top=200,screenX=200,screenY=100,alwaysRaised=yes');
    if (window.focus && newwindow != null) { newwindow.focus() }
}
