// 2009/12 aw

// how to check for defined/declared variables:
//   if (window.variable != "undefined") {}
//   if (typeof(variable) != "undefined") {}


var domainError = new Array(
	"",
	"Für diesen Tarif sind nur Domains mit der Endung .de erlaubt!",
	"Die Domain ist bereits in Ihrer Liste der zu buchenden Domains vorhanden!",
	"Sie haben das Limit an hinzubuchbaren Domains für diesen Tarif erreicht!",
	"Die Domain enthält ungültige Zeichen!"
);


function orderTarif(id) {
	location.href = "https://www.artfiles.de/order/add_tarif.html?id=" + id;
}


function removeFromMerkliste(domain) {
	$.ajax({
		async:		true,
		type:		"GET",
		url:		"/order/remove_from_merkliste.html",
		data:		"domain=" + domain,
		error:		function(req, error, exception) { orderFailure() },
		success:	function() {
						var domainID = domain.replace(/\./g, "_");
						$("#"+domainID).hide();
						reload_merkliste();
					}
	});
}


function addDomainsChecked(form, parentID) {
	try {
		if (document.forms[form].elements["domain_reg"].checked) {
			addDomainCart(document.forms[form].elements["domain_reg"].value, 'REG', parentID);
		}
	} catch(e) {};

	try {
		if (document.forms[form].elements["domain_kk"].checked) {
			addDomainCart(document.forms[form].elements["domain_kk"].value, 'KK', parentID);
		}
	} catch(e) {};
	
	return true;
}


function addDomainRel(domain, idParent) {
	if (checkSyntax(domain, "domain")) {
		var idDomain = addDomain(domain, 'KK', idParent);
		if (idDomain) {
			var input = document.createElement("input");
			$(input).attr({ 'type':'hidden', 'name':idDomain+'.regtype', 'value':'KK' });
			document.getElementById(idDomain).appendChild(input);
		}
	} else {
		alert("Fehler: Die Domain '" + domain + "' enthält ungültige Zeichen!");
	}
}


function addDomain(domain, regType, idParent) {
	var idDomain = null;
	var idParentNum = null;
   	idParent = new String(idParent || idParentGlobal);
	regType = regType || "REG";
	domain = domain.toLowerCase();

	if (idParent != null) {
		if (idParent.search(/^\d+$/) >= 0) {
			idParentNum = idParent;
			idParent = "domains_" + idParent;
		} else {
			idParent.match(/^domains_(\d+)$/);
			idParentNum = RegExp.$1;
		}
	}

	$.ajax({
		async:		true,
		type:		"GET",
		url:		"/order/add_domain.html",
		data:		"domain=" + domain + "&regtype=" + regType + "&id_parent=" + idParentNum,
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) {
						var parts = response.split(":");
						var errCode = parts[0];
						idDomain = parts[1];
						var domFee = parts[2];

						if (errCode > 0) {
							alert(domainError[errCode]);
						} else {
							var tr = document.createElement("tr");
							$(tr).attr('id', idDomain);

							// column domain name
							var td1 = document.createElement("td");
							$(td1).attr('id', idDomain+"_domain");
							$(td1).attr({ 'class':'no_border_no_hover', 'valign':'top' });	//valign hinzugefuegt 20100916kg
							$(td1).css({ 'width':'300px', 'color':'#444444', 'font-size':'1.2em', 'padding-left':'20px' });
							td1.appendChild(document.createTextNode(domain));

							// column domain fee
							var td2 = document.createElement("td");
							$(td2).attr('id', idDomain+"_fee");
							$(td2).attr({ 'class':'no_border_no_hover', 'valign':'top' });	//valign hinzugefuegt 20100916kg
							$(td2).css({ 'color':'#444444', 'font-size':'1.2em', 'padding-left':'20px', 'text-align':'right' });
							td2.appendChild(document.createTextNode(domFee));

							// column remove link
							var td3 = document.createElement("td");
							$(td3).attr({ 'class':'no_border_no_hover', 'valign':'top' });	//valign hinzugefuegt 20100916kg
							$(td3).css({ 'text-align':'right', 'color':'#444444', 'font-size':'1.2em', 'padding-right':'20px' });
							var link = document.createElement("a");
							$(link).attr('href', '#');
							$(link).bind("click", function(e) { removeDomain(idDomain); return false });
							link.appendChild(document.createTextNode("aus der Bestellung entfernen"));
							td3.appendChild(link);

							tr.appendChild(td1);
							tr.appendChild(td2);
							tr.appendChild(td3);

							document.getElementById(idParent).appendChild(tr);

							$("#nodomains").css({ 'display':'none', 'visibility':'collapse' });
							$("#"+idDomain).css({ 'display':'table-row', 'visibility':'visible' });

							//20100924kg
							window.setDomainOrderStatus(domain);	
							window.reloadBestellteDomains();
							window.getAvailableDomains();
							window.getInclDomains();
						}
					}
	});

	return idDomain;
}


