
function checkIsNum(fld, e, allowSpace,isDouble)
{
		var space='';
		var strCheck;
		if(allowSpace!=null && allowSpace==true)
			space=' ';
		if (isDouble !=null && isDouble == true)
		 strCheck = '0123456789.' + space;
		else
		 strCheck = '0123456789' + space;
		 
		var aux = aux2 = '';
		var whichCode = (window.Event) ? e.which : e.keyCode;
		if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;  // Enter
		
		key = String.fromCharCode(whichCode);  // Get key value from key code
		if (strCheck.indexOf(key) == -1)
			return false;  // Not a valid key
		if(fld.value.indexOf(".")>=0 && whichCode==46)
		{
			return false;
		}
		return true;
}

function checkLimitedNum(fld, e, allowSpace,isDouble,maxLength)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;  // Enter
	//alert(fld.selectionEnd +" | " + fld.selectionStart);
	
	var selected = false;
	if(fld.selectionEnd == undefined)
		selected = document.selection.createRange().text.length>0;
	else
		selected = fld.selectionEnd != fld.selectionStart;
			
	return checkIsNum(fld, e, allowSpace,isDouble)&&((fld.value.length<maxLength)||selected);
}

function ValidateNonNumericField(fld, e, allowSpace, isDouble){
	var space='';
	var strCheck;
	if(allowSpace!=null && allowSpace==true)
		space=' ';
	if (isDouble !=null && isDouble == true)
	 strCheck = '0123456789.' + space;
	else
	 strCheck = '0123456789' + space;
	 
	for (i = 0; i < fld.value.length; i++){
		key = fld.value.charAt(i);  // Get key value from key code
		if (strCheck.indexOf(key) == -1)
		{
			fld.value = '';
			fld.focus();
			return;
		}
	}
}

function ValidateEmptyField(fld,e,dftValue)
{
	if(fld.value=="")
		fld.value=dftValue;
}

function daysInFebruary(year)
{
	return ( ( year % 4 == 0 ) && ( ( ! ( year % 100 == 0 ) ) || ( year % 400 == 0 ) ) ) ? 29 : 28;
}

function isLegalDate(year,month,day) {
	var y = parseInt(year);
	var m = parseInt(month);
	var d = parseInt(day);
	switch (m) {
		case 2:
			if (d > daysInFebruary(y) ) return false;
		case 4:
		case 6:
		case 9:
		case 11:
			if ( d > 30 ) return false;
		default:
			if ( d > 31 ) return false;
	}
	return true;
}

function isDouble(el)
{
	var str =/^\d*(\.\d*)?$/;
	if(!str.test(el.value))
	{
		el.value='';
		return;
	}
}

function validSpecifyChar(str)
{
	var jgpattern =/^[ A-Za-z0-9]+$/; 
	if(!jgpattern.test(str)){
		return false; 
	}
	return true;
}

function validCharAndSpace(str,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if(whichCode == 8) return true;
	
	if(whichCode==32) return true;
	
	if(whichCode >= 65 && whichCode <= 122)
	{
		return true;
	}
	return false;
	
}


/**
 * This method checks a string for only UNICODE digits and 
 * UNICODE letters and and returns a boolean.
 * Additionally this method also allows a space or hyphen character.
 * This overloaded method also allows other special characters.
 *
 * @param strField - the string to validate
 * @param otherAllowableCharacters - string containing a set of allowable characters
 * 
 * @return true if the string contains only UNICODE digits and 
 * UNICODE letters
 */
function isAlphaNumeric(strField,otherAllowableChars){
	var letter;
	for(i=0;i<strField.length;i++){
		letter = strField.substr(i,1);
		if(isNumber(letter) || isLetter(letter) || otherAllowableChars.indexOf(letter)>-1)
			continue;
		else
			return false;
	}
	return true;
}

/**
 * Returns true if the string consists of UNICODE letters.
 * This overloaded method also allows other special characters.
 * @param strField - the input string
 * @param otherAllowableCharacters - string containing a set of allowable characters
 *
 * @return	  true if the string consists only of letters.
 *
 */
function isAlpha(strField,otherAllowableChars){
	var letter;
	for(i=0;i<strField.length;i++){
		letter = strField.substr(i,1);
		if(isLetter(letter) || otherAllowableChars.indexOf(letter)>-1)
			continue;
		else
			return false;
	}
	return true;
}


/**
 * Returns true if the string consists of UNICODE letters.
 * @param letter - the input string
 *
 * @return	  true if the string consists only of letters.
 *
 */
function isLetter(letter){
	var asciiValue = letter.charCodeAt(0);
	//A-Z a-z 
	if((asciiValue>=65 && asciiValue<=90) || (asciiValue>=97 && asciiValue<=122))
		return true;
	
	if(asciiValue==170 || asciiValue==181 ||  asciiValue==186)
		return true;
	
	if((asciiValue>=192 && asciiValue<=214) || (asciiValue>=216 && asciiValue<=246) || (asciiValue>=248 && asciiValue<=254))
		return true;
	
	return false;
}

