function updateTips(t) {
$(".validateTips")
.text(t)
.addClass('ui-state-highlight');
setTimeout(function() {
$(".validateTips").removeClass('ui-state-highlight', 1500);
}, 500);
}
function checkLength(o,n,min,max) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass('ui-state-error');
updateTips("Kentän "+n+" tulee olla vähintään "+min+" merkkiä ja enintään "+max+" merkkiä.");
return false;
} else {
return true;
}
}
function checkRegexp(o,regexp,n) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass('ui-state-error');
updateTips(n);
return false;
} else {
return true;
}
}
function wManage_Addresses_Init() {
var name = $("#name"),
name2 = $("#name2"),
street = $("#street"),
street2 = $("#street2"),
postalcode = $("#postalcode"),
postoffice = $("#postoffice"),
country = $("#country"),
allFields = $([]).add(name).add(name2).add(street).add(street2).add(postalcode).add(postoffice).add(country);
$("#dialog-form").dialog({
autoOpen: false,
resizable : false,
draggable : false,
height: 520,
width: 350,
modal: true,
buttons: {
'Lisää': function() {
var bValid = true;
allFields.removeClass('ui-state-error');
bValid = bValid && checkLength(name,"Nimi",3,255);
bValid = bValid && checkLength(street,"Aadress",3,255);
bValid = bValid && checkLength(postalcode,"Postiindeks",5,5);
bValid = bValid && checkLength(postoffice,"Linn/maakond",1,255);
if(bValid) {
$.ajax({
url : "ajax/address",
dataType : "json",
data: {
action : "add",
name : name.val(),
name2 : name2.val(),
street : street.val(),
street2 : street2.val(),
postalcode : postalcode.val(),
postoffice : postoffice.val(),
country : country.val()
},
success : wManage_Addresses_Callback,
error : wManage_Addresses_Error
});
}
},
'Peruuta' : function() {
$(this).dialog('close');
}
}
});
$("#change-form").dialog({
autoOpen: false,
resizable : false,
draggable : false,
height: 510,
width: 350,
modal: true,
open: function() {
$("#typeInd").val($(this).data('type'));
},
buttons: {
'Peruuta' : function() {
$(this).dialog('close');
}
}
});
$('.selectItem').live('click', function() {
var type = $('#typeInd').val();
var id = this.id;
var str = this.innerHTML;
str = str.replace(/
/g, "");
var selectedAdd = new Object();
selectedAdd = str.split('');
if(type == "delivery") {
//$('#delivery_address').val(id);
$.each(selectedAdd, function(key, value) {
if(key == "0") {
$('#delivery_name').html(value);
$('#delivery_name_input').val(value);
} else if(key == "1") {
$('#delivery_name2').html(value);
$('#delivery_name2_input').val(value);
} else if(key == "2") {
$('#delivery_street').html(value);
$('#delivery_street_input').val(value);
} else if(key == "3") {
$('#delivery_street2').html(value);
$('#delivery_street2_input').val(value);
} else if(key == "4") {
$('#delivery_postalcode').html(value);
$('#delivery_postalcode_input').val(value);
} else if(key == "5") {
$('#delivery_postoffice').html(value);
$('#delivery_postoffice_input').val(value);
} else if(key == "6") {
$('#delivery_country').html(value);
$('#delivery_country_input').val(value);
}
});
} else if(type == "billing") {
//$('#billing_address').val(id);
$.each(selectedAdd, function(key, value) {
if(key == "0") {
$('#billing_name').html(value);
$('#billing_name_input').val(value);
} else if(key == "1") {
$('#billing_name2').html(value);
$('#billing_name2_input').val(value);
} else if(key == "2") {
$('#billing_street').html(value);
$('#billing_street_input').val(value);
} else if(key == "3") {
$('#billing_street2').html(value);
$('#billing_street2_input').val(value);
} else if(key == "4") {
$('#billing_postalcode').html(value);
$('#billing_postalcode_input').val(value);
} else if(key == "5") {
$('#billing_postoffice').html(value);
$('#billing_postoffice_input').val(value);
} else if(key == "6") {
$('#billing_country').html(value);
$('#billing_country_input').val(value);
}
});
}
$('#change-form').dialog('close');
});
$('#adduser')
.button()
.click(function() {
$('#dialog-form').dialog('open');
});
$('#changeuser')
.button()
.click(function() {
$('#change-form').data('type', 'delivery').dialog('open');
});
$('#changeuser2')
.button()
.click(function() {
$('#change-form').data('type', 'billing').dialog('open');
});
}
function wManage_Addresses_Callback(data, textStatus, xhr)
{
if(data['result'] == "false") {
wManage_Addresses_Error(xhr, "Virhe osoitteen tallennuksessa.");
} else {
$('#msg_dialog').dialog({
modal : true,
resizable : false,
draggable : false,
title : "Dieta",
width : 400,
buttons : {
"Jatka tilausta" : function() { $(this).dialog('close'); }
}
});
$('#msg_dialog').html("Osoite lisätty.").dialog('open');
$('#dialog-form').dialog('close');
var table = data['table'];
$("#selectList").append(table);
}
}
function wManage_Addresses_Error(xhr, textStatus, errorThrown)
{
wManage_Addresses_Message("Virhe", textStatus);
}
function wManage_Addresses_Message(title, body)
{
$('#msg_dialog').dialog({
modal : true,
resizable : false,
draggable : false,
title : title,
buttons : {
"Ok" : function() { $(this).dialog('close'); }
}
});
$('#msg_dialog').html(body).dialog('open');
}