

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=450,height=500,left = 587,top = 0');");
}

function calculateTotal(frm) {

	var order_total = 0;
	var unique_articles_total = 0;
	var order_articles = "\n";

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

	// Get the current field
	form_field = frm.elements[i]

	// Get the fields name
	form_name = form_field.name

	// Is it a "product" field?
	if (form_name.substring(0,3) == "Ex_") {

	    // If so, extract the price from the name
	    item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

	    // Get the quantity
	    if (!isInteger(form_field.value)) {
	    	alert("Bitte geben Sie eine Zahl zwischen 1 und 99 ein, um den Titel zu bestellen.");
	    }
	    
	    
	    item_quantity = parseInt(form_field.value);
	    
	    // Update the order total
	    if (item_quantity >= 0) {
	    	unique_articles_total += 1;
		order_total += item_quantity * item_price;
		order_articles += item_quantity + "_" + form_name + "\n";
	    }
	}
     }

document.step2.total.value = round_decimals(order_total, 2);
document.step2.bestellung.value = order_articles;
document.step2.bestellung.rows = unique_articles_total+1;

}

function isInteger(val){
	
	if (isBlank(val)){return true;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
}

function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
}

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)

	return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

	// If no, then all decimal places will be padded with 0s
	decimal_part_length = 0

	// If decimal_places is greater than zero, tack on a decimal point
	value_string += decimal_places > 0 ? "." : ""
    }
    else {

	// If yes, then only the extra decimal places will be padded with 0s
	decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

	// Pad the string with 0s
	for (var counter = 1; counter <= pad_total; counter++)
	    value_string += "0"
	}

    return value_string;
}

function checkSubmit(frm) {

	// Run through all the form fields
	for (var i=0; i < frm.elements.length; ++i) {

		// Get the current field
		form_field = frm.elements[i]

		// Get the fields name
		form_name = form_field.name

		// Is it a "product" field?
		if (form_name.substring(0,6) == "anzahl") {

			// If so, extract the price from the name
			item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

			// if no ordering of a title is wanted, make sure the title does not appear in the order list with value 0
			if (form_field.value == 0 || form_field.value == "" || form_field.value == null || form_field.value == "NaN") {
				removeField(form_field);
			} else {
				// if there is a title that should be ordered, no special handling is necessary
			}

		}
	}

	frm.submit();
}


function checkAndSubmit(language) {

		
	if (language == 'en') {
		nameBlank = "Name is empty. Please enter a name";
		orderBlank = "Order is empty. Please select publications to order.";
		addressBlank = "Address is empty. Please enter an address.";
		cityBlank = "Zipcode and City are empty. Please enter them.";
		emailBlank = "Email is empty or invalid. Please enter an email-address.";
		antiSpam = "Type in something else. Value not allowed.";
	} else {
		nameBlank = "Name ist leer. Bitte geben Sie einen Namen ein.";
		orderBlank = "Bestellung ist leer. Bitte wählen Sie Publikationen aus.";
		addressBlank = "Adresse ist leer. Bitte geben Sie eine Adresse ein.";
		cityBlank = "PLZ, Ort ist leer. Bitte geben Sie diese ein.";
		emailBlank = "Email ist leer oder ungültig. Bitte geben Sie eine Email-Adresse ein.";
		antiSpam = "Wert ist nicht erlaubt. Geben Sie einen anderen Wert ein.";

	}
	
	if (document.step2.bestellung.value.length < 10) {
		alert(orderBlank);
		return;
	}
	

	if (document.step2.name.value.length < 1) {
		alert(nameBlank);
		return;
	}
	
	if (document.step2.adresse.value.length < 1) {
		alert(addressBlank);
		return;
	} 
	
	if (document.step2.ort.value.length < 1) {
		alert(cityBlank);
		return;
	}
	

	if (document.step2.email.value.length < 1) {
		alert(emailBlank);
		return;
	}
	
	if ((document.step2.email.value.indexOf(".") > 0) && (document.step2.email.value.indexOf("@") > 0)) {
		//correct 
	} else {
		alert(emailBlank);
		return;
	}
	

	// if fields are filled, submit	

	document.step2.submit();

}



