function randomStrings(length) {
    chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
    pass = "";
    for (var x = 0; x < length; x++) {
        var i = Math.floor(Math.random() * 62);
        pass += chars.charAt(i);
    }

    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    var additive = hour + minute + second;
    return pass + additive;
}

function refreshform() {
    document.cet_form.Graduate[0].checked = false;
    document.cet_form.Graduate[1].checked = false;

    for (i = 0; i < 2; i++) {
        document.cet_form.groupUG[i].disabled = true;
        document.cet_form.groupUG[i].checked = false;
        document.cet_form.groupPG[i].disabled = true;
        document.cet_form.groupPG[i].checked = false;
    }
    document.cet_form.groupPG[i].disabled = true;
    document.cet_form.groupPG[i].checked = false;

    document.cet_form.ddlBDC.disabled = true;
    document.cet_form.groupUGFD1.disabled = true;
    document.cet_form.groupUGFD2.disabled = true;
    document.cet_form.groupUGFD3.disabled = true;
    document.cet_form.groupUGFD4.disabled = true;
    document.cet_form.groupUGFD5.disabled = true;
    //document.cet_form.groupPGFR1.disabled = true;
    //document.cet_form.groupPGFR2.disabled = true;

    var tstr = '';
    tstr = randomStrings(7);
    document.getElementById('atoz').value = tstr;
}

function clearForms() {
    for (var i = 0; i < document.forms.length; i++)
        document.forms[i].reset();
}

function checkCheckBox() {
    if (document.cet_form.agree.checked == false) {
        alert('Please check the box to continue.');
        return false;
    }
    else {
        return true;
    }
}

