//$("input [id^='no_']").click(function(){
//	alert('asdasdasd');
//});
//
//$("input[type='radio']").click(function(){
//   alert('asdasdasd');
//});

function clickChk(id, side, elem) {
		if (elem.checked) {
			$("#"+side+"_img_" + id).css("display", "");
		} else {
			$("#"+side+"_img_" + id).css("display", "none");
		}
}

function clickRad(id, elem) {
	if (elem.id.substr(0, 3) == 'yes') {
		$("#lab_no_" + id).attr("class", '');
		$("#lab_yes_" + id).attr("class", 'yes');
		if ((id != 2) && (id != 3)) {
			$("#av_img_" + id).css("display", 'block');
			$("#rev_img_" + id).css("display", 'block');
		}
		if (id == 1) {
			$("#holder_2").css("display", 'block');
			$("#holder_3").css("display", 'block');
			$("#holder_4").css("display", 'block');
		}
		if (id == 2) {
			$("#lab_no_3").attr("class", 'no');
			$("#lab_yes_3").attr("class", '');
			$("#yes_3").attr("checked", "");
			$("#no_3").attr("checked", "checked");
			$("#holder_111").css("display", 'block');
			$("#holder_112").css("display", 'none');
			$("#av_chk2").attr("checked", '');
			$("#rev_chk2").attr("checked", '');
			$("#rev_img_3").css("display", "none");
			$("#av_img_3").css("display", "none");
			
		}
		if (id == 3) {
			$("#lab_no_2").attr("class", 'no');
			$("#lab_yes_2").attr("class", '');
			$("#yes_2").attr("checked", "");
			$("#no_2").attr("checked", "checked");
			$("#holder_112").css("display", 'block');
			$("#holder_111").css("display", 'none');
			$("#av_chk1").attr("checked", '');
			$("#rev_chk1").attr("checked", '');
			$("#rev_img_2").css("display", "none");
			$("#av_img_2").css("display", "none");
			
		}
		if (id == 5) {
			$("#holder_6").css("display", 'block');
		}
		if (id == 10) {
			$("#holder_11").css("display", 'block');
		}
	}
	
	if (elem.id.substr(0, 2) == 'no') {
		if ((id != 2) && (id != 3)) {
			$("#av_img_" + id).css("display", "none");
			$("#rev_img_" + id).css("display", "none");
		}
		$("#lab_no_" + id).attr("class", 'no');
		$("#lab_yes_" + id).attr("class", '');
		if (id == 1) {
			$("#holder_2").css("display", 'none');
			$("#holder_3").css("display", 'none');
			$("#holder_4").css("display", 'none');
			$("#lab_no_2").attr("class", '');
			$("#lab_yes_2").attr("class", '');
			$("#yes_2").attr("checked", "");
			$("#no_2").attr("checked", "");
			$("#av_img_6").css("display", "none");
			$("#rev_img_6").css("display", "none");
			$("#lab_no_3").attr("class", '');
			$("#lab_yes_3").attr("class", '');
			$("#yes_3").attr("checked", "");
			$("#no_3").attr("checked", "");
			$("#av_img_3").css("display", "none");
			$("#rev_img_3").css("display", "none");
			$("#lab_no_4").attr("class", 'no');
			$("#lab_yes_4").attr("class", '');
			$("#yes_4").attr("checked", "");
			$("#no_4").attr("checked", "checked");
			$("#av_img_4").css("display", "none");
			$("#rev_img_4").css("display", "none");
			$("#holder_111").css("display", 'none');
			$("#holder_112").css("display", 'none');
			$("#av_chk1").attr("checked", '');
			$("#rev_chk1").attr("checked", '');
			$("#av_chk2").attr("checked", '');
			$("#rev_chk2").attr("checked", '');
			$("#av_img_2").css("display", "none");
			$("#rev_img_2").css("display", "none");
			$("#av_img_3").css("display", "none");
			$("#rev_img_3").css("display", "none");
			
		}
		if (id == 2) {
			$("#lab_no_3").attr("class", '');
			$("#lab_yes_3").attr("class", 'yes');
			$("#yes_3").attr("checked", "checked");
			$("#no_3").attr("checked", "");
			$("#holder_111").css("display", 'none');
			$("#holder_112").css("display", 'block');
			$("#av_chk1").attr("checked", '');
			$("#rev_chk1").attr("checked", '');
			$("#rev_img_2").css("display", "none");
			$("#av_img_2").css("display", "none");
			
		}
		if (id == 3) {
			$("#lab_no_2").attr("class", '');
			$("#lab_yes_2").attr("class", 'yes');
			$("#yes_2").attr("checked", "checked");
			$("#no_2").attr("checked", "");
			$("#holder_111").css("display", 'block');
			$("#holder_112").css("display", 'none');
			$("#av_chk2").attr("checked", '');
			$("#rev_chk2").attr("checked", '');
			$("#rev_img_3").css("display", "none");
			$("#av_img_3").css("display", "none");
		}
		if (id == 5) {
			$("#holder_6").css("display", 'none');
			$("#av_img_6").css("display", "none");
			$("#rev_img_6").css("display", "none");
			$("#yes_6").attr("checked", "");
			$("#lab_yes_6").attr("class", '');
			$("#no_6").attr("checked", "checked");
			$("#lab_no_6").attr("class", 'no');
		}
		if (id == 10) {
			$("#holder_11").css("display", 'none');
			$("#av_img_11").css("display", "none");
			$("#rev_img_11").css("display", "none");
			$("#yes_11").attr("checked", "");
			$("#lab_yes_11").attr("class", '');
			$("#no_11").attr("checked", "checked");
			$("#lab_no_11").attr("class", 'no');
		}
	}
}

