// =================================================================== // Author: Matt Kruse // WWW: http://www.mattkruse.com/ // // NOTICE: You may use this code for any purpose, commercial or // private, without any further permission from the author. You may // remove this notice from your final code if you wish, however it is // appreciated by the author if at least my web site address is kept. // // You may *NOT* re-distribute this code in any way except through its // use. That means, you can include it in your product, or your web // site, or any other form where the code is actually being used. You // may not put the plain javascript up on your site for download or // include it in your javascript libraries for download. // If you wish to share this code with others, please just point them // to the URL instead. // Please DO NOT link directly to my .js files from your site. Copy // the files to your server and use them there. Thank you. // =================================================================== // HISTORY // ------------------------------------------------------------------ // December 9, 2003: Added script to the Javascript Toolbox // December 10, 2003: Added the preProcessTrees variable to allow user // to turn off automatic conversion of UL's onLoad // March 1, 2004: Changed it so if a
  • has a class already attached // to it, that class won't be erased when initialized. This allows // you to set the state of the tree when painting the page simply // by setting some
  • 's class name as being "liOpen" (see example) /* This code is inspired by and extended from Stuart Langridge's aqlist code: http://www.kryogenix.org/code/browser/aqlists/ Stuart Langridge, November 2002 sil@kryogenix.org Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109) */ function emptycart(form){ with(form){ submit(); } } //// Automatically attach a listener to the window onload, to convert the trees //addEvent(window,"load",convertTrees); // //// Utility function to add an event listener //function addEvent(o,e,f){ // if (o.addEventListener){ o.addEventListener(e,f,true); return true; } // else if (o.attachEvent){ return o.attachEvent("on"+e,f); } // else { return false; } //} // //// utility function to set a global variable if it is not already set //function setDefault(name,val) { // if (typeof(window[name])=="undefined" || window[name]==null) { // window[name]=val; // } //} // //// Full expands a tree with a given ID //function expandTree(treeId) { // var ul = document.getElementById(treeId); // if (ul == null) { return false; } // expandCollapseList(ul,nodeOpenClass); //} // //// Fully collapses a tree with a given ID //function collapseTree(treeId) { // var ul = document.getElementById(treeId); // if (ul == null) { return false; } // expandCollapseList(ul,nodeClosedClass); //} // //// Expands enough nodes to expose an LI with a given ID //function expandToItem(treeId,itemId) { // var ul = document.getElementById(treeId); // if (ul == null) { return false; } // var ret = expandCollapseList(ul,nodeOpenClass,itemId); // if (ret) { // var o = document.getElementById(itemId); // if (o.scrollIntoView) { // o.scrollIntoView(false); // } // } //} // //// Performs 3 functions: //// a) Expand all nodes //// b) Collapse all nodes //// c) Expand all nodes to reach a certain ID //function expandCollapseList(ul,cName,itemId) { // if (!ul.childNodes || ul.childNodes.length==0) { return false; } // // Iterate LIs // for (var itemi=0;itemi // // Stödjs av IE5+, Mozilla 1.0+, Opera, Safari, Netscape 6+ //----------------------------------------------------------------- function ajax(inControl, inURL){ var xmlhttp = null; document.body.style.cursor = 'wait'; if (window.XMLHttpRequest) { // Firefox, Opera, Safari och Netscape xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // Internet Explorers olika versioner try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Din webbläsare stödjer inte Ajax"); } } } if (xmlhttp){ var url = inURL xmlhttp.open("GET",url,false); xmlhttp.send(null); var docObj = document.getElementById(inControl); /* Kontrollerar om vi ska skriva till value eller innerHTML */ with( docObj ){ var objType = docObj.type; switch( objType ){ case "text": case "button": docObj.value = xmlhttp.responseText; break; default: docObj.innerHTML = xmlhttp.responseText; break; } } xmlhttp.abort(); } document.body.style.cursor = 'auto'; } function getSelectedCheckbox(buttonGroup) { // Go through all the check boxes. return an array of all the ones // that are selected (their position numbers). if no boxes were checked, // returned array will be empty (length will be zero) var retArr = new Array(); var lastElement = 0; if (buttonGroup[0]) { // if the button group is an array (one check box is not an array) for (var i=0; i 0) { // if there was something selected retArr.length = selectedItems.length; for (var i=0; i < selectedItems.length; i++) { if (buttonGroup[selectedItems[i]]) { // Make sure it's an array //retArr[i] = buttonGroup[selectedItems[i]].value; buttonGroup[selectedItems[i]].getAttribute('req'); foundText = true; } else { // It's not an array (there's just one check box and it's selected) retValue = buttonGroup.getAttribute('req'); foundText = true; //retArr[i] = buttonGroup.value;// return that value } if(foundText) { break; } } } else { retValue = notSelectedText.value; } return retValue; } /* Generell funktion fr att hmta valt vrde frn en drop-down */ function GetSelectedValue(drp){ obj = getElementObject(drp);//document.getElementById(drp); return obj.options[obj.selectedIndex].value; } /* Generell funktion fr att hmta valt vrde frn en drop-down */ function GetSelectedObject(drp){ if(drp) { obj = getElementObject(drp); return obj.options[obj.selectedIndex]; }else{ return false; } } // FORM VALIDATION // JavaScript Document window.onload = attachFormHandlers; function getElementObject(elementName){ obj = null; if(document.getElementById){ if(document.getElementById(elementName)){ obj = document.getElementById(elementName); } }else{ if(document.getElementByName(elementName)){ obj = document.getElementByName(elementName); } } return obj; } function attachFormHandlers(){ // Ensure we're working with a 'relatively' standards // compliant browser if (document.getElementsByTagName){ var objForm = document.getElementsByTagName('form'); var msg = null; for (var iCounter=0; iCounter < objForm.length; iCounter++){ //msg += objForm[iCounter].name+'\n'; if ((objForm[iCounter].name) == 'paymentform'){ objForm[iCounter].onsubmit = function(){return checkPaymentForm(this);} } else if ((objForm[iCounter].name) == 'customerform') { objForm[iCounter].onsubmit = function(){return checkCustomerForm(this);} } else { objForm[iCounter].onsubmit = function(){return checkForm(this);} } } //alert(msg); } } // return the value of the radio button that is checked // return an empty string if none are checked, or // there are no radio buttons function getRadioAttribute(radioObj, attrib) { if(!radioObj) return false; var radioLength = radioObj.length; if (radioLength == undefined) { if (radioObj.checked) { return radioObj.getAttribute(attrib); } else { return false; } } else { for(var i = 0; i < radioLength; i++) { if(radioObj[i].checked) { return radioObj[i].getAttribute(attrib); } } } return false; } function checkPaymentForm_nodeli(objForm) { var isValid = false; var msg = ''; with(objForm) { if(getSelectedCheckboxValue(payment_method) == '') { msg += getCheckboxRequiredValue(payment_method, payment_not_selected)+'\n'; } if(msg == '') { isValid = true; } } if(isValid){ //objForm.submit(); //return true; }else{ alert(msg); return false; } } function checkPaymentForm(objForm) { alert("q8"); with(objForm) { if(getSelectedCheckboxValue(payment_method) == '') { bValid = getCheckboxRequiredValue(payment_method, payment_not_selected)+'\n'; reason = 1; } /* if(getSelectedCheckboxValue(delivery_method) == '') { msg += getCheckboxRequiredValue(delivery_method, delivery_not_selected)+'\n'; }*/ if(msg != '') { /*bValid = true; reason = 1;*/ } } } function checkDeliveryForm(objForm) { var isValid = false; var msg = ''; with(objForm) { if(getSelectedCheckboxValue(delivery_method) == '') { msg += getCheckboxRequiredValue(delivery_method, delivery_not_selected)+'\n'; } if(msg == '') { bValid = objField[iFieldCounter].value != ""; reason = 1; } } } //function checkPaymentForm(objForm) //{ // var arClass, bValid; // var objField = objForm.getElementsByTagName('*'); // Hämtar alla HTML-objekt // var reason; // bValid = false; // // returnValue = false; // // var msg = null; // // for (var iFieldCounter=0; iFieldCounter < objField.length; iFieldCounter++) // { // // Allow for multiple values being assigned to the class attribute // arClass = objField[iFieldCounter].className.split(' '); // // msg += objField[iFieldCounter].value+'\n'; // // for (var iClassCounter=0; iClassCounter < arClass.length; iClassCounter++) // { // switch (arClass[iClassCounter]) // { // case 'string': // if(objField[iFieldCounter].value != "") // { // bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, '')); // }else{ // bValid = true; // } // reason = 0; // break; // // case 'number' : // if(objField[iFieldCounter].value != "") // { // bValid = isNumber(objField[iFieldCounter].value); // }else{ // bValid = true; // } // reason = 0; // break; // // case 'email' : // if(objField[iFieldCounter].value != "") // { // bValid = isEmail(objField[iFieldCounter].value); // }else{ // bValid = true; // } // reason = 0; // break; // // case 'required' : // bValid = objField[iFieldCounter].value != ""; // reason = 1; // break; // default: // bValid = true; // break; // } // // if (bValid == false) // { // // If this field is invalid, leave the testing early, // // and alert the visitor to this error // if (reason == 1) // { // alert(objField[iFieldCounter].getAttribute('req')); // //alert('Du måste ange: ' + objField[iFieldCounter].name); // objField[iFieldCounter].select(); // objField[iFieldCounter].focus(); // return false; // } else { // alert(objField[iFieldCounter].getAttribute('wrong')); // //alert('Felaktigt värde i fältet: ' + objField[iFieldCounter].name); // objField[iFieldCounter].select(); // objField[iFieldCounter].focus(); // return false; // } // } else {// end if (bValid) // returnValue = true; // } // } // } // return true; //} function trim(inString) { return inString.replace(" ",""); } function thousand_separator(S) { S = String(S); var RgX = /^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/; return S == (S = S.replace(RgX, "$1$2 $3")) ? S : thousand_separator(S); } function switch_delivery_cost(objForm, decost, deid){ var formname = objForm.name; i = document.getElementById("totsum").innerHTML; i = trim(i); totalsum = parseFloat(decost) + parseFloat(i); vat = objForm._vat.value; vat = vat+".00"; totalvat = parseFloat(totalsum) - (parseFloat(totalsum)/((100 + parseFloat(vat))/100)); //Table document.getElementById('delcost').innerHTML = thousand_separator(decost.toFixed(2)); document.getElementById('totvat').innerHTML = thousand_separator(totalvat.toFixed(2)); document.getElementById('totsum2').innerHTML = thousand_separator(totalsum.toFixed(2)); //Form document.getElementById('_vat_cost').value = thousand_separator(totalvat.toFixed(2)); document.getElementById('_delivery_id').value = deid; document.getElementById('_delivery_cost').value = thousand_separator(decost.toFixed(2)); document.getElementById('_totalsum').value = thousand_separator(totalsum.toFixed(2)); //document.getElementById('_delivery_id').value = deid; } function checkForm(objForm) { var arClass, bValid; var objField = objForm.getElementsByTagName('*'); var reason; for (var iFieldCounter=0; iFieldCounter= 1){ //Leveranssätt if(getSelectedCheckboxValue(delivery_method) == ''){ if(msg == ""){ msg = "Du måste välja leveranssätt"; isValid = false; } } } //Förnamn if(trim(document.getElementById('firstname').value) == ''){ if(msg == ""){ msg = "Du måste fylla i ett förnamn"; document.getElementById('firstname').select(); document.getElementById('firstname').focus(); isValid = false; } } //Efternamn if(trim(document.getElementById('lastname').value) == ''){ if(msg == ""){ msg = "Du måste fylla i ett efternamn"; document.getElementById('lastname').select(); document.getElementById('lastname').focus(); isValid = false; } } //Gatuadress if(trim(document.getElementById('Gatuadress').value) == ''){ if(msg == ""){ msg = "Du måste fylla i en gatuadress"; document.getElementById('Gatuadress').select(); document.getElementById('Gatuadress').focus(); isValid = false; } } //Postnummer if(trim(document.getElementById('Postnummer').value) == ''){ if(msg == ""){ msg = "Du måste fylla i ett postnummer"; document.getElementById('Postnummer').select(); document.getElementById('Postnummer').focus(); isValid = false; } }else{ if(!isNumber(trim(document.getElementById('Postnummer').value))){ msg = "Postnr får enbart innehålla siffror"; document.getElementById('Postnummer').select(); document.getElementById('Postnummer').focus(); isValid = false; } } //Ort if(trim(document.getElementById('Ort').value) == ''){ if(msg == ""){ msg = "Du måste fylla i en ort"; document.getElementById('Ort').select(); document.getElementById('Ort').focus(); isValid = false; } } //Telefon if(trim(document.getElementById('Telefon').value) == ''){ if(msg == ""){ msg = "Du måste fylla i ett Telefonnummer"; document.getElementById('Telefon').select(); document.getElementById('Telefon').focus(); isValid = false; } }else{ if(!isNumber(trim(document.getElementById('Telefon').value))){ msg = "Telefon får enbart innehålla siffror"; document.getElementById('Telefon').select(); document.getElementById('Telefon').focus(); isValid = false; } } //Epost if(trim(document.getElementById('Epost').value) == ''){ if(msg == ""){ msg = "Du måste fylla i en epost"; document.getElementById('Epost').select(); document.getElementById('Epost').focus(); isValid = false; } }else{ if(!isEmail(trim(document.getElementById('Epost').value))){ msg = "Eposten är inte giltig"; document.getElementById('Epost').select(); document.getElementById('Epost').focus(); isValid = false; } } if(msg == ''){ isValid = true; } } if(isValid){ with(objForm){ //Köpvilkor if(getSelectedCheckboxValue(Köpvillkor) == ''){ alert("Du måste godkänna köpvillkoren"); return false; } objForm.submit(); return true; } }else{ alert(msg); return false; } } /*function checkForm(objForm) { var arClass, bValid; var objField = objForm.getElementsByTagName('*'); var reason; for (var iFieldCounter=0; iFieldCounter"; var d = new Date(); var datum; var dag =d.getDate(); var manad =d.getMonth() + 1; var ar =d.getFullYear(); if (manad < 10) manad2 = "0" + manad; else manad2 = manad; if (dag < 10) dag2 = "0" + dag; else dag2 = dag; datum = ar + "-" + manad2 + "-" + dag2; window1= window.open ("", "mywindow"); window1.document.open(); window1.document.write(''); window1.document.write("
    "); window1.document.write(div2Print); window1.document.write("
    "); window1.document.close(); window1.print(); } function showDiv(inDivId){ document.getElementById(inDivId).style.display='block'; } function closeDiv(inDivId){ document.getElementById(inDivId).style.display='none'; } function togglebutton(inId){ var btnStatus; btnStatus = document.getElementById(inId).disabled; if(btnStatus == true){ document.getElementById(inId).disabled = false; }else{ document.getElementById(inId).disabled = true; } } function btnShowPrintableList(form){ with(form){ todo.value = "printcart"; submit(); } } function btnEmptyCart(form, question, inAction){ with(form){ if(confirm(question)){ action = inAction; submit(); } } } function btnDoPost(form, inAction){ with(form){ action = inAction; /*alertText = 'action: '+action+"\n"; for (var i=0; i