// -----------------------------------------   FUNCTION FOR FEED -----------------------------------------------------
function SetFormatedValueDigits( theValue, Digs) {
	var anum = 1.1;
	var comma = anum.toString();
	var strRet = '';
	var Tenths = null;
	var strTwo = null;
	var strArr = null;
	var l = 0;
	var i = 0;
	var dot = '.';
	var minus = '';
	var Zeros = '0000000000000000000000000';

	comma = comma.substring(1,2);
	if( comma == '.' ) dot = ',';

	if( theValue != theValue )	return( '0' );
	if( Digs != Digs ) return( '' + theValue );

	with( Math ) {
		Digs = abs(Digs);
		Tenths = pow(10,Digs);
		theValue = round(theValue * Tenths ) / Tenths;
	}

	strTwo = theValue.toString();
	strArr = strTwo.split(comma);
	//alert( 'val=' + theValue + ' - Digs=' + Digs );
	//if( strArr.length != 2 ) return( theValue.toString() );
	//alert( 'after [strArr.length != 2]' );

	if( strArr.length > 1 ) {
		l = strArr[1].length;
		if( l < Digs )
			for( i=0; i<Digs-l; i++)
				strArr[1] += '0';
	}

	if( strArr[0].charAt(0) == '-' ) {
		minus = '-';
		strArr[0] = strArr[0].substring(1);
	}

	l = strArr[0].length;
	strTwo = ''
	while( l >= 3 ) {
		strTwo = (comma + strArr[0].substr(l-3)) + strTwo;
		strArr[0] = strArr[0].substring(0, l-3);
		l = strArr[0].length;
	}

	if( l > 0 )
		strTwo = strArr[0] + strTwo;
	if( strTwo.charAt(0) == comma.charAt(0) )
		strTwo = strTwo.substr(1);

	if( strArr.length > 1 )
		strTwo = minus + strTwo + dot + strArr[1];
	else {
		if( Digs == 0 )
			strTwo = minus + strTwo;
		else
			strTwo = minus + strTwo + dot + Zeros.substring(0, Digs);
	}

	return( strTwo );
}

function graphPopup(newHtml)
{
	window.open(newHtml,"Win","resizable=yes,toolbar=no,scrollbars=yes,width=350,height=150,status=no,menubar=no");
}
function replaceChar(str, chr, withChr){
		if (!str || str == "" || str == null) return "";
		var remainingCharacters = str.length;
		while (remainingCharacters != 0){
			if (str.indexOf(chr) > -1){
				if (str.indexOf(chr) == 0){
					temp = withChr + str.substring(1);
					str = temp;
					remainingCharacters = str.length - str.indexOf(chr)
				} else if (str.indexOf(chr) == str.length - 1) {
					temp = str.substring(0, str.length - 1) + withChar;
					str = temp;
					remainingCharacters = 0;
				} else {
					temp = str.substring(0, str.indexOf(chr)) + withChr + str.substring(str.indexOf(chr) + 1);
					str = temp;
					remainingCharacters = str.length - str.indexOf(chr)
				}
			} else {
				remainingCharacters = 0;
			}
		}
		return str;
}

function getDRXPrice(val){
if (val)
	return formatToDDP(parseFloat(replaceChar(val.toString(), ",",".")) * 340.75)
return;
}

function formatToDDP(number){
	if (NaN == number) return "0";
		var rounded = Math.round(number * 100) / 100
		var nl = rounded.toString().length
	if (rounded.toString().indexOf(".") == -1){
		return rounded.toString() + ".00";
	} else if (nl - rounded.toString().indexOf(".") == 2){
		return rounded.toString() + "0";
	}
	return rounded;
}

