function isEmail(p_str, p_required) {
    if (p_required) {
        if (!isStringFilledIn(p_str)) {
            return -2;
        }
    }

    if (!p_str.match(/^[\w]{1,3}[\w\.\-_]*@[\w]{1,3}[\w\-_\.]*\.[\w]{2,6}$/i)) {
        return -1;
    } else {
        return 0;
    }

}



String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}



function PostToService(itemID, REMOTE_ADDR) {
    var post_overskrift = $j('#post_overskrift');
    var post_kommentar = $j('#post_kommentar');
    var post_kommentar_fixed = "";
    var post_name = $j('#post_name');
    var post_email = $j('#post_email');
    var validationMessage = "";
    
    if (post_overskrift.val() == "")
        validationMessage += "Venligst indtast en overskrift\n";
    if (post_kommentar.val() == "")
        validationMessage += "Venligst indtast en kommentar\n";
    else {
        // replace html
        post_kommentar_fixed = post_kommentar.val().replace(/<[^>]+>/ig, "");
        // replace newline
        post_kommentar_fixed = post_kommentar_fixed.replace(/\n/g, "<br />");
        post_kommentar_fixed = post_kommentar_fixed.replace(/'/g, "&quot;");
    }
    if (post_name.val() == "")
        validationMessage += "Venligst indtast dit navn\n";
    if (post_email.val() == "")
        validationMessage += "Venligst indtast din email\n";
    else {
        var regEx = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        var matches = regEx.exec(post_email.val());
        if (matches == null)
            validationMessage += "Venligst indtast en gyldig email\n";
    }
    
    
    if (validationMessage != "") {
        alert(validationMessage);
        return false;
    }
    else {
        $j('#post_submit').attr('disabled','true');
        $j.ajax({
            type: "POST",
            url: "/OxygenSoftware/DebatService.asmx/PostToService",
            data: "{sitecoreContentItemID:'" + itemID + "',title:'" + post_overskrift.val().replace(/'/g, "") + "',comment:'" + post_kommentar_fixed + "',name:'" + post_name.val().replace(/'/g, "") + "', email:'" + post_email.val() + "', ip:'" + REMOTE_ADDR + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            success: function(json) {
                setTimeout("window.location.href=window.location.href;",1000);
            },
            error: function(xhr, desc, exceptionobj) { if(xhr.responseText != "")alert("'" + xhr.responseText + "'"); }
        });
    }
}



function qs(qskey) {
    var qsParm = new Array();
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos);
            var val = parms[i].substring(pos + 1);
            qsParm[key] = val;
        }
    }

    return qsParm[qskey]
}

//function slideShow() {

//    //Set the opacity of all images to 0
//    $j('#gallery a').css({ opacity: 0.0 });

//    //Get the first image and display it (set it to full opacity)
//    $j('#gallery a:first').css({ opacity: 1.0 });

//    //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
//    setInterval('gallery()', 6000);

//}

//function gallery() {
//  
//    //if no IMGs have the show class, grab the first image
//    var current = ($j('#gallery a.show') ? $j('#gallery a.show') : $j('#gallery a:first'));

//    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
//    var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $j('#gallery a:first') : current.next()) : $j('#gallery a:first'));

//    //Set the fade in effect for the next image, show class has higher z-index
//    next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, 1000);

//    //Hide the current image
//    current.animate({ opacity: 0.0 }, 1000).removeClass('show');

//}
