/*
	Scripts for formatting input field
*/



//=================================================================
// Account formater
//=================================================================
function format_Account_focus(e)
{
	str = e.value;
	str = str.replace(/^0+/,"");
	
	e.value = str;
	e.select();
}

function format_Account_blur(e)
{
	str = e.value;
	str = str.replace(/^0+/,"");
	
	e.value = str;
}


//=================================================================
// Decimal formater
//=================================================================
function format_Decimal_focus(e)
{
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
	
	if(common_SplitDecimal(str,split)==false)
		return;
	
	e.value = sign + split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_Decimal_blur(e)
{
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
		
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = sign + thousands + lang_DecimalSymbol + after;
}


//=================================================================
// Currency formater
//=================================================================
function format_Currency_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
	
	if(common_SplitDecimal(str,split)==false)
		return;
		
	
	e.value = sign + split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_Currency_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
		
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = sign + thousands + lang_DecimalSymbol + after;
}


//=================================================================
// CurrencySKK formater
//=================================================================
function format_CurrencySKK_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
	
	
	if(common_SplitDecimal(str,split)==false)
		return;
	
	e.value = sign + split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_CurrencySKK_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");
		
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = sign + thousands + lang_DecimalSymbol + after;
}


//=================================================================
// Integer formater
//=================================================================
function format_Integer_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var str;
	str = e.value;
	while (str.indexOf(lang_ThousandSymbol) != -1) str = str.replace(lang_ThousandSymbol,"");
	e.value = str;
	e.select();

}

function format_Integer_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	sign = "";
	if (str.indexOf("-") != -1) {	
		sign = "-";
		str = str.replace("-","");		
	}
	
	str = str.replace(/^0+/,"");		
	
	// format thousands
	var thousands = "";	
	var counter = 0;
	var pos = str.length;

	while(pos>0)
	{
		pos--;
		ch = str.charAt(pos);

		if(counter==3)
		{			
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "";
	
	e.value = sign + thousands;
}


//=================================================================
// Time formater
//=================================================================
function format_Time_blur(e){
	// check for null or empty string
	if(e.value==null || e.NeedFormat!=true) return;
	var str = common_TrimText(e.value);
	if(str == "") return;
	
	if(str.length > 5) return;
	
	var splitted = str.split(lang_TimeSeparator);

	if(splitted.length == 1)
	{
		// no split character
		if(str.length == 4)
			e.value = str.substring(0,2) + lang_TimeSeparator + str.substring(2,4);
		else if(str.length == 3)
			e.value = "0" + str.substring(0,1) + lang_TimeSeparator + str.substring(1,3);
		else if(str.length == 2)
			e.value = str + lang_TimeSeparator + "00";
		else if(str.length == 1)
			e.value = "0" + str + lang_TimeSeparator + "00";
	}
	else if(splitted.length == 2)
	{
		// one split character
		var hh = splitted[0];
		var mm = splitted[1];
		
		if(hh.length==0)
			hh = "00";
		else if(hh.length==1)
			hh = "0" + hh;
		
		if(mm.length==0)
			mm = "00";
		else if(mm.length==1)
			mm = "0" + mm;
		
		e.value = hh + lang_TimeSeparator + mm;
	}

	e.NeedFormat = false;
}

function format_Time_focus(e)
{
	e.NeedFormat = true;

	if(e.value == null)
		return;
		
	e.value = e.value.replace(new RegExp(lang_TimeSeparator,"g"),"");
	e.select();
}


//=================================================================
// uCurrency formater
//=================================================================
function format_uCurrency_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	if(common_SplitDecimal(e.value,split)==false)
		return;
	
	e.value = split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_uCurrency_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
		
	str = e.value;
	
	str = str.replace(/^0+/,"");
	
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = thousands + lang_DecimalSymbol + after;
}


//=================================================================
// uCurrencySKK formater
//=================================================================
function format_uCurrencySKK_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	if(common_SplitDecimal(e.value,split)==false)
		return;
	
	e.value = split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_uCurrencySKK_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	
	str = e.value; 
	
	str = str.replace(/^0+/,"");
	
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = thousands + lang_DecimalSymbol + after;
}


//=================================================================
// uDecimal formater
//=================================================================
function format_uDecimal_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var str;
	
	str = e.value;
		
	
	if(common_SplitDecimal(str,split)==false)
		return;
	
	e.value = split.before + lang_DecimalSymbol + split.after;
	e.select();
}