function numerify(ctrl) {
	if ((event.keyCode >= 33 && event.keyCode <= 46) || (event.keyCode == 9) || (event.keyCode == 16))
		return;
	ctrl.value = ctrl.value.replace(/\.$/,",").replace(/^,/,"0,").replace(/^0([^,])/g,"$1").replace(/[^0-9,\,]/g,"").replace(/,/,"#").replace(/,/g,"").replace(/#/,",")
}

function del(obj){
	obj.value = obj.value.replace(",","").replace(".","");
}

function cap(obj){
	var prevValue=obj.value
	obj.value=obj.value.toUpperCase().replace(/[\[\]\*\$\^]/g,"").replace(/¢/g,"Á").replace(/¸/g,"Å").replace(/¹/g,"Ç").replace(/º/g,"É").replace(/¼/g,"Ï").replace(/¾/g,"Õ").replace(/¿/g,"Ù").replace(/Û/g,"Õ").replace(/Ú/g,"É").replace(/à/g,"Õ").replace(/À/g,"É").replace(/ò/g,"Ó")
	var newValue=obj.value
}

function IsNumeric(aField){
	alert("called");
	if (aField.value != ""){
		alert(aField.value);
		for(var i=0; i<=aField.value.length-1; i++){
			if ((aField.value.charAt(i)<'0') || (aField.value.charAt(i)>'9')){
				alert("problem");
				return false;
			}
    	}
	}
	return true;
}

function checkASEround(field) {
    var field = replaceChar(field,",",".");
    var num = Math.round(field*100);
    if((field>3 && field<=60 && num%2>0) || (field>60 && num%5>0)) return false;
return true;
}


function formatAmount(amount,loc){
 
  var isNegative = false;
  if (amount < 0) {
	amount = Math.abs(amount);
	isNegative = true;
  } else {
	isNegative = false;
  }
  
   var text = amount + "";	
  
  if (loc=="en"){
	dec_pos = text.search(/\./);
	if (dec_pos == -1)
		dec_pos = text.length;
	if (text.length > (dec_pos+3)) {
		//Allow only two decimal digits
	   text = text.substring(0,dec_pos+3);
	} 
	for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (comma)
		text = text.substr(0, i) + "," + text.substr(i);
    }
    return text; 		
  }
  else if (loc=="el"){
	text= text.replace(/\./,",");//replace ending dot with comma if the input finishes with dot
		dec_pos = text.search(",");
	if (dec_pos == -1)
		dec_pos = text.length;
	if (text.length > (dec_pos+3)) {
		//Allow only two decimal digits
		text = text.substring(0,dec_pos+3) 
	}
	
	for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (dot)
		text= text.substr(0, i) + "." + text.substr(i);
	}		
  }
  
  if (isNegative) {
  	text = '-' + text;
  }
  
  return text;
}



function commafyEn(text,event) {

	// do not execute on left, right arrows
	if ((event.keyCode == 39) || (event.keyCode == 37)){
	  return;
	}

    var cursorPos = text.selectionStart;
        
    var tmpValue = text.value;   
    for (j = 0; j<tmpValue.length ; j++) {
      //alert(cursorPos+"   "+event.keyCode+"   "+tmpValue.charAt(j));
      if (("0123456789,.").indexOf(tmpValue.charAt(j)) == -1){
        text.value = text.value.replace(tmpValue.charAt(j),"")
        cursorPos -= 1;
      }
    }
    
    //48-57 digits
    //190 period
    //188 comma
    //8 back space
    //46 delete
    //if ((event.keyCode<48 || event.keyCode > 57)&&
    //    (event.keyCode!= 8)&&
    //    (event.keyCode != 46)&&
    //    (event.keyCode != 188)&&
    //    (event.keyCode != 190)){
    //  cursorPos -=1; 
    //}  
        
    if(event.keyCode == 188 && cursorPos<text.value.length){
      cursorPos -=1; 
    }   
     
    var strTemp = text.value;
    var startingSeparators = strTemp.split(",").length-1;
    if (event.keyCode == 188)
      startingSeparators -=1;

	text.value = text.value.replace(/\,$/,".").//replace ending comma with dot if the input finishes with comma
		replace(/^\./,"0.").//Replace dot with "0." if dot is the first char
		replace(/^0([^\.])/g,"$1").//Replace first char with nothing if it is zero and it is not followed by dot
		replace(/[^0-9,\.]/g,"").//Enything but 1,2,...,9 and dot are replaced with nothing!
		replace(/\./,"#").//Replace first ocurence of dot with #
		replace(/\./g,"").//Replace All the dots with nothing
		replace(/,/g,"").//Replace all Commas with nothing
		replace(/#/,".");//replace first  ocurence of # with comma
		


	// Try to find a matching (.) user might have given
	dec_pos = text.value.search(/\./);
	//alert(text.value + 'dec_pos: '+dec_pos);
	if (dec_pos == -1)
		dec_pos = text.value.length;
	// Reverse iterate the string and add every 2 digits a (,)
	// (digit grouping symbol)
	// alert(text.value+"\n"+text.value.substring(0,dec_pos+2))
	if (text.value.length > (dec_pos+3)) {
		//Allow only two decimal digits
		text.value = text.value.substring(0,dec_pos+3) 
	}		
	if (dec_pos > 13 ) { 
		text.value = text.value.substring(0,text.value.length-1);
		dec_pos--;
	}
        var separators = 0;
    for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (comma)
		text.value = text.value.substr(0, i) + "," + text.value.substr(i);
        separators +=1;
    }
    //alert(cursorPos+"  "+separators +"  "+startingSeparators );
    if (separators >= startingSeparators)
      cursorPos = cursorPos+separators -startingSeparators; 
    else{
      if (event.keyCode==190)
        cursorPos -=1; 
    }  
    
    if ((separators < startingSeparators)&&(event.keyCode==8 || event.keyCode==46))
      cursorPos -=1; 
      
    if (text.value.indexOf(".")>0 && text.value.indexOf(".")<(cursorPos-1) && event.keyCode==190)
      cursorPos -=1; 
	text.setSelectionRange(cursorPos, cursorPos); 
}