function showDetail(title, text, img){ 

	
	width = 550; 
	height = 426; 

	if(window.innerWidth){ 
		LeftPosition =(window.innerWidth-width)/2; 
		TopPosition =((window.innerHeight-height)/4)-50; 
	} else { 
		LeftPosition =(parseInt(window.screen.width)-   width)/2; 
		TopPosition=((parseInt(window.screen.height)-height)/2)-50; 
	} 
	
	
        
	attr = 'resizable=yes,statusbar=no,scrollbars=yes,width=' + width + ',height=' + height + ',screenX=300,screenY=200,left=' + LeftPosition + ',top=' + TopPosition;
	id = new Date().getTime();

	popWin=window.open(null, id, attr); 

	popWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
	popWin.document.write('<html><head>'); 
	popWin.document.write('	<base href="http://www.tibet-institut.ch/content/tir/de/"> <!-- to enable preview before uploading -->');
	popWin.document.write('	<title>' + title + ' &lt; Shop Publikationen &lt; Tibet-Institut Rikon</title>');
	popWin.document.write('	<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">');
	popWin.document.write('	<link rel="stylesheet" type="text/css" href="../../../style/css/shop-detail-screen.css" media="screen">');
	popWin.document.write('	<link rel="stylesheet" type="text/css" href="../../../style/css/shop-detail-print.css" media="print">');
	popWin.document.write('	<meta http-equiv="Content-Style-Type" content="text/css">');
	popWin.document.write('</head>');
	popWin.document.write('<body>');
	popWin.document.write('    <h1>' + title + '</h1>');
	popWin.document.write('    <p>');
	popWin.document.write('		' + text);
	popWin.document.write('    </p>');
	if (img != "") {
		popWin.document.write('    <img src="' + img + '" width="500">');
	}
	popWin.document.write('</body>');
	popWin.document.write('</html>');
} 


function checkAndSubmitEvent(language) {

		
	if (language == 'en') {
		nameBlank = "Name is empty. Please enter a name";
		addressBlank = "Address is empty. Please enter an address.";
		cityBlank = "Zipcode and City are empty. Please enter them.";
		emailBlank = "Email is empty or invalid. Please enter an email-address.";
		antiSpam = "Type in something else. Value not allowed.";
	} else {
		nameBlank = "Name ist leer. Bitte geben Sie einen Namen ein.";
		addressBlank = "Adresse ist leer. Bitte geben Sie eine Adresse ein.";
		cityBlank = "PLZ, Ort ist leer. Bitte geben Sie diese ein.";
		emailBlank = "Email ist leer oder ungültig. Bitte geben Sie eine Email-Adresse ein.";
		antiSpam = "Wert ist nicht erlaubt. Geben Sie einen anderen Wert ein.";

	}
	
	if (document.step.email.value.length < 1) {
		alert(emailBlank);
		return;
	}
	
	if ((document.step.email.value.indexOf(".") > 0) && (document.step.email.value.indexOf("@") > 0)) {
		//correct 
	} else {
		alert(emailBlank);
		return;
	}

	if (document.step.name.value.length < 1) {
		alert(nameBlank);
		return;
	}
	
	if (document.step.adresse.value.length < 1) {
		alert(addressBlank);
		return;
	} 
	
	if (document.step.ort.value.length < 1) {
		alert(cityBlank);
		return;
	}
	



	// if fields are filled, submit	

	document.step.submit();

}

function checkAndSubmitDonour(language) {

		
	if (language == 'en') {
		nameBlank = "Name is empty. Please enter a name";
		addressBlank = "Address is empty. Please enter an address.";
		cityBlank = "Zipcode and City are empty. Please enter them.";
	} else {
		nameBlank = "Name ist leer. Bitte geben Sie einen Namen ein.";
		addressBlank = "Adresse ist leer. Bitte geben Sie eine Adresse ein.";
		cityBlank = "PLZ, Ort ist leer. Bitte geben Sie diese ein.";
	}
	
	

	if (document.step.name.value.length < 1) {
		alert(nameBlank);
		return;
	}
	
	if (document.step.adresse.value.length < 1) {
		alert(addressBlank);
		return;
	} 
	
	if (document.step.ort.value.length < 1) {
		alert(cityBlank);
		return;
	}
	
	// if fields are filled, submit	

	document.step.submit();

}

function tweak(formname) {
	newDestination = "http://" + get1() + get2();
	document.forms[formname].action = newDestination;
}

function get1() {
	return "www.tibet-institut.ch/c";
}

function get2() {
	return "gi-bin/post" + "x.plx".substring(1,4);
}