// JavaScript Document
var baseuri = 'http://'+window.location.hostname+'/';
var base_url = 'http://'+ window.location.hostname + '/index.php/';
var imgURL = "http://"+window.location.hostname+"/resources/images/";

function hoverSideImages(imgName){
    /*$("#SideImage"+imgName).attr("src",imgURL+"toolsImages/"+imgName+"hov.jpg").mouseout(function(){
									$("#SideImage"+imgName).attr("src",imgURL+"toolsImages/"+imgName+".jpg");
																					});*/
    $("#SideImage"+imgName).attr("src",imgURL+"toolsImages/"+imgName+"hov.jpg").hover(function(){},function(){
        $("#SideImage"+imgName).attr("src",imgURL+"toolsImages/"+imgName+".jpg");
    });
}

function show_light_(obj){
    $("#"+obj).fadeIn(300, function () { });
}

function CloseCopyright(objID){
    document.getElementById(objID).style.display = "none";
    document.getElementById('preLoading').style.display = "none";
}

function enrollAlert(frm){

    var errorHandler = validateFormOnSubmit(frm)
    if(errorHandler != true){
        document.getElementById('ULerrorDescription').innerHTML = errorHandler
        $(".emailAlertTooltip").fadeIn(300, function () { });
    }else{
        document.getElementById('emailAlertTooltip').style.display = "none";
        processEmailEnrolment(frm)
    }
}

function processEmailEnrolment(frm){
    var Ename = frm.name.value
    var Eemail = frm.email.value
    var xhr;
    try{
        xhr = new XMLHttpRequest();
    }catch(error){
        try{
            xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }catch(error){
            xhr=null;
        }
    }
    if (xhr != null){
        url = baseuri +"index.php/home/enrollmailer";
        xhr.open('POST',url, true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function()
        {
            document.getElementById('preLoading').style.display = 'block';
            if(xhr.readyState == 4){
                if(xhr.status == 200 || xhr.status == 304){
                    document.getElementById('preLoading').style.display = 'none';
                    document.getElementById('ULerrorDescription').innerHTML = xhr.responseText
                    $(".emailAlertTooltip").fadeIn(300, function () { });
                    document.getElementById('frmEmailAlerts').reset();
                }
            }
        };
        strpost = "email="+Eemail+"&name="+Ename
        xhr.send(strpost);
        return false;
    }
    return true;
}

function closeValidate(obj){
    //obj.parentNode.style.display="none";
    $("."+obj.parentNode.className).fadeOut(300, function () { });

}

function validateFormOnSubmit(theForm) {
    var reason = "";

    reason += validateEmpty(theForm.name,'Name');
    reason += validateEmail(theForm.email);
    if (reason != "") {
        return reason;
    }

    return true;
}

function validateEmpty(fld ,fieldname) {
    var error = "";

    if (fld.value.length == 0) {
        error = '<li>'+fieldname+" field is required. </li>"
    }
    return error;
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        error =  '<li>Email field is required. </li>'
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters

        error =  '<li>Invalid Email Address. </li>'
    } else if (fld.value.match(illegalChars)) {
        error =  '<li>Invalid Email Address. </li>'
    }
    return error;
}

function trim(s)
{
    return s.replace(/^\s+|\s+$/, '');
}