function commafy(text,event) {
    //alert(event.keyCode);
	// do not execute on left, right arrows
    // 27  = escape
	if ((event.keyCode == 39) || (event.keyCode == 37)){
	  return;
	}

    var cursorPos = text.selectionStart;

    var tmpValue = text.value;   
    for (j = 0; j<tmpValue.length ; j++) {
      //alert(cursorPos+"   "+event.keyCode+"   "+tmpValue.charAt(j));
      if (("0123456789,.").indexOf(tmpValue.charAt(j)) == -1){
        text.value = text.value.replace(tmpValue.charAt(j),"")
        cursorPos -= 1;
      }
    }

    //alert(cursorPos);
    //48-57 digits
    //190 period
    //188 comma
    //8 back space
    //46 delete
    //if ((event.keyCode<48 || event.keyCode > 57)&&
    //    (event.keyCode!= 8)&&
    //    (event.keyCode != 46)&&
    //    (event.keyCode != 188)&&
    //    (event.keyCode != 190)){
    //  cursorPos -=1; 
    //}  
    
    //alert(cursorPos);    
    
    if(event.keyCode == 190 && cursorPos<text.value.length){
      cursorPos -=1; 
    }    
    
    //alert(cursorPos);    
    var strTemp = text.value;
    var startingSeparators = strTemp.split(".").length-1;
	
    //alert(startingSeparators);

	text.value = text.value.replace(/\.$/,",").//replace ending dot with comma if the input finishes with dot
		replace(/^,/,"0,").//Replace comma with "0," if comma is the first char
		replace(/^0([^,])/g,"$1").//Replace first char with nothing if it is zero and it is not followed by comma
		replace(/[^0-9,\,]/g,"").//Enything but 1,2,...,9 and comma are replaced with nothing!
		replace(/,/,"#").//Replace first ocurence of comma with #
		replace(/,/g,"").//Replace All the commas with nothing
		replace(/#/,",")//replace first  ocurence of # with comma
	// Try to find a matching (,) user might have given
	dec_pos = text.value.search(",");
	if (dec_pos == -1)
		dec_pos = text.value.length;
	// Reverse iterate the string and add every 2 digits a (.)
	// (digit grouping symbol)
	// alert(text.value+"\n"+text.value.substring(0,dec_pos+2))
	if (text.value.length > (dec_pos+3)) {
		//Allow only two decimal digits
		text.value = text.value.substring(0,dec_pos+3) 
	}		
	if (dec_pos > 13 ) { 
		text.value = text.value.substring(0,text.value.length-1);
		dec_pos--;
	}
        var separators = 0;
	for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (dot)
		text.value = text.value.substr(0, i) + "." + text.value.substr(i);
        separators +=1;
	}
    //alert(cursorPos+"  "+separators +"  "+startingSeparators );
    if (separators >= startingSeparators)
      cursorPos = cursorPos+separators -startingSeparators; 
    else{
      if (event.keyCode==188)
        cursorPos -=1; 
    }    
    if ((separators < startingSeparators)&&(event.keyCode==8 || event.keyCode==46))
      cursorPos -=1; 

    //alert(cursorPos );
    if (text.value.indexOf(",")>0 && text.value.indexOf(",")<(cursorPos-1) && event.keyCode==188)
      cursorPos -=1; 
    //alert(cursorPos );
	text.setSelectionRange(cursorPos,cursorPos );
}