function format_uDecimal_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;	
	var str;
	
	str = e.value;
	
	str = str.replace(/^0+/,"");
			
	if(common_SplitDecimal(str,split)==false)
		return;
	
	// format thousands
	var thousands = "";
	var before = split.before;
	var after = split.after;
	var counter = 0;
	var pos = before.length;
	
	while(pos>0)
	{
		pos--;
		ch = before.charAt(pos);

		if(counter==3)
		{
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "0";
	
	pos = after.length;
	for(pos = after.length; pos<2; pos++)
		after = after + "0";
	
	e.value = thousands + lang_DecimalSymbol + after;
}


//=================================================================
// uInteger formater
//=================================================================
function format_uInteger_focus(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var str;
	str = e.value;
	while (str.indexOf(lang_ThousandSymbol) != -1) str = str.replace(lang_ThousandSymbol,"");
	e.value = str;
	e.select();
}

function format_uInteger_blur(e){
	e.value = common_nbsp_to_sp(e.value);
	var split = new Array;
	var sign; //znamienko
	var str;
	
	str = e.value;
	while (str.indexOf(lang_ThousandSymbol) != -1) str = str.replace(lang_ThousandSymbol,"");
	
	str = str.replace(/^0+/,"");
	
	// format thousands
	var thousands = "";	
	var counter = 0;
	var pos = str.length;

	while(pos>0)
	{
		pos--;
		ch = str.charAt(pos);

		if(counter==3)
		{			
			thousands = lang_ThousandSymbol + thousands;
			counter = 0;
		}
		
		thousands = ch + thousands;
		counter++;
	}
	
	// final formatting
	if(thousands.length==0)
		thousands = "";
	
	e.value = thousands;
}

//=================================================================
// email formater
//=================================================================

function format_Email_focus(e){}
function format_Email_blur(e){}

//=================================================================
// date formater
//=================================================================

function format_Date_blur(e){
	if (e.value == null || e.value =="" || e.NeedFormat != true) return;	

	str = e.value;
	
	if (lang_DateSeparator != "."){
		while (str.indexOf(lang_DateSeparator) != -1) str = str.replace(lang_DateSeparator,".");
	}

	while (str.indexOf("/") != -1) str = str.replace("/",".");
	
	if (str.indexOf(".") == -1){
		if (str.length == 8) str = str.substring(0,2) + lang_DateSeparator + str.substring(2,4) + lang_DateSeparator + str.substring(4,8);
		else if (str.length == 6) str = str.substring(0,1) + lang_DateSeparator + str.substring(1,2) + lang_DateSeparator + str.substring(2,6);
		else return;
	}

	var pattern=/^\d{1,2}\.\s{0,1}\d{1,2}\.\s{0,1}\d{2,4}$/;
	if ( !pattern.test(str) ) {
		return;
	}

	date = 	str.split(".");
	day = date[0];
	month = date[1];
	year = date[2];

	if (day.length == 1) day = "0" + day;
	if (month.length == 1) month = "0" + month;
	if (year.length == 2) year = "20" + year;
	
	e.value = day + lang_DateSeparator + month + lang_DateSeparator + year;
	
	e.NeedFormat = false;
}

function format_Date_focus(e)
{
	e.NeedFormat = true;

	if (e.value == null || e.value =="") return;	

	var str = e.value;
	
	while (str.indexOf(lang_DateSeparator) != -1) {
		str = str.replace(lang_DateSeparator,"");
	}
	
	e.value = str;
	e.select();
}

//=================================================================
// sms formater
//=================================================================

function format_Sms_blur(e){

	if (e.value == null || e.value =="") return;	

	str = e.value;
	//str = str.replace(" ","");
	//str = str.replace(/\D/g,"");
	str = str.replace("/","");
	str = str.replace("-","");

	if (str.length > 0 && str.substring(0,1) == '+'){
		str = str.substring(1, str.length)
	}
	if (str.length > 1 && str.substring(0,2) == '00'){
		str = str.substring(2, str.length)
	}
	if (str.length > 0 && str.substring(0,1) == '0'){
		str = str.substring(1, str.length)
	}
	if (str.length > 2 && str.substring(0,3) == '421'){
		str = "+" + str;
	}
	else{
		str = "+421" + str;
	}
	if (e.maxLength > 0)
	{
		str = str.substring(0, e.maxLength);
	}
	
	e.value = str;
}

function format_Sms_focus(e){}

//=================================================================
// MonthYear formater
//=================================================================

function format_MonthYear_blur(e)
{
	if(e.value == null || e.value == "") return;
	
	str = e.value;
	str = str.replace(".","");
	separator = "/";
	
	if (str.indexOf(separator) == -1){
		if (str.length == 6) str = str.substring(0,2) + separator + str.substring(2);
		else if (str.length == 5) str = str.substring(0,1) + separator + str.substring(1);
		else if (str.length == 4) str = str.substring(0,2) + separator + str.substring(2);
		else if (str.length == 3) str = str.substring(0,1) + separator + str.substring(1);
		else return;
	}
	var pattern=/^\d{1,2}\/\d{2,4}$/;
	if ( !pattern.test(str) ) {
		return;
	}
	date = 	str.split(separator);
	month = date[0];
	year = date[1];

	if (month.length == 1) month = "0" + month;
	if (year.length == 2) 
	{
		if (parseInt(year, 10) < 30 ) year = "20" + year;
		else year = "19" + year;	
	}
	e.value = month + separator + year;

	e.NeedFormat = false;
}

function format_MonthYear_focus(e)
{
	e.NeedFormat = true;

	if (e.value == null || e.value =="") return;	

	var str = e.value;

	while (str.indexOf("/") != -1) {
		str = str.replace("/","");
	}

	e.value = str;
	e.select();
}

//=================================================================
// ZIP formater
//=================================================================

function format_ZIP_blur(e)
{
	if(e.value == null || e.value == "") return;
	
	str = e.value;
	str = str.replace(" ","");
	
	if (e.maxLength > 0)
	{
		str = str.substring(0, e.maxLength);
	}
	
	e.value = str;

}
function format_ZIP_focus(e){}

//=================================================================
// Mobile formater
//=================================================================

function format_Mobile_blur(e){

	if (e.value == null || e.value =="") return;	

	str = e.value;
	str = str.replace("+4219", "09");
	str = str.replace(/\D/g,"");

	if (str.length > 0 && str.substring(0,1) == "9"){
		str = "0" + str;
	}
	if(str.length > 0 && str.substring(0,2) != "09"){
		str = "09" + str;
	}
	if (e.maxLength > 0)
	{
		str = str.substring(0, e.maxLength);
	}
	
	e.value = str;
}

function format_Mobile_focus(e){}

//=================================================================
// IDCard formater
//=================================================================

function format_IDCard_blur(e){

	if (e.value == null || e.value =="") return;	

	str = e.value;
	str = str.replace(/\s/g,"");
	str = str.toUpperCase();

	e.value = str;
}

function format_IDCard_focus(e){}

var Format = 1;

