﻿// JavaScript Document

function newCfwindow(nameWin,titleWin,sourceWin,heightWin,widthWin,isClosable) {
	if(isClosable==null){isClosable='true';}
	ColdFusion.Window.create(nameWin, titleWin, sourceWin, {height:heightWin,width:widthWin,modal:true,closable:isClosable,draggable:true,resizable:false,center:true,initshow:true});
	ColdFusion.Window.onHide(nameWin, cfwindowOnHide );
	return true;
}
function cfwindowOnHide(name) {
	ColdFusion.Window.destroy(name,true);
	return true;
}

function changeQty(oForm,idArticle,idField,operation) {
	document.getElementById('idProduit').value=idArticle;
	if(operation=='+') {document.getElementById(idField).value++;}
	if(operation=='-') {document.getElementById(idField).value--;}
	oForm.submit();
}

function copyAddress(oForm) {
	for (i=0; i<oForm.civilite.length; i++) oForm.civilite_facturation[i].checked = oForm.civilite[i].checked;
	oForm.civilite_facturation.value = oForm.civilite.value;
	oForm.nom_facturation.value = oForm.nom.value;
	oForm.prenom_facturation.value = oForm.prenom.value;
	oForm.adresseL1_facturation.value = oForm.adresseL1.value;
	oForm.adresseL2_facturation.value = oForm.adresseL2.value;
	oForm.cp_facturation.value = oForm.cp.value;
	oForm.ville_facturation.value = oForm.ville.value;
	oForm.pays_facturation.value = oForm.pays.value;
	oForm.telephone_facturation.value = oForm.telephone.value;
}

function copyAddress2(oForm) {
	for (i=0; i<oForm.civilite_facturation.length; i++) oForm.civilite[i].checked = oForm.civilite_facturation[i].checked;
	oForm.civilite.value = oForm.civilite_facturation.value;
	oForm.nom.value = oForm.nom_facturation.value;
	oForm.prenom.value = oForm.prenom_facturation.value;
	oForm.adresseL1.value = oForm.adresseL1_facturation.value;
	oForm.adresseL2.value = oForm.adresseL2_facturation.value;
	oForm.cp.value = oForm.cp_facturation.value;
	oForm.ville.value = oForm.ville_facturation.value;
	oForm.pays.value = oForm.pays_facturation.value;
	oForm.telephone.value = oForm.telephone_facturation.value;
}

function testCGV(oForm) {
	if (!oForm.cgv.checked) {
		switch (oForm.lang.value) { 
			case 'en': 
				alert("Accept Terms and Conditions of Sale");
				return false;
				break;
			default:
				return false;
				break;
		}
	} 
	else {return true;}
}

function postForm(formName,mode) {
	var msgError='';
	var cBasket = new _cfc.cBasket();
	cBasket.setForm(formName);
	switch (formName) { 
		case 'shippingForm':
			msgError=cBasket.changeTransporter(mode);
			if (msgError!='') {alert(msgError);} 
			else {ColdFusion.navigate('index.cfm');}
			break;
		case 'basketForm':
			msgError=cBasket.changeTransporter(mode);
			if (msgError!='') {alert(msgError);} 
			else {ColdFusion.navigate('index.cfm');}
			break;
	} 
}

function postForm2(formName) {
	var oForm=document.getElementById(formName);
	var formAction='';
	var range='';
	var brand='';
	var model='';
	var year='';
	switch (formName) { 
		case 'selectBikeForm':
			range=document.getElementById('bikeFormRange').value;
			brand=document.getElementById('bikeFormBrand').value;
			model=document.getElementById('bikeFormModel').value;
			year=document.getElementById('bikeFormYear').value;
			break;
		case 'myBikeForm':
			range=document.getElementById('myBikeListRange').value;
			brand=document.getElementById('myBikeListBrand').value;
			model=document.getElementById('myBikeListModel').value;
			year=document.getElementById('myBikeListYear').value;
			break;
	} 
	range = range.replace("route","street-scooter");
	range = range.replace("cross-sm","mx-sx-supermoto");
	range = range.replace("quad","atv");
	model = model.replace("+","$");
	year = year.replace(">","$");
	formAction=escape(utf8_encode(brand)) + '~' + escape(utf8_encode(model)) + '~' + escape(utf8_encode(year)) + '.' + escape(utf8_encode(range)) + '.exhaust';
	oForm.action='/'+formAction;
	oForm.submit();
	return true;
}






function utf8_encode ( argString ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'

    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}
