if(document.cookie == '') {
	document.cookie = 'hasCookies=yes';
	if (document.cookie.indexOf('hasCookies=yes') != -1) hasCookies = true;
}
else hasCookies = true;

function setCookie (name, value, hours, path) {
	if (hasCookies) {
		if(hours) {
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) var numHours = hours;
			else if (typeof(hours) == 'number') var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
		}
		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'');
	}
}

function openWindow(url, name, width, height, scrollbar, menubar, toolbar, statusbar, resizable) {
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 3;
  
  toolbar	= toolbar	? 'yes' : 'no';
  menubar	= menubar	? 'yes' : 'no';
  statusbar	= statusbar	? 'yes' : 'no';
  scrollbar	= scrollbar	? 'yes' : 'no';
  resizable	= resizable	? 'yes' : 'no';

  window.open(url, name, 'width='+width+', height='+height+', left=' + left + ', top=' + top + ', toolbar=' + toolbar + ', menubar=' + menubar + ', status=' + statusbar + ', scrollbars=' + scrollbar + ', resizable=' + resizable);
}

function selectAll(form, itemName, mark) {
	for (i = 0; i < form.elements.length; i++) {
    	if (form.elements[i].name == itemName) {
			form.elements[i].checked = mark;
		}
	}
}

function isValidEmail(email) {
	var splitted = email.match("^(.+)@(.+)$");

	if(splitted == null)
		return false;

    if(splitted[1] != null) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if (splitted[1].match(regexp_user) == null)
			return false;
    }

    if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) {
				return false;
			}
		}
		return true;
	}
	return false;
}

function setSections(selectedSectionsId, selectedBrandsId) {
	sectionsId	= document.getElementById('sectionsId');
	brandsId	= document.getElementById('brandsId');

	if (selectedBrandsId == '') {
		selectedBrandsId = brandsId.options[brandsId.selectedIndex].value;
	}

	selectedIndex = -1;
	sectionsId.options.length = 0;

	if (sections[ selectedBrandsId ] != '') {
		for(i=0; i < sections[ selectedBrandsId ].length; i++) {
			sectionsId.options[ sectionsId.options.length ] = new Option(sections[ selectedBrandsId ][ i ][ 1 ], sections[ selectedBrandsId ][ i ][ 0 ]);
			if (selectedSectionsId == sections[ selectedBrandsId ][ i ][ 0 ]) {
				selectedIndex = sectionsId.options.length - 1;
			}
		}
	} else {
		sectionsId.options[ sectionsId.options.length ] = new Option('Keine Auswahl', 0);
	}


	if (selectedIndex != -1) {
		sectionsId.selectedIndex = selectedIndex;
	}
}

function setBrands(selectedSectionsId, selectedBrandsId) {
	categoriesId	= document.getElementById('categoriesId');
	brandsId		= document.getElementById('brandsId');

	if (selectedSectionsId == '') {
		selectedSectionsId = categoriesId.options[categoriesId.selectedIndex].value;
	}

	selectedIndex = -1;
	brandsId.options.length = 0;

	if (brands[ selectedSectionsId ] != '') {
		for(i=0; i < brands[ selectedSectionsId ].length; i++) {
			brandsId.options[ brandsId.options.length ] = new Option(brands[ selectedSectionsId ][ i ][ 1 ], brands[ selectedSectionsId ][ i ][ 0 ]);
			if (selectedBrandsId == brands[ selectedSectionsId ][ i ][ 0 ]) {
				selectedIndex = brandsId.options.length - 1;
			}
		}
	} else {
		brandsId.options[ brandsId.options.length ] = new Option('Keine Auswahl', 0);
	}

	if (selectedIndex != -1) {
		brandsId.selectedIndex = selectedIndex;
	}
}

function showHideDeliveryAddressBlock(element) {
	obj = document.getElementById('addressBlock');
	obj.style.display = (element.checked) ? 'block' : 'none';
}

function setFormAction(form, url) {
	form.action = url;
	form.submit();
}

function selectPaymentFields(payment) {

//	document.cart.bankName.disabled					= true;
//	document.cart.bankAccount.disabled				= true;
//	document.cart.bankCode.disabled					= true;
//	document.cart.cardsId.disabled					= true;
//	document.cart.cardNumber.disabled				= true;
//	document.cart.cardHolder.disabled				= true;
//	document.cart.cardIssue.disabled				= true;
//	document.cart.cardExpireMonth.disabled			= true;
//	document.cart.cardExpireYear.disabled			= true;

	switch (payment) {
		case '1':
			document.cart.bankName.disabled			= false;
			document.cart.bankAccount.disabled		= false;
			document.cart.bankCode.disabled			= false;
			break;
		case '2':
			document.cart.cardsId.disabled			= false;
			document.cart.cardNumber.disabled		= false;
			document.cart.cardHolder.disabled		= false;
			document.cart.cardIssue.disabled		= false;
			document.cart.cardExpireMonth.disabled	= false;
			document.cart.cardExpireYear.disabled	= false;
			break;
	}
}

var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr) {
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	
	if (pos1==-1 || pos2==-1){
//		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}

	return true
}

function clearSectionAndBranch() {
	document.getElementById('sectionsId').selectedIndex = 0;
	document.getElementById('brandsId').selectedIndex = 0;
}

function sendPurchases(form, languageCode) {
	if (isValidEmail(form.email.value)) {
		form.action = '/' + languageCode + '/purchases/send.html';
		form.submit();
	} else {
		alert('E-mail is not correct.');
	}
}


function getBrowserInfo() {
 var t,v = undefined;
 if (window.opera) t = 'Opera';
 else if (document.all) {
  t = 'IE';
  var nv = navigator.appVersion;
  var s = nv.indexOf('MSIE')+5;
  v = nv.substring(s,s+1);
 }
 else if (navigator.appName) t = 'Netscape';
 return {type:t,version:v};
}
 
function bookmark(a){
 var url = window.document.location;
 var title = window.document.title;
 var b = getBrowserInfo();
 if (b.type == 'IE' && 8 > b.version && b.version >= 4) window.external.AddFavorite(url,title);
 else if (b.type == 'Opera') {
  a.href = url;
  a.rel = "sidebar";
  a.title = url+','+title;
  return true;
 }
 else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
 else alert("Droken Sie auf CTRL-D, im die Seite zu Favoriten hinzuzufugen.");
 return false;
}
