// Copyright 2008, Tobias Deutsch
// eMail: tobias-copyright@strix.at
// Homepage: www.strix.at

function REMOVEQUOTES(text) {
	while (text.search(/"/) != -1) {
		text = text.replace('"', '');
	}
	
	return text;
}

String.prototype.leftTrim = function () {
    return (this.replace(/^\s+/,""));
};
String.prototype.rightTrim = function () {
    return (this.replace(/\s+$/,""));
};
//kombiniert "leftTrim" und "rightTrim";
String.prototype.basicTrim = function () {
    return (this.replace(/\s+$/,"").replace(/^\s+/,""));
};
  
function YYYYMMDDtoDDMMYYYY(date) { //20090123 -> 23.01.2009
	if ((date.length!=8) || (isNaN(date))) {
		return "00.00.0000";
	} else {
		return date.substring(6,9)+"."+date.substring(4,6)+"."+date.substring(0,4);
	}
}

function YYYY_MM_DDtoDDMMYYYY(date) { //2009-01-23 -> 23.01.2009

	if ((date.length!=10)) {
		return "00.00.0000";
	} else {
		return date.substring(8,10)+"."+date.substring(5,7)+"."+date.substring(0,4)
	}
}

function DDMMYYtoDDMMYYYY(date) { //23.01.09 -> 23.01.2009
	if ((date.length!=8)) {
		return "00.00.0000";
	} else {
		return date.substring(0,6)+".20"+date.substring(6,8);
	}
}

function COMMAtoDOT(value) {
	value = value.replace(".",""); //remove 1000er dot. eg. 1.000,00- => 1000.00
	return value.replace(",",".");
}

function GENERATEQIFSPLITENTRY(date, amount, memo, category) {
	entry = "";
	entry = "D"+date+"\n";
	entry+= "T"+amount+"\n";
	entry+= "M"+memo+"\n";
	if (category.length > 0) {
		entry+="L"+category+"\n";
	}
	entry+= "^\n";
	
	return entry;
}

function GENERATEQIFHEADENTRY(date, amount, account, type) {
	entry = "";
	entry = "!Type:"+type+"\n";
	entry+= "D"+date+"\n";
	if (amount.length > 0) {
		entry+= "T"+amount+"\n";
	}
	entry+= "CX\n";
	entry+= "L["+account+"]\n";
	entry+= "^\n";
	
	return entry;
}

function openInNewWindow(text) {
	newwindow = window.open("","_blank");
	newwindow.document.open("text/qif","replace");
	newwindow.document.write(text);
	newwindow.document.close();
}

function PROCESSTEXT(text) {
	alert("PROCESSLINES not implemented!");
}


function CSV2QIF(text) {
	openInNewWindow( PROCESSTEXT( text ) );
}
