/**
 *	This function rescales the image object to fit the maxSize.
 */
function imageRescale(id, maxSize) {

    var image = document.getElementById(id);

    if (image.naturalWidth === undefined) {
        image.naturalWidth = image.width;
    }
        
    if (image.naturalHeight === undefined) {
        image.naturalHeight = image.height;
    }
    
    if (image.naturalWidth === 0 || image.naturalHeight === 0) {        
        image.style.height = maxSize + "px";
        image.style.width = maxSize + "px";        
    } else {    
        if (image.naturalHeight > image.naturalWidth) {
            image.style.height = maxSize + "px";
        } else {
            image.style.width = maxSize + "px";
        }
    }
    
    image.style.display = "";
}
/**
 *	This function validates the search_form form on submit
 */
function validateSearch(searchForm) {

    var searchQuery = searchForm.sq.value;
    if (searchQuery == "J\xe4mf\xf6r priser nu!") {
        return false;
    }
    
    if (searchQuery.length < 2) {
        this.alert("Din s\xf6kning m\xe5ste inneh\xe5lla minst 2\nbokst\xe4ver, " +
                "siffor eller tecken.");
        return false;
    }
}
/**
 *	This function validates the ext_search_form form on submit
 */
function validateExtSearch() {
    var priceInterval = document.getElementById("ext_pi");
    var manufacturer = document.getElementById("ext_mf");
    var searchQuery = document.getElementById("ext_sq");
    var category = document.getElementById("ext_c");
    var store = document.getElementById("ext_s");
    if (searchQuery.value == "Ange dina s\xf6kord h\xe4r") {
        searchQuery.value = "";
    }
    if (!priceInterval.value && !manufacturer.value && !searchQuery.value && !category.value && !store.value) {
        this.alert("Du m\xe5ste begr\xe4nsa din s\xf6kning\np\xe5 ett eller annat s\xe4tt!");
        return false;
    }
}
function sortSearch(sortByOption) {
    if (sortByOption.value !== "") {    
        document.location = sortByOption.value;
    }
}
function validateLoginBox(loginForm) {
    var emailValue = loginForm.email.value;
    var password = loginForm.password.value;
    
    if (emailValue === "" || password === "") {
        this.alert("Ange e-post och l\xf6senord f\xf6r att logga in.");
        return false;
    }
}
function validateRegisterUser(registerForm) {

    var nameValue = registerForm.reg_user_name.value;
    var emailValue = registerForm.reg_user_email.value;
    var password = registerForm.reg_user_password.value;
    var repassword = registerForm.reg_user_repassword.value;
    
    if (nameValue === "" || emailValue === "" || password === "" || repassword === "") {
        this.alert("Ange namn, e-post och l\xf6senord f\xf6r att skapa ett " + 
                "anv\xe4ndarkonto");
        return false;
    }
    if (password === repassword) {
        return true;
    } else {
        this.alert("L\xf6senorden m\xe5ste vara samma i b\xe5da f\xe4lten.");
        return false;
    }
}

function validateRegisterStore(registerForm) {

    var errorMsg       = "";    
    var name           = registerForm.name.value;
    var category_id    = registerForm.category_id.value;
    var contact_name   = registerForm.contact_name.value;
    var contact_phone  = registerForm.contact_phone.value;
    var contact_mobile = registerForm.contact_mobile.value;
    var contact_email  = registerForm.contact_email.value;
    var homepage_url   = registerForm.homepage_url.value;
    var export_url     = registerForm.import_url.value;
    
    if (name.length < 3) {
        errorMsg = errorMsg + "- Butiksnamn\n";
    }
    if ((contact_name.length < 2) || (contact_name.indexOf(" ") == -1)) {
        errorMsg = errorMsg + "- Kontaktperson (F\xf6r- & Efternamn)\n";
    }
    if ((contact_phone.length < 2) && (contact_mobile.length < 2)) {
        errorMsg = errorMsg + "- Kontakt telefon\n- Kontakt mobil\n";
    }
    if ((contact_email.length < 6) || (contact_email.indexOf("@") == -1) || (contact_email.indexOf(".") == -1)) {
        errorMsg = errorMsg + "- Kontakt e-post\n";
    }
    if (!parseInt(category_id) > 0) {
        errorMsg = errorMsg + "- Butikskategori\n";
    }
    if (homepage_url.length < 10 || (homepage_url.indexOf("http://") == -1 && homepage_url.indexOf("https://") == -1) || homepage_url.indexOf(".") == -1) {
        errorMsg = errorMsg + "- URL till hemsida\n";
    }
    if (export_url.length < 10 || (export_url.indexOf("http://") == -1 && export_url.indexOf("https://") == -1) || export_url.indexOf(".") == -1) {
        errorMsg = errorMsg + "- URL f\xf6r produktexport\n";
    }
    
    if (errorMsg !== "") {
        this.alert("Du m\xe5ste fylla i anm\xe4lningsformul\xe4ret ordentligt." +
                "\n\nV\xe4nligen se \xf6ver f\xf6ljande f\xe4lt:\n" +
                errorMsg + "\n");
        return false;
    }
}
