// inserting via DOM fails in Safari 2.0 so applying brute force
document.write('<script type="text/javascript" src="'+path+'scripts/acimage.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/xmlparser.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/php.min.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/tiny_mce/tiny_mce.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/jquery/jquery-ui-1.7.custom.min.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/jquery/ui.datepicker-da.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/jquery/jquery.easing.1.3.js"><\/script>');
document.write('<script type="text/javascript" src="'+path+'scripts/jquery/jquery.lightbox-0.5.min.js"><\/script>');
document.write('<link type="text/css" href="'+path+'scripts/jquery/themes/smoothness/ui.all.css" rel="stylesheet" />');
document.write('<link type="text/css" href="'+path+'scripts/jquery/themes/smoothness/jquery.lightbox-0.5.css" rel="stylesheet" />');

//Aconti client prototype member. For parsing XML data received from Aconti server API
var xmlResponse;
var xmlObj;
var ssArray = new Array();
var sgArray = new Array();
var pitStop = 0;
var imgPref = 'http://live.aconti.dk/userdata/';
var imglarge;
var imgthumb;
var selFrm;
var cartvalue = 0, cartnet = 0;

$(document).ready(function() {
	var dtps = $('form input:text');
	
	for (var i=0;i<dtps.length;i++) {
		var dtformat = (typeof $(dtps[i]).attr('format') == 'undefined') ? 'mm/dd/yy' : $(dtps[i]).attr('format');
		
		if($(dtps[i]).attr('rel') == 'datepicker') {
			$(dtps[i]).datepicker({dateFormat:dtformat,firstDay:1});
		}
	}
	
	autoSS();
	autoSG();
	autoLG();
	autoLB();
	formValidation();
	
	$('#selcard').change(function(){
		var urel = document.location.href.split('&');
		var form_url = urel[0];
		
		if ($(this).val() == 'creditcard') {
		  form_url = form_url + '&ptype=cc';
			//document.location.href = urel[0] + '&ptype=cc';
		} else {
		  form_url = form_url + '&ptype=nc';
			//document.location.href = urel[0] + '&ptype=nc';
		}
		
		$('#cart_mode').attr('name', 'oc_mode');
		$('#shippingform').attr('action', form_url);
		document.forms["shippingform"].submit();
		
	});
	
	if (getCookie('catalog') == null) {
		window.open('http://www.forlagetscandinavia.dk/bogklubblad.php','win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,directories=no,location=no');
	}
});

function initEditor(opt) {
	opt.mode = (typeof opt.mode == 'undefined') ? 'basic' : opt.mode;
	opt.feature = (typeof opt.feature == 'undefined') ? 'web' : opt.feature;
	opt.toolbar = (typeof opt.toolbar == 'undefined') ? 'top' : opt.toolbar;
	opt.statusbar = (typeof opt.statusbar == 'undefined') ? 'bottom' : opt.statusbar;
	opt.resize = (typeof opt.resize == 'undefined') ? false : opt.resize;
	
	if (opt.mode == 'basic') {
		tinyMCE.init({
			// General options
			mode : "textareas",
			theme : "simple",
			plugins : "safari,spellchecker,iespell,paste,directionality,template"});
	} else if (opt.mode == 'full') {
		if (opt.feature == 'web') {
			tinyMCE.init({
				// General options
				mode : "exact",
				elements: "editor", 
				theme : "advanced",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				relative_urls : false,
				remove_script_host : false,
				convert_url : false,

				// Theme options
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl",
				theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote",
				theme_advanced_toolbar_location : opt.toolbar,
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : opt.statusbar,
				theme_advanced_resizing : opt.resize,

				// Replace values for the template plugin
				template_replace_values : {
					username : "Some User",
					staffid : "991234"
				}
				
			});
		} else if (opt.feature == 'web-lite') {
			tinyMCE.init({
				// General options
				mode : "exact",
				elements: "editor", 
				theme : "advanced",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				relative_urls : false,
				remove_script_host : false,
				convert_url : false,

				// Theme options
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,link,unlink,anchor,image,|,forecolor,backcolor",
				theme_advanced_buttons2 : "tablecontrols,|,hr,|,spellchecker,|,code",
				theme_advanced_buttons3 : "",
				theme_advanced_toolbar_location : opt.toolbar,
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : opt.statusbar,
				theme_advanced_resizing : opt.resize,

				// Replace values for the template plugin
				template_replace_values : {
					username : "Some User",
					staffid : "991234"
				}
			});
		} else if (opt.feature == 'office') {
			tinyMCE.init({
				// General options
				mode : "exact",
				elements: "editor", 
				theme : "advanced",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
				relative_urls : false,
				remove_script_host : false,
				convert_url : false,

				// Theme options
				theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak",
				theme_advanced_toolbar_location : opt.toolbar,
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : opt.statusbar,
				theme_advanced_resizing : opt.resize,

				// Replace values for the template plugin
				template_replace_values : {
					username : "Some User",
					staffid : "991234"
				}
			});

		}
	}
}

