var IgnoreChars=",37,39,38,16,40,36,35,34,33,8,13,9,46,112,113,114,115,116,117,118,119,120,121,122,123";

function GetSelectedText(obj)
{
    var returnText = "";
    if (document.selection) 
    {
        if(document.selection.type == 'Text')
        {
		    var newRange = document.selection.createRange();
		    returnText=newRange.text;
		    newRange.text='';
		}
    	document.selection.createRange().text='';
    }
    else if (obj.selectionStart)
    {
//        obj.selectionStart=0;
//        obj.selectionEnd=0;
        returnText=obj.value.substring(obj.selectionStart, obj.selectionEnd);
    }
    
    return returnText;
}

function stopEvent(e) 
{
	if(!e) var e = window.event;
	
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = false;

	//e.stopPropagation works only in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
	return false;
}

function GetKeyCodeForFW(e) 
{
    if(!window.event)
    {
        return "," + e.keyCode + ",";
    }
    
    return "--";
}

// isDigit
//    c 01....9 ise true, yoksa false doner.
function isDigit(myInput){
	var test = '' + myInput;
	if (isNaN(parseInt(test)))
		{
		return false;
		}
	return true;
}
// trim fonksiyonlari
// ltrim: left trim
// rtrim: right trim
// ntrim: her iki taraftan trim
function lTrim(myInput2){
 while(myInput2.indexOf(' ',0)==0){
  myInput2 = myInput2.substr(1);
 }
 return myInput2;
}
function rTrim(myInput1){
 if (myInput1.length==0)
  return '';
 while(myInput1.lastIndexOf(' ')==(myInput1.length-1)){
  myInput1 = myInput1.substr(0,myInput1.length-1);
 }
 return myInput1;
}
function nTrim(myInput3){
 myInput3 = rTrim(lTrim(myInput3));
 return myInput3;
}

function leapYear(Year){
	if(((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0))
		return (1);
	else
		return (0);
}

function getDaysInMonth(month, year) {
	var days;
	if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
		days=31;
	else if (month==4 || month==6 || month==9 || month==11)
		days=30;
	else if (month==2){
		if (leapYear (year)==1)
			days=29;
		else
			days=28;
	}
	return (days);
}
function setDate(myInput){
	if (myInput.value.length>0){
		myInput.value = nTrim(myInput.value);
		var hata=0;
		var myDate='';
		var myMonth='';
		var myYear='';
		
		for(i=0; i<2 ; i++){
			if(isDigit(myInput.value.substr(i,1))){
				myDate=myDate + myInput.value.substr(i,1)
			}
			else{
				hata=1;
			}
		}
		
		if(myInput.value.substr(2,1)!='/' || myInput.value.substr(5,1)!='/')
		{
    		hata=1;
		}

		for(i=3; i<5 ; i++){
			if(isDigit(myInput.value.substr(i,1))){
				myMonth=myMonth + myInput.value.substr(i,1)
			}
			else{
				hata=1;
			}
		}

		for(i=6; i<10; i++){
			if(isDigit(myInput.value.substr(i,1))){
				myYear=myYear + myInput.value.substr(i,1);
			}
			else{
				hata=1;
			}
		}

		if(hata==0){
			if(parseInt(myDate) <= getDaysInMonth(myMonth, myYear)){
				if(parseInt(myMonth) <= 12){
					if((parseInt(myYear) < 2099) && (parseInt(myYear) > 1900)){
					}
					else{
						hata=1;
					}
				}
				else{
					hata=1;
				}
			}
			else{
				hata=1;
			}
		
			if(hata==1){
				alert(LocalizeTexts.InvalidDate);
				myInput.value='';
			}
		}	
		else{
			alert(LocalizeTexts.WrongDateFormat);
			myInput.value='';
		}
	}
}

// Gordion modülleri
function CheckMask(e, toField, tcMask )
{
	var i;
	var lcVal = toField.value;
	var llValOK = true;

	if (tcMask.length == 0)
		return true;

    var evt = e || window.event;
	var CharCode = String.fromCharCode(evt.which || evt.keyCode);
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;

	for (i=0; i<lcVal.length && llValOK; i++)
	{
		lcMaskChar = tcMask.charAt(i);
		lcValChar  = lcVal.charAt(i);
		switch (lcMaskChar)
		{
			case '9':  // Sayısal
    	        llValOK = ("0123456789".indexOf(lcValChar)>=0);
				break;			
			case 'X':  // Herhangi
				llValOK = true;
				break;			
			case '!':  // Büyük harf
				llValOK = (lcValChar >= 'A' && lcValChar <= 'Z') || (lcValChar >= '0' && lcValChar <= '9') || (lcValChar = '%' ) || (lcValChar = '*' );
				break;
			default:   // masktaki harf
				llValOK = lcValChar == lcMaskChar;
		}
	}

	if (!llValOK)
		toField.focus();
		//toField.value = '';

	if(!llValOK && CharCode != 13)
	{
        stopEvent(evt);
	}

	return llValOK;
}

function InputMask(e, toField, tcMask )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;

	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;
    
	var lcMaskChar, lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = true;
	
	var selectedText = GetSelectedText(toField);	
	
	if (tcMask.length == 0)
		return true;

	if (toField.value.length >= tcMask.length)  // InputMask tamam. Artık kabul etme.
	{
		llRetVal = false;
	}
	else                                        // Maskı yeni gelen harfe göre parse et
	{
		lcMaskChar = tcMask.charAt(toField.value.length);
		switch (lcMaskChar)
		{
			case '9':  // Sayısal
            	llRetVal = ("0123456789".indexOf(lcNewChar)>=0);
				break;
			case 'X':  // Herhangi
				break;
			case '!':  // Büyük harf
				CharCode = lcNewChar.toUpperCase().charCodeAt(0);
				break;
			default:   // masktaki harf
			    if(lcMaskChar!=lcNewChar)
			    {
				    toField.value += lcMaskChar;
				    llRetVal = InputMask(e, toField, tcMask);
			    }
		}
	}
	
	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}
	
	return llRetVal;
}

