﻿
function setText(el, txt) {
    if(el.value==txt) {
        el.value = "";
    } else if(el.value=="") {
        el.value=txt;
    }
}

function showVideo() {
    $("span[id*='lblVideo']").css("visibility","visible");
}

$(document).ready(function () {
    // Global roll over effects
    $(".red-button").mouseover(function () {
        $(this).animate({backgroundColor: "#ce371a"},200);
    });
    
    $(".red-button").mouseout(function () {
        $(this).animate({backgroundColor: "#c21b04"},200);
    });
    
    $("a.yellow").mouseover(function () {
        $(this).animate({color: "#ffdd53"},200);
    });
    
    $("a.yellow").mouseout(function () {
        $(this).animate({color: "#ffd634"},200);
    });
    
    $("a.top-ten-recipe").mouseover(function () {
        $(this).animate({color: "#c21b04"},200);
    });
    
    $("a.top-ten-recipe").mouseout(function () {
        $(this).animate({color: "#000000"},200);
    });
});

function validate(id, format, initval, msg) {
    var rxStr="";
    var special=false;
    switch(format) {    // decide which regex to test vs
        case "alphanum":
            special=false;
            rxStr=/^[-a-zA-Z\s]+$/;
            break;
        case "email":
            special=false;
            rxStr=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
            break;
        case "sqlinjection":
            special=false;
            rxStr=/^[^']+$/;
            break;
        case "dob":
            special=true;
            rxStr=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
            break;
        case "date":
            format="dob";
            special=true;
            rxStr=/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
            break;
        case "time":
            special=true;
            rxStr=/^[0-9]{2}\:[0-9]{2}$/;
            break;
        case "number":
            special=false;
            rxStr=/^[0-9]+$/;
            break;
        case "telnumber":
            special=false;
            rxStr=/^[0-9\s]+$/;
            break;
        case "password":
            rxStr=/^[0-9a-zA-Z]+$/;
    }
    
    if(!special) {
        if(!Boolean(rxStr.test($(id).attr("value")))) {
            if($(id).attr("value")!=initval) {
                var offset = $(id).offset();
                validationWarning(id, offset, msg);
            } else {
                hideValidationBox(2);
            }
        } else {
            hideValidationBox(1);
        }
    } else {
        if(!Boolean(rxStr.test($(id).attr("value")))) {
            if($(id).attr("value")!=initval) {
                var offset = $(id).offset();
                validationWarning(id, offset, msg);
            } else {
                hideValidationBox(2);
            }
        } else {
            var pass=true;
            switch(format) { // special cases here
                case "dob":
                    pass = validateDate($(id).attr("value"));
                    break;
                case "time":
                    pass = validateTime($(id).attr("value"));
                    break;
            }
            
            if(pass) {
                hideValidationBox(1);
            } else {
                var offset = $(id).offset();
                validationWarning(id, offset, msg);
            }
        }
    }
}

function validateDate(dob) {
    var result=true;
    var arr = dob.split("/");
    var days = parseInt(arr[0]);
    var mons = parseInt(arr[1]);
    var yers = parseInt(arr[2]);
    
    if(mons>12) {result=false;}
    if(days>31) {result=false;}
    
    return result;
}

function validateTime(time) {
    var result=true;
    var arr = time.split(":");
    var hrs=parseInt(arr[0]);
    var mns=parseInt(arr[1]);
    
    if(hrs>23) {result=false;}
    if(mns>59) {result=false;}
    
    return result;
}

function validationWarning(id, offset, msg) {
    hideValidationBox(3);
    $("p.err").html(msg);
    var vId= ".validationAlert";
    var errH = $(vId).height()/2;
    var iX = offset["left"]+($(id).width()+15);
    var iY = offset["top"]+(($(id).height()/2)-errH+5);
        
    $(vId).css({top: iY});
    $(vId).css({left:iX});
    $(vId).fadeIn(200);
}

function hideValidationBox(i) {
    $(".validationAlert").fadeOut(250);
}