function check(Graduate) {
    var tempGraduate = Graduate.value;
    if (tempGraduate == 1) {
        document.cet_form.ddlBDC.disabled = true;
        document.getElementById("rONE").style.display = "none";
        document.getElementById("rTWO").style.display = "none";
        document.getElementById("rTHREE").style.display = "none";
        document.getElementById("rFOUR").style.display = "none";
        document.getElementById("rFIVE").style.display = "block";
        document.getElementById("rSIX").style.display = "block";
        document.getElementById("rSEVEN").style.display = "block";
        for (i = 0; i < 2; i++) {
            document.cet_form.groupUG[i].checked = false;
            document.cet_form.groupPG[i].disabled = false;
        }
        document.cet_form.groupPG[i].disabled = false;

        document.cet_form.groupUGFD1.disabled = true;
        document.cet_form.groupUGFD2.disabled = true;
        document.cet_form.groupUGFD3.disabled = true;
        document.cet_form.groupUGFD4.disabled = true;
        document.cet_form.groupUGFD5.disabled = true;
        return true;
    }

    if (tempGraduate == 0) {
        document.getElementById("rONE").style.display = "block";
        document.getElementById("rTWO").style.display = "block";
        document.getElementById("rTHREE").style.display = "block";
        document.getElementById("rFOUR").style.display = "block";
        document.getElementById("rFIVE").style.display = "none";
        document.getElementById("rSIX").style.display = "none";
        document.getElementById("rSEVEN").style.display = "none";
        for (i = 0; i < 2; i++) {
            document.cet_form.groupUG[i].disabled = false;
            document.cet_form.groupPG[i].checked = false;
        }
        document.cet_form.groupPG[i].checked = false;
        //document.cet_form.groupPGFR1.disabled = true;
        //document.cet_form.groupPGFR2.disabled = true;
        return true;
    }

    if (tempGraduate == 'BD') {
        document.cet_form.ddlBDC.disabled = false;
        document.cet_form.ddlBDC.selectedIndex = 0;

        document.cet_form.groupUGFD1.selectedIndex = 0;
        document.cet_form.groupUGFD2.selectedIndex = 0;
        document.cet_form.groupUGFD3.selectedIndex = 0;
        document.cet_form.groupUGFD4.selectedIndex = 0;
        document.cet_form.groupUGFD5.selectedIndex = 0;
        /*document.cet_form.groupUGFD1.disabled = false;
        document.cet_form.groupUGFD2.disabled = false;
        document.cet_form.groupUGFD3.disabled = false;
        document.cet_form.groupUGFD4.disabled = false;*/
        return true;
    }

    if (tempGraduate == 'JD') {
        document.cet_form.ddlBDC.disabled = true;
        document.cet_form.groupUGFD1.disabled = true;
        document.cet_form.groupUGFD2.disabled = true;
        document.cet_form.groupUGFD3.disabled = true;
        document.cet_form.groupUGFD4.disabled = true;
        document.cet_form.groupUGFD5.disabled = true;
        return true;
    }

    /*if(tempGraduate=='FC')     {
    document.cet_form.ddlBDC.disabled = true;
    document.cet_form.groupUGFD1.disabled = true;
    document.cet_form.groupUGFD2.disabled = true;
    document.cet_form.groupUGFD3.disabled = true;
    document.cet_form.groupUGFD4.disabled = true;
    return true;
    }   
    
    if (tempGraduate == 'FR') {
    //document.cet_form.groupPGFR1.disabled = false;
    //document.cet_form.groupPGFR2.disabled = false;
    return true;
    }

    if (tempGraduate == 'AMM') {
    //document.cet_form.groupPGFR1.disabled = true;
    //document.cet_form.groupPGFR2.disabled = true;
    return true;
    }

    if (tempGraduate == 'FD') {
    //document.cet_form.groupPGFR1.disabled = true;
    //document.cet_form.groupPGFR2.disabled = true;
    return true;
    }
    */

    if (tempGraduate == 'Fashion Design') {
        var len = document.cet_form.groupUGFD1.length;
        if (len < 6) {
            groupUGFD1.options.length = 0;
            AddSelectOption(groupUGFD1, "-- Select College --", "", true);
            AddSelectOption(groupUGFD1, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD1, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD1, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);
            AddSelectOption(groupUGFD1, "S.N.D.T. Mumbai(Juhu)", "S.N.D.T. Mumbai(Juhu)", false);
            AddSelectOption(groupUGFD1, "S.D.P.S.(Indore)", "S.D.P.S.(Indore)", false);

            groupUGFD2.options.length = 0;
            AddSelectOption(groupUGFD2, "-- Select College --", "", true);
            AddSelectOption(groupUGFD2, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD2, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD2, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);
            AddSelectOption(groupUGFD2, "S.N.D.T. Mumbai(Juhu)", "S.N.D.T. Mumbai(Juhu)", false);
            AddSelectOption(groupUGFD2, "S.D.P.S.(Indore)", "S.D.P.S.(Indore)", false);

            groupUGFD3.options.length = 0;
            AddSelectOption(groupUGFD3, "-- Select College --", "", true);
            AddSelectOption(groupUGFD3, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD3, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD3, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);
            AddSelectOption(groupUGFD3, "S.N.D.T. Mumbai(Juhu)", "S.N.D.T. Mumbai(Juhu)", false);
            AddSelectOption(groupUGFD3, "S.D.P.S.(Indore)", "S.D.P.S.(Indore)", false);    
        }
        document.cet_form.groupUGFD1.disabled = false;
        document.cet_form.groupUGFD2.disabled = false;
        document.cet_form.groupUGFD3.disabled = false;
        document.cet_form.groupUGFD4.disabled = false;
        document.cet_form.groupUGFD5.disabled = false;
        return true;
    }
    if (tempGraduate == 'Fashion Communication') {
        var len = document.cet_form.groupUGFD1.length;
        if (len < 6) {
            groupUGFD1.options.length = 0;
            AddSelectOption(groupUGFD1, "-- Select College --", "", true);
            AddSelectOption(groupUGFD1, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD1, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD1, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);

            groupUGFD2.options.length = 0;
            AddSelectOption(groupUGFD2, "-- Select College --", "", true);
            AddSelectOption(groupUGFD2, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD2, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD2, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);

            groupUGFD3.options.length = 0;
            AddSelectOption(groupUGFD3, "-- Select College --", "", true);
            AddSelectOption(groupUGFD3, "School of Fashion Technology(Pune)", "School of Fashion Technology(Pune)", false);
            AddSelectOption(groupUGFD3, "Satyam Fashion Institute Noida(Delhi)", "Satyam Fashion Institute Noida(Delhi)", false);
            AddSelectOption(groupUGFD3, "Vanita Polytechnic(Varanasi)", "Vanita Polytechnic(Varanasi)", false);
        }
        else {
            for (var i = len - 1; i > 3; i--) {
                document.cet_form.groupUGFD1.options[i] = null;
                document.cet_form.groupUGFD2.options[i] = null;
                document.cet_form.groupUGFD3.options[i] = null;
            } 
        }
        document.cet_form.groupUGFD1.disabled = false;
        document.cet_form.groupUGFD2.disabled = false;
        document.cet_form.groupUGFD3.disabled = false;
        document.cet_form.groupUGFD4.disabled = true;
        document.cet_form.groupUGFD5.disabled = true;
        return true;
    }
    if ((tempGraduate == 'Lifestyle') || (tempGraduate == 'Textile Design')) {
        var len = document.cet_form.groupUGFD1.length;
        for (var i = len - 1; i > 2; i--) {
            document.cet_form.groupUGFD1.options[i] = null;
            document.cet_form.groupUGFD2.options[i] = null;
        }
        document.cet_form.groupUGFD1.disabled = false;
        document.cet_form.groupUGFD2.disabled = false;
        document.cet_form.groupUGFD3.disabled = true;
        document.cet_form.groupUGFD4.disabled = true;
        document.cet_form.groupUGFD5.disabled = true;
        return true;
    }
}