function addDomainCart(domain, regType, idParent) {
	var idDomain = null;
	var idParentNum = null;
   	idParent = new String(idParent || idParentGlobal);
	regType = regType || "REG";
	domain = domain.toLowerCase();

	if (idParent != null) {
		if (idParent.search(/^\d+$/) >= 0) {
			idParentNum = idParent;
			idParent = "domains_" + idParent;
		} else {
			idParent.match(/^domains_(\d+)$/);
			idParentNum = RegExp.$1;
		}
	}
	
	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/add_domain.html",
		data:		"domain=" + domain + "&regtype=" + regType + "&id_parent=" + idParentNum
	});
	
	return true;
}

function removeDomain(id) {
	window.reloadBestellteDomains();
	window.getInclDomains();

	$.ajax({
		async:		true,
		type:		"GET",
		url:		"/order/remove_domain.html",
		data:		"id=" + id,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) {
						var domainsRemoved = response;
						if (domainsRemoved >= 1) {
							var domainsLeft = getOrderedDomains();
							if (domainsLeft == null || domainsLeft < 1) {
								$("#nodomains").css({ 'display':'table-row', 'visibility':'visible' });
							}
							$("#"+id).remove();
						}
					}
	});
    //20100924kg
    var deleted_domain = document.getElementById(id+"_domain").innerHTML;
    deleted_domain = deleted_domain.replace(/\s/g, "");
    window.bestell_domains[deleted_domain] = 0;
    window.changeInclDomain(deleted_domain, id);
}


function addFeatures(idFeature, amount, idParent, name, unit) {
	for (var i=1; i<=amount; i++) {
		addFeature(idFeature, idParent, name, unit);
	}
}


function addFeature(idFeature, idParent, name, unit) {
	var id = null;
	var idParentNum = null;
	if (unit == null) {
		unit = '';
	}
	idParent = new String(idParent || idParentGlobal);

	if (idParent != null) {
		if (idParent.search(/^\d+$/) >= 0) {
			idParentNum = idParent;
			idParent = "features_" + idParent;
		} else {
			idParent.match(/^features_(\d+)$/);
			idParentNum = RegExp.$1;
		}
	}

	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/add_feature.html",
		data:		"feature_id=" + idFeature + "&id_parent=" + idParentNum + "&name=" + name + "&unit=" + unit,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) {
						var parts = response.split(":");
						id = parts[0];
						var featFee = parts[1];

						if (id > 0) {
							var tr = document.createElement("tr");
							$(tr).attr('id', id);

							// feature name column
							var td1 = document.createElement("td");
							$(td1).attr({ 'class':'no_border_no_hover' });
							$(td1).css({ 'width':'300px', 'color':'#444444', 'font-size':'1.2em', 'padding-left':'20px' });
							td1.appendChild(document.createTextNode(name));

							// feature fee column
							var td2 = document.createElement("td");
							$(td2).attr({ 'class':'no_border_no_hover' });
							$(td2).css({ 'color':'#444444', 'font-size':'1.2em', 'padding-left':'20px', 'text-align':'right' });
							td2.appendChild(document.createTextNode(featFee + " €"));

							// remove link column
							var td3 = document.createElement("td");
							$(td3).attr({ 'class':'no_border_no_hover' });
							$(td3).css({ 'text-align':'right', 'color':'#444444', 'font-size':'1.2em', 'padding-right':'20px' });
							var link = document.createElement("a");
							$(link).attr('href', '#');
							$(link).bind("click", function(e) { removeFeature(id); return false });
							link.appendChild(document.createTextNode("aus der Bestellung entfernen"));
							td3.appendChild(link);

							tr.appendChild(td1);
							tr.appendChild(td2);
							tr.appendChild(td3);
							document.getElementById(idParent).appendChild(tr);

							$("#nofeatures").css({ 'display':'none', 'visibility':'collapse' });
							$("#"+id).css({ 'display':'table-row', 'visibility':'visible' });
						}
					}
	});

	return id;
}