function setClass(item_id, ref, img1, img2, l_left, l_top, r_left, r_top) {
	
	if (ref.substr(0,3) == 'yes') {
		document.getElementById('lab_yes_'+item_id).className = 'yes';
		document.getElementById('lab_no_'+item_id).className = '';
		showImg(item_id, img1, img2, l_left, l_top, r_left, r_top);
	} else if (ref.substr(0,2) == 'no') {
		document.getElementById('lab_no_'+item_id).className = 'no';
		document.getElementById('lab_yes_'+item_id).className = '';
		if ((item_id != 2) && (item_id != 3)) {
			hideImg(item_id);
		}
		
	}
}

function chkDep(elem) {
	if (elem.id == 'yes_2') {
		setToNo(3);
		hideChild(112);
	} 
	if (elem.id == 'yes_3') {
		setToNo(2);
		hideChild(111);
	} 
	if (elem.id == 'no_2') {
		setToYes(3);
		showChild(112);
	} 
	if (elem.id == 'no_3') {
		setToYes(2);
		showChild(111);
	} 
}

function setToNo(id) {
	$("#no_"+id).attr("checked", true);
	setClass(id, 'no');
}

function setToYes(id) {
	$("#yes_"+id).attr("checked", true);
	setClass(id, 'yes');
}

function showTip(id, title) {
	title = title + '?';
	document.getElementById('whatIs').style.display = '';
	var text = document.getElementById('text_'+id).innerHTML;
	document.getElementById('tips').innerHTML = text;
	document.getElementById('tip_title').innerHTML = title;
}

function hideTip() {
	document.getElementById('tip_title').innerHTML = '';
	document.getElementById('whatIs').style.display = 'none';
	var text = document.getElementById('text_0').innerHTML;
	document.getElementById('tips').innerHTML = text;
}

