function validate(event) {
    e = event || window.event;
    /*
    alert(event.charCode);
    if(charachter.match(/[a-zA-Z0-9]/) === null) {
        return false;
    }
    return true;
    */
    var unicode=e.charCode? e.charCode : e.keyCode;

    if (unicode!=8) { //if the key isn't the backspace key (which we should allow)
        if(unicode==46 || unicode==37 || unicode==38 || unicode==39 || unicode==40) {
            return true;
        }
        if(unicode>=97 && unicode<=122) {
            return true;
        }
        if(unicode>=65 && unicode<=90) {
            return true;
        }
        if (unicode<48||unicode>57) //if not a number
            return false //disable key press

    }
}
function getFontDimensions(font) {
    var map = {
        'oldenglish': [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],
        'lucida': [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],        
        "arialbold": [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],
        "script": [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],
        "chancery": [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],
        "celtic": [973,1124,972,1127,973,1138,1057,1128,821,908,1132,977,1358,1356,1128,1126,1131,1133,1130,1060,1131,975,1351,907,1057,825,598,595,442,594,527,374,675,595,368,372,654,373,977,681,591,597,592,528,604,373,681,595,902,679,597,442,751,731,752,752,754,749,751,751,750,751,361,360,588,361,361,344,295,1057,440,440,364,503],
        "times": [1064,975,975,1059,896,814,1060,1062,485,571,1066,897,1309,1067,1057,814,1057,985,805,895,1065,1064,1392,1065,1064,897,650,734,646,735,646,478,732,737,403,410,739,403,1148,738,729,735,734,492,563,409,739,736,1065,735,737,651,728,654,730,721,732,722,727,727,722,727,349,354,647,391,386,370,378,1140,483,483,459,480]
    }
    try {
        return map[font];
    } catch(e) {
        throw "Font " + font + " is unknow";
    }
}
function getStringWidth(str, font) {
    var chargefor = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var measure = chargefor+".,\";: \'&()!-";    
    var dimensions = getFontDimensions(font);
    var linelength=0;
    var	loop=0;
	while (loop<str.length) {
        linelength += dimensions[measure.indexOf(str.charAt(loop))];
		loop += 1;
    }
    return linelength;
}
function init() {
	var pl = $('customize-form');
		
	var radios = pl.getElementsByClassName('radio');
	for(var i=0; i<radios.length; i++) {
		radios[i].onclick = function() {
			customizeSign();
		}
	}
	
	var fields = pl.getElementsByClassName('field');
	for(var i=0; i<fields.length; i++) {
		fields[i].onchange= function() {
			customizeSign();
		}
	}
	customizeSign();
	
}
function customizeSign() {
	var f = document.forms.customize.elements;
	
	tit1 = f.title1.value;
	tit2 = f.title2.value;
	tit3 = f.title3.value;
	
	color =f.color[getCheckedIndex(f.color)].value;
	holes =f.holes[getCheckedIndex(f.holes)].value;
	font =f.font[getCheckedIndex(f.font)].value;
	border=f.border[getCheckedIndex(f.border)].value;
//	alert(color);
	
	
//alert();

	var fla = new SWFObject("/customize.v8.swf", "", "330", "340", 8, "#FFFFFF");
	
	//TEXTS
	var titles = Array();
	for(var i=1; i<=3; i++) {
		tit = eval("tit"+i);
		if(tit!='') titles.push(tit)
	}
	for(var i=0; i<titles.length; i++) {
		fla.addVariable("title"+(i+1), titles[i]);
	}
	//COLORS
	fla.addVariable("colorText", color);
	
	//Holes
	fla.addVariable("holes", holes);
	
	// FONT
	fla.addVariable("fontFamily", font);
//	alert(border);
	fla.addVariable("brd", border);
	
	fla.addParam("swLiveConnect", "true");
	fla.write("flash");
	
	calculate(font);
}

function getCheckedIndex(col) {
	for(var i=0; i<col.length; i++) {
		if(col[i].checked) break;
	}
	return i;
}