function removeFeature(id) {
	$.ajax({
		async:		true,
		type:		"GET",
		url:		"/order/remove_feature.html",
		data:		"id=" + id,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) {
						var featuresRemoved = response;
						if (featuresRemoved >= 1) {
							var featuresLeft = getOrderedFeatures();
							if (featuresLeft == null || featuresLeft < 1) {
								$("#nofeatures").css({ 'display':'table-row', 'visibility':'visible' });
							}
							$("#"+id).remove();
						}
					}
	});
}


function setParentTarif(id, tarif) {
	$.ajax({
		async:		true,
		type:		"GET",
		url:		"/order/set_parent.html",
		data:		"id=" + id + "&tarif=" + tarif,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() }
	});
}


function getOrderedDomains() {
	var orderedDomains = null;

	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/get_orders.html",
		data:		"item_type=domain",
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) { orderedDomains = response }
	});

	return orderedDomains;
}


function getOrderedFeatures() {
	var orderedFeatures = null;

	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/get_orders.html",
		data:		"item_type=feature",
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) { orderedFeatures = response }
	});

	return orderedFeatures;
}


function radioSubmit(form, rname) {
	var suffix;
	for (var i=0; i<document.forms[form].elements.length; i++) {
		if (document.forms[form].elements[i].name == rname) {
			if (document.forms[form].elements[i].checked) {
				suffix = document.forms[form].elements[i].value;
				break;
			}
		}
	}

	document.forms[form].action = "step2_" + suffix + ".html";
	document.forms[form].method = "post";
	document.forms[form].elements[rname].value = suffix;
	document.forms[form].submit();
}


function busy() {
	$("*").css("cursor", "progress");
}


function idle() {
	$("*").css("cursor", "auto");
}


function orderFailure(msg, dbg) {
	if (msg) {
		alert("Ein Fehler ist aufgetreten. Bitte wenden Sie sich an support@artfiles.de\n\n" + msg);
		if (dbg) {
			console.log("%s", msg);
		}
	} else {
		alert("Ein Fehler ist aufgetreten. Bitte wenden Sie sich an support@artfiles.de");
	}
}