function showImg(id, av_image, rev_image,l_left, l_top, r_left, r_top) {
	try {
		var av_exist = document.getElementById('img_av_'+id);
		if (!av_exist) {
			if (av_image) {
				var av_container = document.getElementById('card_averse');
				var img = document.createElement('img');
				img.id = 'img_av_'+id;
				img.className = 'img';
				img.src = av_image;
				if (l_left && l_top) {
					img.style.position = 'absolute';
					img.style.left = l_left+'px';
					img.style.top = l_top+'px';
				}
				av_container.appendChild(img);
			}
		}
		var rev_exist = document.getElementById('img_rev_'+id);
		if (!rev_exist) {
			if (rev_image) {
				var rev_container = document.getElementById('card_reverse');
				var img = document.createElement('img');
				img.id = 'img_rev_'+id;
				img.className = 'img';
				img.src = rev_image;
				if (r_left && r_top) {
					img.style.position = 'absolute';
					img.style.left = r_left+'px';
					img.style.top = r_top+'px';
				}
				rev_container.appendChild(img);
			}
		}
	} catch (e) {}
}

function hideImg(id) {
	try {
		var av_img = document.getElementById('img_av_'+id);
		if (av_img) {
			var av_container = document.getElementById('card_averse');
			av_container.removeChild(av_img);
		}
		var rev_img = document.getElementById('img_rev_'+id);
		if (rev_img) {
			var rev_container = document.getElementById('card_reverse');
			rev_container.removeChild(rev_img);
		}
	} catch (e) {}
}

function showChild(id) {
	if (IsNumeric(id)) {
		try {
			document.getElementById('holder_'+id).style.display = 'block';
		} catch (e) {}
	} else {
		var ids = new Array();
		ids = id.split(',')
		for (i=0; i<ids.length; i++) {
			try {
				document.getElementById('holder_'+ids[i]).style.display = 'block';
			} catch (e) {}
		}
	}
}

function hideChild(id) {
	if (IsNumeric(id)) {
		try {
			document.getElementById('holder_'+id).style.display = 'none';
		} catch (e) {}
	} else {
		var ids = new Array();
		ids = id.split(',')
		for (i=0; i<ids.length; i++) {
			try {
				document.getElementById('holder_'+ids[i]).style.display = 'none';
			} catch (e) {}
		}
	}
}

