All pastes #2063675 Raw Edit

bill

public text v1 · immutable
#2063675 ·published 2011-05-18 15:59 UTC
rendered paste body
//This code was written by shrsv. This is worthless, and do not refer to it for the good of yourself because it's written by a complete non-professional
var total = 0; //the grand total
var numberTotal = 0; //total number of items
var enabled = 1; //if 1 adding items is enabled, if 0 disabled
var saved = -99; //if custom header details are saved are available through cookies(1) else (0)
var status = "Ready!" //set the messaage for the status bar. Do not change this if you do not know what you are doing!(If you neglect and edit the program might get broken)

function main() {

    if (enabled == 1) {
        if (document.getElementById("price").value == "" | document.getElementById("numbers").value == "" | document.getElementById("product").value == "") {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>Please make sure that you've entered every mandatory details in the above fields!";
        } else if (document.getElementById("product").value == "Enter a product") {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>Please make sure that you've entered a correct product name!";
        } else {
            document.getElementById("suggestions").style.backgroundColor = '#ffff80';
            document.getElementById("suggestions").style.color = '#003322';
            document.getElementById("suggestions").innerHTML = "<strong>Good!</strong> You are entering items correctly. Go on! When you are done, click <strong>Finish Billing</strong>";
            addUp();
            addRow("dataTable");
        }
    } else {
        document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
        document.getElementById("suggestions").style.color = 'white';
        document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>Click <strong>Undo Finish</strong> to add items again!";
    }
}

//checks if cookie exists on the user's machine
function cookieCheck(){
	var x = readCookie('company')
		if (x) {
			company = x;	// if thre is x, then assign it to company
			saved = 1;		//and also make sure that the variable is saved
		} else {
			saved = 0;		//else the it is not saved
		}

	var y = readCookie('place')
		if (y) {
			place = y;
		} 

	var z = readCookie('email')
		if (z) {
			email = z;
		} 

	var a = readCookie('website')
		if (a) {
			website = a;
			customHead();
	} else {
		customHeadDefault();
	}
}


function addUp() {
    var price = document.getElementById("price");
    var numbers = document.getElementById("numbers");

    price = parseFloat(price.value);
    numbers = parseFloat(numbers.value);

    var out = price * numbers;
    total += out;
	
	numberTotal += numbers;
	
	status = "Doing billing!"
	statusUpdate();
    
}

function addRow(tableID) {

    var product = document.getElementById("product");
    product = product.value;

    var numbers = document.getElementById("numbers");
    numbers = numbers.value;

    var price = document.getElementById("price");
    price = price.value;
    price = price * numbers;

    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0);
    cell1.innerHTML = rowCount;

    var cell2 = row.insertCell(1);
    var element1 = document.createElement("input");
    cell2.innerHTML = product;

    var cell3 = row.insertCell(2);
    cell3.innerHTML = numbers;

    //cell2.innerHTML = rowCount + 1;
    var cell4 = row.insertCell(3);
    cell4.innerHTML = price;
}

function finish() {
    if (enabled == 1) {
		status = "Billing finished!"
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        if (rowCount == 1) {
			
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>There is nothing to bill! Please enter some data to start billing.";
        } else {
            if (saved == 0) {
                document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
                document.getElementById("suggestions").style.color = 'white';
                document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>You haven't set any custom header. Please set it in the <strong>settings</strong> form below. Once set, it'll last for a week";
                toggleVis();
            } else if (saved == 1) {
                var table = document.getElementById("dataTable");

                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);

                var cell1 = row.insertCell(0);
                cell1.innerHTML = "";

                var cell2 = row.insertCell(1);
                var element1 = document.createElement("input");
                cell2.innerHTML = "<strong>TOTAL</strong>";

                var cell3 = row.insertCell(2);
                cell3.innerHTML = "<strong>" + Math.floor(numberTotal) + "</strong>";
                //cell2.innerHTML = rowCount + 1;
                var cell4 = row.insertCell(3);
                cell4.innerHTML = "<strong>" + total + "</strong>";

                document.getElementById("finish").disabled = true;
                document.getElementById("add").disabled = true;
                document.getElementById("remove").disabled = true;
                document.getElementById("undoFinish").disabled = false;
                document.getElementById("print").disabled = false;
                document.getElementById("set").disabled = true;

                document.getElementById("suggestions").style.backgroundColor = '#ffff80';
                document.getElementById("suggestions").style.color = '#003322';
                document.getElementById("suggestions").innerHTML = "<strong>Suggestions:</strong> What next? You can <strong>Undo Finish</strong>, to get back. You can <strong>Print</strong> the bill, Save the page as html, or hit <strong>Reset</strong> at the top in order to start another bill. Thanks!";
				
				
				statusUpdate();
                enabled = 0;
            }
        }
    } else {
        document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
        document.getElementById("suggestions").style.color = 'white';
        document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>The bill is already finished! Click <strong>Reset</strong> to start anew or <strong>Undo Finish</strong> to get back";
    }
}