function formatInt ( ctrl )
{
	var separator = ",";
	var dpoint = ".";
	
	var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
	var intdpoint = ctrl.value.indexOf(dpoint);
	if (intdpoint<0)
	{
		var intm = ctrl.value.replace ( new RegExp ( separator, "g" ), "" );
		do
		{
			intm = intm.replace ( regexp, separator + "$1" );
		}
		while ( intm.search ( regexp ) >= 0 )
		ctrl.value = intm;
	}
	else
	{
		var slefthand = ctrl.value.substring(0,intdpoint);
		var intm = slefthand .replace ( new RegExp ( separator, "g" ), "" );
		do
		{
			intm = intm.replace ( regexp, separator + "$1" );
		}
		while ( intm.search ( regexp ) >= 0 )
		var srighthand = ctrl.value.substring(intdpoint+1,ctrl.value.length);
		var srighthandclear =  srighthand.replace(dpoint,"");
		ctrl.value = intm + "." + srighthandclear.replace ( new RegExp ( separator, "g" ), "" );
	}
}

function PriceMask(e, toField )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;
	var lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = false;
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;

	llRetVal = ("0123456789.,".indexOf(lcNewChar)>=0);

	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}
	
	return llRetVal;
}

function ERetailPhoneMask(e, toField )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;
	var lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = false;
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;
	
	llRetVal = ("0123456789-+".indexOf(lcNewChar)>=0);

	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}
	
	return llRetVal;
}

function UpperMask(e, toField )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;
	var lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = true;
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;
    try
    {
	    if(window.event)
	    {
	        evt.keyCode = lcNewChar.toUpperCase().charCodeAt(0);
	    }
	    else
	    {
	        toField.value = toField.value.toUpperCase();
	    }
	}
	catch(err)
	{
	}

	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}

	return llRetVal;
}
function LowerMask(e, toField )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;
	var lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = true;
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;

	try
    {
	    if(window.event)
	    {
	        evt.keyCode = lcNewChar.toLowerCase().charCodeAt(0);
	    }
	    else
	    {
	        toField.value = toField.value.toLowerCase();
	    }
	}
	catch(err)
	{
	}
	
	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}

	return llRetVal;
}
function setTime(myInput)
{
	if (myInput.value.length>0)
	{
		myInput.value = nTrim(myInput.value);
		var hata=0;
		var myHour='';
		var myMinute='';
		
		for(i=0; i<2 ; i++)
		{
			if(isDigit(myInput.value.substr(i,1)))
			{
				myHour=myHour + myInput.value.substr(i,1)
			}
			else
			{					
				hata=1;
			}
		}

		for(i=3; i<5 ; i++)
		{
			if(isDigit(myInput.value.substr(i,1)))
			{
				myMinute=myMinute + myInput.value.substr(i,1)
			}
			else
			{
				hata=1;
			}
		}

		if(hata==0)
		{
			if(parseInt(myHour) <= 23)
			{
				if((parseInt(myMinute) < 60) && (parseInt(myMinute) >= 0))
				{
				}
				else
				{
					hata=1;
				}
			}
			else
			{
				hata=1;
			}
		}	
		else
		{
			hata=1;
		}
		
		if(hata==1)
		{
			alert(LocalizeTexts.InvalidTime);
			//myInput.focus();
			myInput.value='';
		}
	}
}

function OnlyNumeric(e, toField )
{
    var evt = e || window.event;
    var CharCode = evt.which || evt.keyCode;
	var lcNewChar = String.fromCharCode(CharCode);
	var llRetVal = false;
	
	if(IgnoreChars.indexOf(GetKeyCodeForFW(e))>=0)
	    return true;

	llRetVal = ("0123456789".indexOf(lcNewChar)>=0);
	
	if(!llRetVal && CharCode!=13)
	{
        stopEvent(evt);
	}
	
	return llRetVal;
}