function commafyEnOld(text) {
	// Ignore cursor movement
	//	if ((event.keyCode >= 33 && event.keyCode <= 46) || (event.keyCode == 9) || (event.keyCode == 16))
	//	return;
	// Dump the dot and alpha characters -- allow only numeric
	
	text.value = text.value.replace(/\,$/,".").//replace ending comma with dot if the input finishes with comma
		replace(/^\./,"0.").//Replace dot with "0." if dot is the first char
		replace(/^0([^\.])/g,"$1").//Replace first char with nothing if it is zero and it is not followed by dot
		replace(/[^0-9,\.]/g,"").//Enything but 1,2,...,9 and dot are replaced with nothing!
		replace(/\./,"#").//Replace first ocurence of dot with #
		replace(/\./g,"").//Replace All the dots with nothing
		replace(/,/g,"").//Replace all Commas with nothing
		replace(/#/,".")//replace first  ocurence of # with comma
		
	// Try to find a matching (.) user might have given
	dec_pos = text.value.search(/\./);
	//alert(text.value + 'dec_pos: '+dec_pos);
	if (dec_pos == -1)
		dec_pos = text.value.length;
	// Reverse iterate the string and add every 2 digits a (,)
	// (digit grouping symbol)
	// alert(text.value+"\n"+text.value.substring(0,dec_pos+2))
	if (text.value.length > (dec_pos+3)) {
		//Allow only two decimal digits
		text.value = text.value.substring(0,dec_pos+3) 
	}		
	if (dec_pos > 13 ) { 
		text.value = text.value.substring(0,text.value.length-1);
		dec_pos--;
	}
	for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (comma)
		text.value = text.value.substr(0, i) + "," + text.value.substr(i);
	}
}


function commafyOld(text) {
	// Ignore cursor movement
	//	if ((event.keyCode >= 33 && event.keyCode <= 46) || (event.keyCode == 9) || (event.keyCode == 16))
	//	return;
	// Dump the dot and alpha characters -- allow only numeric
	text.value = text.value.replace(/\.$/,",").//replace ending dot with comma if the input finishes with dot
		replace(/^,/,"0,").//Replace comma with "0," if comma is the first char
		replace(/^0([^,])/g,"$1").//Replace first char with nothing if it is zero and it is not followed by comma
		replace(/[^0-9,\,]/g,"").//Enything but 1,2,...,9 and comma are replaced with nothing!
		replace(/,/,"#").//Replace first ocurence of comma with #
		replace(/,/g,"").//Replace All the commas with nothing
		replace(/#/,",")//replace first  ocurence of # with comma
	// Try to find a matching (,) user might have given
	dec_pos = text.value.search(",");
	if (dec_pos == -1)
		dec_pos = text.value.length;
	// Reverse iterate the string and add every 2 digits a (.)
	// (digit grouping symbol)
	// alert(text.value+"\n"+text.value.substring(0,dec_pos+2))
	if (text.value.length > (dec_pos+3)) {
		//Allow only two decimal digits
		text.value = text.value.substring(0,dec_pos+3) 
	}		
	if (dec_pos > 13 ) { 
		text.value = text.value.substring(0,text.value.length-1);
		dec_pos--;
	}
	for (i = dec_pos-3; i>0; i-=3) {
		//Add grouping symbol (dot)
		text.value = text.value.substr(0, i) + "." + text.value.substr(i);
	}
}