function singleAjax(element) {
	var elements = null;
	
	if (element == 'checkbox' || element == 'radio') {
		if (element == 'checkbox') {
			elements = $('form input:checkbox');
		} else {
			elements = $('form input:radio');
		}
		
		for (var i=0;i<elements.length;i++) {
			if (elements[i].checked) {
				if ($(elements[i]).attr('acWdgt' == 'ajax')) {
					callAJAX($(elements[i]).attr('acURL'),$(elements[i]).attr('acContainer'),$(elements[i]).attr('acMsg'));
					break;
				}
			}
		}
	} else if (element == 'select') {
		elements = $('form select');
		
		for (var i=0;i<elements.length;i++) {
			if ($(elements[i]).attr('acWdgt' == 'ajax')) {
				callAJAX(elements[i].value,$(elements[i]).attr('acContainer'),$(elements[i]).attr('acMsg'));
				break;
			}
		}
	}
}


function callAJAX(url,output,param) {
	param.validate = (typeof param.validate == 'undefined') ? false : param.validate;
	param.premsg = (typeof param.premsg == 'undefined') ? '' : param.premsg;
	param.postmsg = (typeof param.postmsg == 'undefined') ? '' : param.postmesg;
	
	
	if (param.validate) {
		if (confirm(param.premsg)) {
			new $.ajax({
				url:path+'/js/transport.php?url='+url+'&method=GET',
				type:'GET',
				success:function(transport){
					$('#'+output).html(transport);
					if (param.postmsg != '') {
						alert(param.postmsg);
					}
				}
			});
		}
	} else {
		new $.ajax({
			url:path+'/js/transport.php?url='+url+'&method=GET',
			type:'GET',
			success:function(transport){
				$('#'+output).html(transport);
				if (param.postmsg != '') {
					alert(param.postmsg);
				}
			}
		});
	}
	
}

function submitAJAX(id) {
	var frmelem = '#'+id;
	
	var method = $(frmelem).attr('method');
	var url = $(frmelem).attr('action');
	var output = $(frmelem).attr('acAJAXOutput');
	var subdata = '', lurl = '';
	
	var inputElements = $(frmelem+' :input');
	
	for (var i=0;i<inputElements.length;i++) {
		if (inputElements[i].type != 'submit' && inputElements[i].type != 'reset' && inputElements[i].type != 'button') {
			if (inputElements[i].type == 'radio' && inputElements[i].checked) {
				if (subdata=='') {
					subdata = '&' + inputElements[i].name + '=' + inputElements[i].value;
				} else {
					subdata += '&' + inputElements[i].name + '=' + inputElements[i].value;
				}
			} else if (inputElements[i].type == 'checkbox' && inputElements[i].checked) {
				if (subdata=='') {
					subdata = '&' + inputElements[i].name + '=' + inputElements[i].value;
				} else {
					subdata += '&' + inputElements[i].name + '=on';
				}
			} else {
				if (subdata=='') {
					subdata = '&' + inputElements[i].name + '=' + inputElements[i].value;
				} else {
					subdata += '&' + inputElements[i].name + '=' + inputElements[i].value;
				}
			}
		}
	}
	
	lurl = path+'/js/transport.php?url='+url+subdata+'&method='+method;
	
	$(frmelem).submit(function(){ return false;});
	
	new $.ajax({
		type:'GET',
		url:lurl,
		success:function(transport){		
			$('#'+output).html(transport);
		}});
}

function formValidation() {
	var frmId = '';
	var forms = $("form");
	
	for (var i=0;i<forms.length;i++) {
		if ($(forms[i]).attr("acAction") == 'validator') {
			frmId = $(forms[i]).attr('id');
			$(forms[i]).submit(function() {
				validateForm(frmId);
				return false;
			});
		}
	}
}