/**
 * Returns true if the string consists of UNICODE numbers.
 * @param letter - the input string
 *
 * @return	  true if the string consists only of numbers.
 *
 */
function isNumber(letter){
	var asciiValue = letter.charCodeAt(0);
	//0-9
	if((asciiValue>=48 && asciiValue<=57))
		return true;
	
	return false;
}


function validateZip(zipControlId){

	var zipCode = ASC.getEl(zipControlId).getValue();
    var valid = true;//ASC.util.isZip(zipCode);	
    
    if(zipCode=='00000' || zipCode=='99999') {
	   valid = false;
    }	    
    if(!valid){
    	ASC.getEl(zipControlId).focus();
    }
    return valid;
}

function isNumeric(controlId,locale){
    var valid = ASC.util.isNumeric(ASC.getEl(controlId,locale).getValue());	

    if(!valid){
    	ASC.getEl(controlId).focus();
    }
    return valid;
}

ASC.namespace('ASC.CF.ValidateZip');

ASC.CF.ValidateZip = Ext.extend(Ext.util.Observable, {
	ajax: null,
	
	constructor: function ()
	{
		var me = this;
		ASC.CF.ValidateZip.superclass.constructor.call(this);
		
		this.addEvents({
			success: true,
			failure: true
		});
		
		this.ajax = new ASC.Ajax.AjaxEngine({ handleAsDefault: 'json' });
		this.ajax.registerRequest("validateZipCode", ASC.cfg.getContextPath() + '/util/validZip.ajax');		
		this.ajax.registerAjaxObject('validateZip',new ASC.Ajax.Response({
			events: {
				success: function (response, ioArgs)
				{					
					me.fireEvent("success", response, ioArgs);
				}
			}
		}));
		
		
	},
	
	
	validate: function(zipCode) 
	{
		this.ajax.sendRequest('validateZipCode', {
		params: {
    			zipCode:zipCode  
			}
		});
	}
});
		
ASC.apply(Page,{
	
	sessionAjax: null,
	
	initSessionAjax: function ()
	{		

		Page.sessionAjax = new ASC.Ajax.AjaxEngine({ handleAsDefault: 'json' });
		Page.sessionAjax.registerRequest('sessionReset', Page.contextPath + '/util/restartSession.ajax');
		Page.sessionAjax.registerAjaxObject('sessionRestarted', new ASC.Ajax.Response());
	},
	
	restartSession: function ()
	{		
		Page.sessionAjax.sendRequest('sessionReset');	
	},	
	
	startTimer: function ()
	{
		this.myTimer = setTimeout(this.alertSessionExpiration,420000);
		this.timerRunning = true;				
	},
				
	restartTimer: function () 
	{
		if(this.timerRunning){
			clearTimeout(this.myTimer);
		}
		this.startTimer();
		this.restartSession();
	},
	
	alertSessionExpiration: function ()
	{				
		var calculateTime = function(hours, minutes){
			var ampm = 'AM';
			var returnHours = hours;
			var returnMinutes = minutes;
			if(hours >= 12){
				ampm = 'PM';
				returnHours = returnHours - 12;
			}
			if(returnHours==0){
				returnHours = 12;
			}
			
			if(returnMinutes <= 9){
			
				returnMinutes = '0' + returnMinutes;
			}
			return returnHours + ':' + returnMinutes + ' ' + ampm;
		};

		var todaysDate = new Date();
		var currentTime= Date.UTC(todaysDate.getUTCFullYear(), todaysDate.getUTCMonth(), todaysDate.getUTCDate(), todaysDate.getUTCHours(), todaysDate.getUTCMinutes(), todaysDate.getUTCSeconds());
		var expiresOn = currentTime + 180000; /* 3 minutes = 180000 */								
		var newDate =new Date(expiresOn);				
		
		if(confirm(Page.timeoutMsg1 + ' ' + calculateTime(newDate.getHours(), newDate.getMinutes())+ ' ' + Page.timeoutMsg2)){
			var todaysConfirmDate = new Date();
			var currentConfirmTime= Date.UTC(todaysConfirmDate.getUTCFullYear(), todaysConfirmDate.getUTCMonth(), todaysConfirmDate.getUTCDate(), todaysConfirmDate.getUTCHours(), todaysConfirmDate.getUTCMinutes(), todaysConfirmDate.getUTCSeconds());					
			if(currentConfirmTime >= expiresOn){
				window.location.href = Page.contextPath + '/credit/creditQuestionnaire.do';
			}else{
				Page.restartTimer();
			}
		}else{
			window.location.href = Page.contextPath + '/credit/creditQuestionnaire.do';
		}
	}	
});