function deleteRow() {
    if (enabled == 1) {
        var row = document.getElementById("row").value;
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
		parseInt(row);
        rowCount = rowCount - 1;
        if (row == 0) {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>It is not possible to delete the header row!";
        } else if (row > rowCount) {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>There are only " + rowCount + " rows. You've entered a higher value which does not exists";
        } else {
            var content = document.getElementsByTagName('td');
            total -= content[3 + (row * 4)].innerHTML;
            if (row == 1){
				numberTotal -= content[6].innerHTML;
				document.getElementById("dataTable").deleteRow(row);
				} else {
					row -= 1;
					numberTotal -= content[6 + (row * 4)].innerHTML;
					document.getElementById("dataTable").deleteRow(row+1);
					}
            reNumber();
			statusUpdate();
        }
    } else {
        document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
        document.getElementById("suggestions").style.color = 'white';
        document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>The billing has finished. If you want to delete rows then click <strong>Undo Finish</strong>!";
    }
}

function reNumber() {
    var table = document.getElementById("dataTable");
    var rowCount = table.rows.length;
    var content = document.getElementsByTagName('td');
    for (var i = 1; i <= rowCount - 1; i++) {
        content[i * 4].innerHTML = i;
    }
}

function undoFinish() {
    if (enabled == 0) {
        enabled = 1;
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        table.deleteRow(rowCount - 1);
        document.getElementById("finish").disabled = false;
        document.getElementById("add").disabled = false;
        document.getElementById("remove").disabled = false;
        document.getElementById("undoFinish").disabled = true;
        document.getElementById("print").disabled = true;
        document.getElementById("set").disabled = false;
		status = "Undid Finish. Ready again!"
		statusUpdate();
    } else {
        document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
        document.getElementById("suggestions").style.color = 'white';
        document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>You've already made an Undo. So you cannot again perform <strong>Undo Finish</strong>!";
    }
}

function disable() {
    document.getElementById("finish").disabled = false;
    document.getElementById("add").disabled = false;
    document.getElementById("remove").disabled = false;
    document.getElementById("undoFinish").disabled = true;
    document.getElementById("print").disabled = true;
    document.getElementById("undoFinish").disabled = true;
    document.getElementById("print").disabled = true;
    cookieCheck();
}

function rowDlg() {
    if (enabled == 1) {
        var table = document.getElementById("dataTable");
        var rowCount = table.rows.length;
        var row = prompt("Which row do you want to delete?:");
        parseInt(row);
        rowCount = rowCount - 1;
        if (row == 0) {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>It is not possible to delete the header row!";
        } else if (row > rowCount) {
            document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
            document.getElementById("suggestions").style.color = 'white';
            document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>There are only " + rowCount + " rows. You've entered a higher value which does not exists";
        } else {
            var content = document.getElementsByTagName('td');
            total -= content[3 + (row * 4)].innerHTML;
				if ( row == 1){
					numberTotal -= content[row*6].innerHTML;
					document.getElementById("dataTable").deleteRow(1);
				} else {
					row -= 1;
					numberTotal -= content[6 + (row * 4)].innerHTML;
					document.getElementById("dataTable").deleteRow(row+1);
					}
            reNumber();
			statusUpdate();
        }
    } else {
        document.getElementById("suggestions").style.backgroundColor = '#ff7f4c';
        document.getElementById("suggestions").style.color = 'white';
        document.getElementById("suggestions").innerHTML = "<strong>Warning:</strong>The billing has finished. If you want to delete rows then click <strong>Undo Finish</strong>!";
    }
}

