// Form validation javascript

 function validate(form) {
    var message = "";

//  alert("Entering validation script");

    form.subject.value = form.formName.value

//  alert(form.subject.value);
    
    if (form.firstName.value == "") {
        message += "First name is required \n";
    }

    if (form.lastName.value == "") {
        message += "Last name is required \n";
    }

    if (form.email.value == "") {
        message += "Your e-mail address is required \n";
    }

    if (message.length != 0) {
        alert(message);
        return false;
    } 
    else 
    {
        form.mailFrom.value = form.email.value;
        form.subject.value += " from " + form.firstName.value + " " + form.lastName.value;
        return true;
    }
}


function echeck(str) {

        var at="@";
        var dot=".";
        var lat=str.indexOf(at);
        var lstr=str.length;
        var ldot=str.indexOf(dot);
        if (str.indexOf(at)==-1){
           //alert("Invalid E-mail ID");
           return false;
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           //alert("Invalid E-mail ID");
           return false;
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            //alert("Invalid E-mail ID");
            return false;
        }

         if (str.indexOf(at,(lat+1))!=-1){
            //alert("Invalid E-mail ID");
            return false;
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            //alert("Invalid E-mail ID");
            return false;
         }

         if (str.indexOf(dot,(lat+2))==-1){
            //alert("Invalid E-mail ID");
            return false;
         }

         if (str.indexOf(" ")!=-1){
            //alert("Invalid E-mail ID");
            return false;
         }

         return true;
    }

function validateForm(item){
    var itemchecked;
    var error_message = "";
    for (var i=0; i < document.lyris.category.length; i++)
    {
       if (document.lyris.category[i].checked) {
           itemchecked = document.lyris.category[i].value;
       }
    }
    if(itemchecked==undefined){
        error_message =  "Please select at least one option\n";
    }
    if(document.lyris.name.value == ''){
        error_message += "Please enter Name\n";
    }
    if((document.lyris.email.value == '') || (echeck(document.lyris.email.value)==false)) {
        error_message +=  "Please enter a valid E-mail address";
    }
    if (error_message.length != 0) {
       alert(error_message);
       return false;
    }
    else {
        if (itemchecked == 'student'){
            document.lyris.list.value       ='bimson_student';        
            document.lyris.Additional_.value='bimson_student';        
        } else if (itemchecked == 'faculty'){
            document.lyris.list.value       ='bimson_faculty';
            document.lyris.Additional_.value='bimson_faculty';
        } else if (itemchecked == 'giving'){
            document.lyris.list.value       ='bimson_giving';
            document.lyris.Additional_.value='bimson_giving';
        } else if (itemchecked == 'other'){
            document.lyris.list.value       ='bimson_other';
            document.lyris.Additional_.value='bimson_other';
        }
        
        if(itemchecked != 'student') {
            document.lyris.x_Doctorate.checked = false;
            document.lyris.x_Masters.checked   = false;
            document.lyris.x_Undergrad.checked = false;
        }
        document.lyris.url.value= 'http://' + document.location.hostname + '/nursing/feedback/thankyou.html';
        return true;
    }

    return false;
}

function validateUnSubForm(item) {
    var error_message = "";
    if((document.lyris.email.value == '') || (echeck(document.lyris.email.value)==false)) {
        error_message =  "Please enter a valid Email ID";
    }
    if (error_message.length != 0) {
       alert(error_message);
       return false;
    }
    else {
        document.lyris.url.value= 'http://' + document.location.hostname + '/nursing/feedback/unsub_confirmation.html';
        return true;
    }

    return false;

}

function gup( name ){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if( results == null )    
        return "";  
    else    
        return results[1];
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function( 
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    ){
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );
     
    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1){
        // Relace out the current instance.
        strText = strText.replace( strTarget, strSubString )
         
        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget );
    }
     
    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return( strText );
}

function handleSelection( selectionValue ) {
    if (selectionValue == 'student') {
        document.lyris.x_Doctorate.disabled = false;        
        document.lyris.x_Masters.disabled = false;
        document.lyris.x_Undergrad.disabled = false;
    } else {
        document.lyris.x_Doctorate.disabled = true;        
        document.lyris.x_Masters.disabled = true;
        document.lyris.x_Undergrad.disabled = true;
        document.lyris.x_Doctorate.checked = false;
        document.lyris.x_Masters.checked   = false;
        document.lyris.x_Undergrad.checked = false;
    }
    return true;
}