function resetCustomize() {
	var f = document.forms.customize.elements;
	
	f.title1.value ='Your Sign';
	f.title2.value = '';
	f.title3.value = '';
	resetRadio(f.color);
	resetRadio(f.font);
	resetRadio(f.holes);
	resetRadio(f.border);
	resetRadio(f.size);
	
	customizeSign();
}
function resetRadio(col) {
	for(var i=0; i<col.length; i++) {
		if(i==0) {
			col[i].checked=true;
		}else {
			col[i].checked=false;
		}
	}
}

function calculate(font) {
    var f = document.forms.customize.elements;
    var title = '';
    var title_lines = 0;
    for(i=1; i<=3; i++) {
        var v = document.getElementsByName('title'+i)[0].value;
        if(v.length) {
            
            title_lines++;
        }
        title += v;
    }
    var noEmptyChars = title.replace(/\s/g, '');

    var numChars = noEmptyChars.length;

    var size = getCheckedIndex(f.size) ? 'large_size' : 'std_size';
    var border = getCheckedIndex(f.border) ? 'with_border' : 'no_border';
    try {
        if(numChars==21) {
            throw "calculate with additinal size";
        }
        eval('var price = prices[numChars].' + size + '_' + border+';');
        var additional_price = 0;
    } catch(e) {
        eval('var price = prices[20].' + size + '_' + border+';');
        eval('var forEachNextChar = prices[21].' + size + '_' + border+';');
        var additional_price = (numChars - 20) * forEachNextChar;
    }
    //var price = numChars * perChar;
    //$('current_price').innerHTML = number_format(price + additional_price, 2, '.', ',');
    $('current_price').innerHTML = number_format(parseFloat(price*1 + additional_price*1), 2, '.', ',');
    $('product_price').value = number_format(parseFloat(price*1 + additional_price*1), 2, '.', ',');
    var heights = {
        'large_size': [
            '175',
            '275',
            '380'
        ],
        'std_size':[
            '125',
            '200',
            '275'
        ]
    };
/*    var widths = [
        ,
        ,
        getStringWidth(
    ];*/
    eval('var height = heights.' + size + '['+parseInt(title_lines - 1)+']');
    var length = Math.max(
            getStringWidth(document.getElementsByName('title'+1)[0].value, font), 
            getStringWidth(document.getElementsByName('title'+2)[0].value, font),
            getStringWidth(document.getElementsByName('title'+3)[0].value, font)
    );
    if(size == 'std_size') {
        length=(length*50/1000)+82.5;
		length=Math.round(length/10)*10;
    } else {
        length=(length*70/1000)+112.5;
		length=Math.round(length/10)*10;
        if(length<210) {
            length=210;
        }
    }
    if (length<150) {
        length=150
    }
    var weight = Math.round(length*height/1000000*58.55*10)/10;
    var current_size = length + " mm x " + height+" mm";
    $('current_size').innerHTML = current_size;
    $('product_size').value = current_size;
    $('current_weight').innerHTML = weight;
    /*
    var weights = {
        'large_size': [
            '3.2 kg approx.',
            '6.9 kg. approx.',
            '11.8 kg. approx.'
        ],
        'std_size':[
            '1.7 kg. approx.',
            '3.6 kg. approx.',
            '6.1 kg. approx.'
        ]
    };
    eval('var weight = weights[size][' + parseInt(title_lines - 1) + ']');
    $('current_weight').innerHTML = weight;
    
    eval('var current_size = sizes[size][' + parseInt(title_lines - 1) + ']');
    $('current_size').innerHTML = current_size;
    */
}

function importFlash() {}

function sendVar(varname,value) {
   if (navigator.userAgent.indexOf("Opera") != -1) {
      document.all.main.SetVariable(varname,value);
   } else {
      window.document.main.SetVariable(varname,value);
   }
    //alert(value);
} 
//http://krijnhoetmer.nl/stuff/javascript/number-format/
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

Event.observe(window, 'load', init);