function save() {
   
    if (saved == 0) {
        enabled = 1;
        document.getElementById("finish").disabled = false;
        document.getElementById("add").disabled = false;
        document.getElementById("remove").disabled = false;
        document.getElementById("undoFinish").disabled = true;
        document.getElementById("print").disabled = true;
        document.getElementById("product").disabled = false;
        document.getElementById("numbers").disabled = false;
        document.getElementById("price").disabled = false;

        var company = document.getElementById("company").value;
        var place = document.getElementById("place").value;
        var email = document.getElementById("email").value;
        var website = document.getElementById("website").value;
        createCookie('company', company, 7);
        createCookie('place', place, 7);
        createCookie('email', email, 7);
        createCookie('website', website, 7);
		
		setBanner(company, place, email, website);
        
        saved = 1;
        var settings = document.getElementById("settings");
        settings.style.display = "none";
        document.getElementById("suggestions").style.backgroundColor = '#ffff80';
        document.getElementById("suggestions").style.color = '#003322';
        document.getElementById("suggestions").innerHTML = "Your setting has been saved successfully!";
        if (status != "Ready!"  && status != "Doing billing!"){finish();}
    } else if (saved == 1){
        enabled = 1;
        document.getElementById("finish").disabled = false;
        document.getElementById("add").disabled = false;
        document.getElementById("remove").disabled = false;
        document.getElementById("undoFinish").disabled = true;
        document.getElementById("print").disabled = true;
        document.getElementById("product").disabled = false;
        document.getElementById("numbers").disabled = false;
        document.getElementById("price").disabled = false;

        var company = document.getElementById("company").value;
        var place = document.getElementById("place").value;
        var email = document.getElementById("email").value;
        var website = document.getElementById("website").value;
        createCookie('company', company, 7);
        createCookie('place', place, 7);
        createCookie('email', email, 7);
        createCookie('website', website, 7);
		setBanner(company, place, email, website);
        saved = 1;
        var settings = document.getElementById("settings");
        settings.style.display = "none";
        document.getElementById("suggestions").style.backgroundColor = '#ffff80';
        document.getElementById("suggestions").style.color = '#003322';
        document.getElementById("suggestions").innerHTML = "Your setting has been saved successfully!";
    } else{alert("something went wrong");}
}

function toggleVis() {
		
		
		enabled = 0;
    var settings = document.getElementById("settings");
    if (settings.style.display == "block") {
        settings.style.display = "none";
        document.getElementById("finish").disabled = false;
        document.getElementById("add").disabled = false;
        document.getElementById("remove").disabled = false;
        document.getElementById("undoFinish").disabled = true;
        document.getElementById("print").disabled = false;
        document.getElementById("product").disabled = false;
        document.getElementById("numbers").disabled = false;
        document.getElementById("price").disabled = false;
    } else {
        settings.style.display = "block";
        document.getElementById("settings").scrollIntoView();
        document.getElementById("finish").disabled = true;
        document.getElementById("add").disabled = true;
        document.getElementById("remove").disabled = true;
        document.getElementById("undoFinish").disabled = true;
        document.getElementById("print").disabled = true;
        document.getElementById("product").disabled = true;
        document.getElementById("numbers").disabled = true;
        document.getElementById("price").disabled = true;
    }
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function statusUpdate(){
	
	statusBar = document.getElementById("statusBar");
	statusBar.innerHTML = "<strong>Status: </strong>" + status+ " <strong>Total Rs:</strong> " + total + " <strong>Total Items:</strong> " + numberTotal;
}
function customHead(){
	document.getElementById("company").value = company;
	document.getElementById("place").value = place;
	document.getElementById("email").value = email;
	document.getElementById("website").value = website;
}

function customHeadDefault(){
	document.getElementById('company').value = "Microsoft";
	document.getElementById('place').value = "Redmond, Washington";
	document.getElementById('email').value = "billgates@microsoft.com";
	document.getElementById('website').value = "www.microsoft.com";
}

function setBanner(company, place, email, website){
	var banner = "<strong>" + company + "</strong>" + "<br />" + place + "<br />" + "<em>" + email + " | " + website + "</em>";
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	banner += "<br />Billed on: " + month + "/" + day + "/" + year;
    document.getElementById("title").innerHTML = banner;
}