function submitform() {	
	var holders = $("span[id^='holder_']:visible");
	var inputs = $("#form_left li");
	var checkboxes = $('span[id^="holder_"][style=""]:checkbox');
	var selects = $('select');
	var values = new Array();
	var checks = new Array();
	var currCheck = new Array();
	values['radio'] = new Array();
	var error_1 = new Array();
	var error_2 = new Array();
	var error_3 = new Array();
	var error_4 = new Array();
	error_1['lt'] = 'Būtina pasirinkti termotransferinio personalizavimo būdą';
	error_2['lt'] = 'Būtina pažymėti termotransferiniu būdu personalizuojamos kortelės pusę';
	error_3['lt'] = 'Būtina nurodyti užsakomą tiražą';
	error_4['lt'] = 'Ištaisykite klaidas';
	error_1['de'] = 'Sie müssen eine Personalisierung Thermo-Transfer-Verfahren';
	error_2['de'] = 'Sie müssen einen Seite für die Personalisierung wählen Thermo-Transfer-Methode';
	error_3['de'] = 'Sie müssen den Verkehr';
	error_4['de'] = 'Korrigieren von Fehlern';
	error_1['ru'] = 'Необходимо выбрать персонализацию термотрансферным способом';
	error_2['ru'] = 'Необходимо выбрать сторону для персонализаци термотрансферным способом';
	error_3['ru'] = 'Необходимо указать тираж';
	error_4['ru'] = 'Исправьте ошибки';
	error_1['en'] = 'You must select a personalization thermal transfer method';
	error_2['en'] = 'You must select a side for personalization thermal transfer method';
	error_3['en'] = 'You must specify the circulation';
	error_4['en'] = 'Correct errors';
	if ($('#yes_1').attr('checked') === true) {
		if ($('input[type="radio"]:checked', $('#lab_yes_2').parent()).length == 0 && $('input[type="radio"]:checked', $('#lab_yes_3').parent()).length == 0) {
			alert(error_1[site.lng]);
			return;
		}
	}
	if ($('#yes_1').attr('checked') === true) {
		if ($('input[type="checkbox"]:checked').length == 0) {
			alert(error_2[site.lng]);
			return;
		}
	}
	if ($('#e3').val().length == 0) {
		alert(error_3[site.lng]);
		return;
	}
	jQuery.each(holders, function(i, val) {
		currCheck = Array();
		if ($("input[type='radio']:checked", val).attr('id') != null) {
			if (val.id == 'holder_2') {
				if ($('#holder_111 input[type="checkbox"][id^="av"]').attr('checked') === true)
					currCheck.push('aversas');
				if ($('#holder_111 input[type="checkbox"][id^="rev"]').attr('checked') === true)
					currCheck.push('reversas');
			}  
			if (val.id == 'holder_3') {
				if ($('#holder_112 input[type="checkbox"][id^="av"]').attr('checked') === true)
					currCheck.push('aversas');
				if ($('#holder_112 input[type="checkbox"][id^="rev"]').attr('checked') === true)
					currCheck.push('reversas');
			}
			if (currCheck.length > 0) {
				checks = " (" +currCheck.join(', ') + ")"; 
			} else {
				checks = "";
			}
			values['radio'].push({ 'name' : $("li:first", val).html() + checks, 'value': $("input[type='radio']:checked", val).attr('id').split('_')[0] });
		}
	});
	values['inputs'] = new Array();
	jQuery.each(inputs, function(i, val) {
		values['inputs'].push({ 'name' : $("label", val).html(), 'value': $("input", val).val(), 'id' : $("input", val).attr('id') });
	});
	values['selects'] = new Array();
	jQuery.each(selects, function(i, val) {
		values['selects'].push({ 'name' : $("label", $('#' + val.id).parent()).html(), 'value': $('option[value="' + $('#' + val.id).val() + '"]').text() });
	});
	values['quantity'] = $('#e3').val();
	values['comment'] = $('#f5').val();

	$.post("/supra/mail", 
			{
				'val' : serialize(values)
			},
			function(val) {
				umarkRequired();
				if (val == 'ok') {
					requestOk();
				} else {
					jQuery.each(val, function(i, value) {
						markRequired(value);
					});
					alert(error_4[site.lng]);
				}
			}, 
			"json"
		  );
	
	
}

function markRequired(field) {
	var li = $("label:contains('" + field + "')").parent();
	$('input', li).css('border', '1px solid red');
}

function umarkRequired() {
	var inputs = $('#form_left input').css('border', '');
}

function requestOk() {
	var resp = new Array();
	resp['lt'] = 'Ačiū, Jūsų užklausa buvo išsiųsta.';
	resp['en'] = 'Thank you, your request sent.';
	resp['de'] = 'Vielen Dank für Ihre Anfrage zugesandt.';
	resp['ru'] = 'Спасибо, ваш запрос выслан.';
	$("div#about_card").html('<h1>'+resp[site.lng]+'</h1>');
	document.getElementById('scrollable').scrollTop = 0;
}

function serialize (mixed_value) {
    var _getType = function (inp) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            match = cons.match(/(\w+)\(/);
            if (match) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            mixed_value = mixed_value;
            val = "s:" + encodeURIComponent(mixed_value).replace(/%../g, 'x').length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key, 10) : key);
                vals += this.serialize(okey) +
                        this.serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
        case "undefined": // Fall-through
        default: // if the JS object has a property which contains a null value, the string cannot be unserialized by PHP
            val = "N";
            break;
    }
    if (type != "object" && type != "array") {
        val += ";";
    }
    return val;
}

function utf8_encode ( argString ) {
    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;
}


function IsNumeric(sText)

{
	try {
		 var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
	} catch(e) {}
  
   }