function AddSelectOption(selectObj, text, value, isSelected) {
    if (selectObj != null && selectObj.options != null) {
        selectObj.options[selectObj.options.length] =
            new Option(text, value, false, isSelected);
    }
}

function validate_form() {
    var fname = document.getElementById('fname');
    var mname = document.getElementById('mname');
    var lname = document.getElementById('lname');
    var Place = document.getElementById('Place');
    var ffname = document.getElementById('ffname');
    var fmname = document.getElementById('fmname');
    var flname = document.getElementById('flname');
    var mfname = document.getElementById('mfname');
    var mmname = document.getElementById('mmname');
    var mlname = document.getElementById('mlname');
    var zip = document.getElementById('zip');
    var tele = document.getElementById('tele');
    var mobo = document.getElementById('mobo');
    var email = document.getElementById('email');
    var colofgra = document.getElementById('colofgra');
    var Percentage = document.getElementById('Percentage');
    var subject = document.getElementById('subject');
    var Draft = document.getElementById('Draft');
    var address = document.getElementById('address');

    if (isAlphabet(fname)) {
        if (date()) {
            if (isAlphabet(ffname)) {
                if (isAlphabet(mfname)) {
                    if (addr()) {
                        if (isNumeric(zip)) {
                            if (radio_button_checker()) {
                                if (cet_cen()) {
                                    if (isLetters(Draft)) {
                                        if (dddate()) {
                                            if (checkCheckBox()) {
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

function cet_cen() {
    if (document.cet_form.cet_center.value == 0) {
        alert("Please select CET Exam Center");
        document.cet_form.cet_center.focus();
        return false;
    }
    return true;
}

function dddate() {
    if (!(document.cet_form.dday.value == 0)) {
        if (!(document.cet_form.dmonth.value == 0)) {
            if (!(document.cet_form.dyear.value == 0)) {
                return true;
            } else document.cet_form.dday.focus();
        } else document.cet_form.dday.focus();
    } else document.cet_form.dday.focus();
    alert("Please select valid Date of Demand Draft");
    document.cet_form.dday.focus();
    return false;

}

function addr() {
    if (document.cet_form.address.value == 0) {
        alert("Plaese enter valid Address");
        document.cet_form.address.focus();
        return false;
    }
    return true;
}

function date() {
    if (!(document.cet_form.day.value == 0)) {
        if (!(document.cet_form.month.value == 0)) {
            if (!(document.cet_form.year.value == 0)) {
                return true;
            } else document.cet_form.year.focus();
        } else document.cet_form.month.focus();
    } else document.cet_form.day.focus();
    alert("Please select valid Birth Date");
    document.cet_form.day.focus();
    return false;
}


function radio_button_checker() {
    var a = 0, b;
    document.getElementById('Degree').value = '';
    document.getElementById('Course').value = '';
    document.getElementById('prefer1').value = '';
    document.getElementById('prefer2').value = '';
    document.getElementById('prefer3').value = '';
    document.getElementById('prefer4').value = '';
    document.getElementById('prefer5').value = '';

    if (document.cet_form.Graduate[0].checked) {
        if (document.cet_form.groupUG[0].checked) {
            document.getElementById('Degree').value = "B. Design";
            document.getElementById('Course').value = document.cet_form.ddlBDC.value;
            if (document.cet_form.ddlBDC.value != "") {
                if (document.cet_form.groupUGFD1.value == '') { b = 0; }
                else { document.getElementById('prefer1').value = document.cet_form.groupUGFD1.value; a++; }

                if (document.cet_form.groupUGFD2.value == '') { b = 0; }
                else { document.getElementById('prefer2').value = document.cet_form.groupUGFD2.value; a++; }

                if (document.cet_form.groupUGFD3.value == '') { b = 0; }
                else { document.getElementById('prefer3').value = document.cet_form.groupUGFD3.value; a++; }

                if (document.cet_form.groupUGFD4.value == '') { b = 0; }
                else { document.getElementById('prefer4').value = document.cet_form.groupUGFD4.value; a++; }

                if (document.cet_form.groupUGFD5.value == '') { b = 0; }
                else { document.getElementById('prefer5').value = document.cet_form.groupUGFD5.value; a++; }

                if (!a) {
                    alert("Please select college");
                    return false;
                }
            }
            else {
                alert("Please select course");
                return false;
            }
            return true;
        }
        /* These courses merged into the B.Designs hence commented
        if(document.cet_form.groupUG[1].checked)    {
        document.getElementById('Course').value = "B.Sc. Fashion communication";
        document.getElementById('prefer1').value = "Satyam Fashion Institute Noida(Delhi)";
        alert("Please select college");
        return true;
        }
        if(document.cet_form.groupUG[2].checked)    {
        document.getElementById('Course').value = " B.Sc. Apparel Manufacturing & Merchandising";
        document.getElementById('prefer1').value = "Satyam Fashion Institute Noida(Delhi)";
        return true;
        }
        */
        if (document.cet_form.groupUG[1].checked) {
            document.getElementById('Course').value = "B.Sc. Jewelry Design";
            document.getElementById('prefer1').value = "S.N.D.T. Mumbai(Juhu)";
            return true;
        }
    }

    if (document.cet_form.Graduate[1].checked) {
        if (document.cet_form.groupPG[0].checked) {
            document.getElementById('Course').value = "Fashion Retail Management";
            if (document.cet_form.groupPGFR1.value == '') { b = 0; }
            else { document.getElementById('prefer1').value = document.cet_form.groupPGFR1.value; a++; }

            if (document.cet_form.groupPGFR2.value == '') { b = 0; }
            else { document.getElementById('prefer2').value = document.cet_form.groupPGFR2.value; a++; }
            if (!a) {
                alert("Please select college");
                return false;
            }
            return true;
        }
        if (document.cet_form.groupPG[1].checked) {
            document.getElementById('Course').value = "Apparel Manufacturing & Merchandising";
            document.getElementById('prefer1').value = "Satyam Fashion Institute Noida(Delhi)";
            return true;
        }
        if (document.cet_form.groupPG[2].checked) {
            document.getElementById('Course').value = "Fashion Design";
            document.getElementById('prefer1').value = "Satyam Fashion Institute Noida(Delhi)";
            return true;
        }
    }

    alert("Please select course");
    document.cet_form.Graduate[0].focus();
    return false;
}

function isAlphabet(elem) {
    var alphaExp = /^[a-zA-Z]/;
    if (trim(elem.value).match(alphaExp)) {
        return true;
    }
    else {
        alert("Please enter Name");
        elem.focus();
        elem.value = "";
        return false;
    }
}

function isNumeric(elem) {
    var zipexp = /^[0-9]/;
    if (trim(elem.value).match(zipexp)) {
        return true;
    }
    else {
        alert("Please enter valid zip code");
        elem.focus();
        elem.value = "";
        return false;
    }
}

function emailValidator(elem) {
    var emailExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
    if (trim(elem.value).match(emailExp)) {
        return true;
    }
    else {
        alert("Please enter valid E-mail");
        elem.focus();
        elem.value = '';
        return false;
    }
}

function trim(str) {
    var temp = str;
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(temp)) {
        temp = temp.replace(obj, '$2');
    }
    var obj = / +/g;
    temp = temp.replace(obj, " ");
    if (temp == " ") {
        temp = "";
    }
    return temp;
}

function isLetters(elem) {
    var draft = trim(elem.value);
    if (draft == "") {
        alert("Please enter valid Demand Draft no.");
        elem.focus();
        return false;
    }
    return true;
}