function checkSyntax(value, type, size) {
	/*try {
		value = document.getElementById(value).value;
	} catch(e) {}*/

	if (value && ! value.match(/^\s+$/)) {
		if (type == "char") {
			if (value.match(/^[\-\wäöüÄÖÜß]+$/)) {
				if (size && value.length > size) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		} else if (type == "char_ws") {
			if (value.match(/^[\-\s\wäöüÄÖÜß]+$/)) {
				if (size && value.length > size) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		} else if (type == "widechar") {
			if (value.match(/^[\.\,\-\s\wäöüÄÖÜß&]+$/)) {
				if (size && value.length > size) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		} else if (type == "number") {
			if (value.match(/^\d+$/i)) {
				if (size && value.length > size) {
					return false;
				} else {
					return true;
				}
			} else {
				return false;
			}
		} else if (type == "phone") {
			value.replace(/[^\d+]/g, "");
			if (size && value.length < size) {
				return false;
			} else {
				return true;
			}
		} else if (type == "date") {
			if (value.match(/^\d{1,2}\D\d{1,2}\D\d{2,4}$/i)) {
				return true;
			} else {
				return false;
			}
		} else if (type == "domain") {
			if (value.match(/^[a-zäöüÄÖÜ\d\-]+\.[a-z]+(\.[a-z]+)?$/i) && !value.match(/^www\./i)) {
				return true;
			} else {
				return false;
			}
		} else if (type == "email") {
			if (value.match(/^[^\@]+\@[a-z0-9-]+(\.[a-z0-9-]+)+$/i)) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	} else {
		return false;
	}
}


function checkSyntaxBLZ(blz) {
	var valid = null;
	var bank = null;

	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/check_blz.html",
		data:		"blz=" + blz,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) {
						var parts = response.split(":");
						valid = parts[0];
						bank = parts[1];				
		}
	});

	$("#cform_bank").remove();
	if (valid == 1) {
		var input = document.createElement("input");
		$(input).attr({ 'type':'hidden', 'name':'cform_bank', 'id':'cform_bank', 'value':bank });
		document.getElementById("order").appendChild(input);
		return 1;
	} else {
		return 0;
	}
}


function checkSyntaxKTO(blz, kto) {
	var valid = null;

	$.ajax({
		async:		false,
		type:		"GET",
		url:		"/order/check_kto.html",
		data:		"blz=" + blz + "&kto=" + kto,
		dataType:	"text",
		error:		function(req, error, exception) { orderFailure() },
		success:	function(response) { valid = response }
	});

	if (valid > 0) {
		return 1;
	} else {
		return 0;
	}
}


function setError(id) {
	error.push(id);
	$("#errorBox").css({ 'display':'block', 'visibility':'visible' });
	$("#"+id).parent().addClass("syntaxError");
	$("#"+id).parent().prev().addClass("syntaxError");
	$("#"+id).parent().next().addClass("syntaxError");
}


function toggleList(id, varName, isFolded) {
	var list = document.getElementById(id).getElementsByTagName("li")
	for (var i=0; i<list.length; i++) {
		var li = list[i];
		if (isFolded) {
			$(li).css({ 'display':'block', 'visibility':'visible' });
		} else {
			$(li).css({ 'display':'none', 'visibility':'collapse' });
		}
	}

	if (varName == "contactFold") {
		contactFold = contactFold ? 0 : 1;
	} else if (varName == "bankFold") {
		bankFold = bankFold ? 0 : 1;
	}
}


function toggleBankContacts(type) {
	if (type == "rechnung") {
		$("#row_cform_ktoowner").hide();
		$("#row_cform_blz").hide();
		$("#row_cform_ktonr").hide();
	} else {
		$("#row_cform_ktoowner").show();
		$("#row_cform_blz").show();
		$("#row_cform_ktonr").show();
	}
}


function toggleHelp(id) {
	if (rowSet[id] == 'show') {
		rowSet[id] = 'hide';
		$("#"+id).css('display', 'none');
	} else {
		/*
		for (var i in rowSet) {
			rowSet[i] = 'hide';
			$("#"+i).css('display', 'none');
		}
		*/
		rowSet[id] = 'show';
		$("#"+id).css('display', 'table-row');
	}
}


function domRedirect(form) {
	if (checkSyntax(document.forms[form].elements["domain"].value, "domain") || checkSyntax(document.forms[form].elements["domain_rel"].value, "domain")) {
	//if (document.forms[form].elements["domain"].value || document.forms[form].elements["domain_rel"].value) {
		document.forms[form].action = "step1_domcheck.html";
		document.forms[form].method = "post";
		document.forms[form].submit();
	} else {
		return true;
	}
}