function validateForm(id) {
	var elems = null, isPassed = true;
	var frmid = "#"+id, elemid = '';
	
	elems = $(frmid + " :input");
	for (var j=0;j<elems.length;j++) {
		if (elems[j].type != 'submit' && elems[j].type != 'reset' && elems[j].type != 'button') {
			if ($(elems[j]).attr("acVdMode") == 'require') {
				if ($(elems[j]).attr("acVdtype") == 'email') {
					if (!validateField(elems[j],'email')) {
						elemid = '#'+$(elems[j]).attr("acVdlabel");
						$(elemid).css({'color':'#d00'});
						isPassed = false;
					} else {
						elemid = '#'+$(elems[j]).attr("acVdlabel");
						$(elemid).css({'color':'#6C6C6C'});
					}
				} else {
					if (!validateField(elems[j],'regular')) {
						elemid = '#'+$(elems[j]).attr("acVdlabel");
						$(elemid).css({'color':'#d00'});
						isPassed = false;
					} else {
						elemid = '#'+$(elems[j]).attr("acVdlabel");
						$(elemid).css({'color':'#6C6C6C'});
					}
				}
			}
		}
	}
	
	if (isPassed) {
		$(frmid).submit();
	}
}

function validateField(subelem, validationType) {
	if (subelem.type == 'select' || subelem.type == 'select-one') {
		if ($(subelem).attr("acVdSElem") == 'first') {
			if (subelem.selectedIndex < 0) {
				return false;
			} else {
				return true;
			}
		} else if ($(subelem).attr("acVdSElem") == 'second') {
			if (subelem.selectedIndex == 0) {
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	} else if (subelem.type == 'checkbox' || subelem.type == 'radio') {
		if (subelem.checked) {
			return true;
		} else {
			return false;
		}
	} else {
		if (trim(subelem.value) != '') {
			if (validationType == 'email') {
				if (strpos(subelem.value,'@') > 0 ) {
					var emAr = subelem.value.split('@');
					if (strpos(emAr[1],'.') > 0) {
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
}

function changeCountry(cname) {
	var htmldata = '', tmpval = 0, tmpship = 0;
	
	tmpval = cartvalue;
				
	if (cname.toLowerCase() == 'denmark' || cname.toLowerCase() == 'danmark') {
		htmldata = '<option value="select">v�lg betalingsm�de...</option><option value="creditcard">Dankort</option>';
		htmldata += '<option value="giro">GIRO kort</option>';
					
		$('#selcard').html(htmldata);
					
		htmldata = "Forsendelse til Danmark uanset v�gt";
		$('#shippinginfobox').html(htmldata);
		htmldata = '29,00 kr.';
		$('#shippingvaluebox').html(htmldata);
		
		tmpship = 29;
	} else {
		htmldata = '<option value="select">v�lg betalingsm�de...</option><option value="giro">GIRO kort</option>';
					
		$('#selcard').html(htmldata);
					
		htmldata = "Ved levering til F�r�erne, Gr�nland og Europa er leveringsgebyret max. 83,00 DKK<br />for pakker under 2 kg. ";
		htmldata += "For pakker over 2 kg. til F�r�erne og Europa er prisen<br />167,00 DKK + 25,00 DKK pr. kg. max. 20 kg. ";
		htmldata += "For pakker over 2 kg. til Gr�nland er<br />prisen 152,00 DKK + 79,00 DKK pr. kg. max. 20 kg.<br /><br />";
		htmldata += "Ved levering udenfor Europa, samme porto som Post Danmark.";
		$('#shippinginfobox').html(htmldata);
		$('#shippingvaluebox').html('');
	}
	
	tmpval += tmpship;
		
	if (tmpval.toString().indexOf('.') < 0) {
		cartnet = tmpval.toString() + ',00 kr.';
	} else if (tmpval.toString().substring(tmpval.toString().indexOf('.')+1).length == 1) {
		cartnet = tmpval.toString().replace('.',',') + '0 kr.';
	} else {
		cartnet = tmpval.toString().replace('.',',') + ' kr.';
	}
				
	$('#carttotbox').html(cartnet);
}

function expandCollapseMedlemer () {
	if ($('#bogklub-member').attr('class') == 'medlem-collapse') {
		$('#bogklub-member').animate({'height':'740px'},'slow','linear');
		$('#bogklub-member').attr({class: "medlem-expand"});
	} else {
		$('#bogklub-member').animate({'height':'20px'},'slow','linear');
		$('#bogklub-member').attr({class: "medlem-collapse"});
	}
}

function getCookie( checkname ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for (var i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == checkname )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

