﻿//This is used to trim a string in Javascript
String.prototype.TrimAll = function() 
{ 
    return this.replace(/^\s+|\s+$/g, ''); 
}

//---- This function is used to consolidate the error messages in the User control
function ManageError(errorList, errBlkId)
{ 
    var tab = document.getElementById(errBlkId + '_tblError'); 
    
    if(tab != null)
    {        
        var tabRow=parseInt(tab.rows.length);
        
        if(tabRow > 0)
        {
            for(var i = 0; i < tabRow; i++)
            {
               tab.deleteRow(tab.i);           
            }
        }
            
        if(errorList.length > 0)
        {
            var newRow = tab.insertRow(0);
            var newCell = newRow.insertCell(0);																					
            var newText  = document.createTextNode('Please correct the following problem(s):')
            newCell.appendChild(newText);        
        }
        
        for(var i = 0; i < errorList.length; i++)
        {
            var newRow      = tab.insertRow(i + 1);
            var newCell     = newRow.insertCell(0);																					
            var errorText   = errorList[i];
            var newText     = document.createTextNode('■  ' + errorText + '\n\r');
            newCell.appendChild(newText);
        }        
    }
}
//this function is used for opening link in another window with specified size
function Winopen(Page,W,H)
{
	win = window.open(Page, "newWin", "left=80,top=80,toolbar=no,location=no,directories=no, status=no, menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=" + W + ",height=" + H );
	return false;
}

//This function is used ask user input before delete any row
function ConfirmationDelete(itemToDelete)
{
    return confirm("Are you sure to delete the " + itemToDelete + " ?");
}

//This function is used to clear gadget cookies.
function delCookies() 
{
	var cookieCt = 0;

	if (document.cookie != "") 
	{
		var thisCookie = document.cookie.split(";");
		cookieCt = thisCookie.length;

		var expireDate = new Date();
		expireDate.setDate(expireDate.getDate()-1);

		for (var i=0; i<cookieCt; i++) {
			var cookieName = thisCookie[i].split("=")[0];
			if(cookieName.indexOf('ASPXAUTH') == -1 && cookieName.indexOf('ErrorMessage') == -1)
			{
			    document.cookie = cookieName + "=;expires=" + expireDate.toGMTString();
			}
		}
	}
}

// Function to validate Security Answers
function ValidateSecurityQs(errCtrlId)
{    
    var errMsgColl = new Array();    
    var txtAnswer1 = document.getElementById('ctl00_cphBody_' + 'txtAnswer1');
    var txtAnswer2 = document.getElementById('ctl00_cphBody_' + 'txtAnswer2');
    var txtAnswer3 = document.getElementById('ctl00_cphBody_' + 'txtAnswer3');
    var drpQuestion1 = document.getElementById('ctl00_cphBody_' + 'drpQuestion1').selectedIndex;
    var drpQuestion2 = document.getElementById('ctl00_cphBody_' + 'drpQuestion2').selectedIndex;
    var drpQuestion3 = document.getElementById('ctl00_cphBody_' + 'drpQuestion3').selectedIndex;
    
    if( ( drpQuestion1 == drpQuestion2 ) || ( drpQuestion1 == drpQuestion3 ) || ( drpQuestion2 == drpQuestion3 ) )
    {
        errMsgColl.push("Please select three unique security questions.");
        saveUserCtrl = document.getElementById('ctl00_cphBody_' + 'ucConfirmSaving_lblMessage');	    
	    saveUserCtrl.style.visibility="hidden";
	    saveUserCtrlTbl = document.getElementById('ctl00_cphBody_' + 'ucConfirmSaving_tblConfirm');	    
	    saveUserCtrlTbl.style.visibility="hidden";	
    }
    if( (txtAnswer1.value.TrimAll() == "") || (txtAnswer2.value.TrimAll() == "") || (txtAnswer3.value.TrimAll() == "") )
    {
        errMsgColl.push("Answers should not be blank.");
        saveUserCtrl = document.getElementById('ctl00_cphBody_' + 'ucConfirmSaving_lblMessage');	    
	    saveUserCtrl.style.visibility="hidden";
	    saveUserCtrlTbl = document.getElementById('ctl00_cphBody_' + 'ucConfirmSaving_tblConfirm');	    
	    saveUserCtrlTbl.style.visibility="hidden";	    
    }  
    
    if(errMsgColl.length > 0)
    {
        ManageError(errMsgColl, errCtrlId);
        return false;
    }
    
    return true;
}
