/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var clear = "true";
var clearMessage = "true";

//submit form data.
function submitCommands(formName, cmd) {
    var form_name = document.forms[formName];
    form_name.command.value = cmd;
    form_name.submit();
}

function submit(formName){
    var form_name = document.forms[formName];
    form_name.action.value = "Submit";
    form_name.submit();
}   
//Update register mode.
function updateregisterSection(cmd){
   var contextPath = document.getElementById('contextPath').value;
     document.forms['contactProviderForm'].action.value = cmd;
    callAjax_registered_section(contextPath+'/contactProvider');
}
//Update register mode at user login.
function updateregisterSectionFromUserLogin(cmd){
    var contextPath = document.getElementById('contextPath').value;
    document.forms['providerRegistrationForm'].command.value = cmd;    
    callAjax_registered_section(contextPath+'/providerLogin');
    
}
//Update register mode at demand aggregation.
function updateregisterSectionFromDemanAggregation(cmd){
    var contextPath = document.getElementById('contextPath').value;
    document.forms['demandAggregationForm'].command.value = cmd;
    document.forms['demandAggregationForm'].action.value = "";
    callAjax_registered_section(contextPath+'/demandAggregation');

}
function updateregisterSectionFromStudentInformation(cmd){
    var contextPath = document.getElementById('contextPath').value;     
    //document.forms['captureStudentInformationForm'].command.value = cmd;    
    document.forms['captureStudentInformationForm'].action.value = cmd;
   
    
    //doNothing('captureStudentInformationForm', cmd); 
    callAjax_registered_section(contextPath+'/askInformation','captureStudentInformationForm');
    document.forms['captureStudentInformationForm'].submit();
}

//Check length of textbox
function textCounter(formName,field) { 
    var data=document.forms[formName][field].value;
    if (data.length >= 300)
    {
        data = data.substring(0, 290);
        document.forms[formName][field].value = data;
        alert('Too much data in the text box!');
    }
}
function textCountMailBody(formName,field,limit) {
    var data=document.forms[formName][field].value;
    if (data.length >= limit)
    {
        data = data.substring(0, limit-2);
        document.forms[formName][field].value = data;
        alert('Too much data in the text box!');
    }
}
//Remove Default Value
function RemoveDefaultValue(formName,field) {
    if(clear == "true")
    {
        clear = "false";
        document.forms[formName][field].value="";
        document.forms[formName][field].style.color = "black";
    }
}
function AddDefaultValue(formName,field)
{
    if(document.forms[formName][field].value == "" || document.forms[formName][field].trim().length == 0 ) {
        document.forms[formName][field].value="Enter the Courses, separated by comma Ex:c, c++, java";
        clear = "true";
        document.forms[formName][field].style.color = "#999999";
    }
}


//Remove Default Message
function RemoveDefaultMessage(formName,field) {
    if(clearMessage == "true")
    {
        clearMessage = "false";
        document.forms[formName][field].value="";
        document.forms[formName][field].style.color = "black";
    }
}

function AddDefaultMessage(formName,field)
{
    if(document.forms[formName][field].value == "" || document.forms[formName][field].trim().length == 0 ) {
        document.forms[formName][field].value="Budget,more details of course you are looking for,time and day preferences";
        clearMessage = "true";
        document.forms[formName][field].style.color = "#999999";
    }
}

//Cancel registration.
function checkCancelCondition(formName,path){
    var path = document.forms[formName][path].value;
    var answer = confirm("Are you sure, you want to cancel the registration?");
    if (answer ==true){
        location = path + '/index.jsp'
    }

}

function extendForm(){
    var thisObj = document.getElementById("form").style;
    thisObj.visibility = "visible";
    thisObj.display="block";
}

function compressForm(){
    var thisObj = document.getElementById("form").style;
    thisObj.visibility = "hidden";
    thisObj.display="none";
}

// Select training_category, authorization, certification
function movetoright(optiontype){
    var leftSelectListName = 'select_available_' + optiontype;
    var rightSelectListName = 'select_chosen_' + optiontype;

    // Add to right list.
    var leftSelectList = document.getElementById(leftSelectListName);
    var rightSelectList = document.getElementById(rightSelectListName);
    var options = leftSelectList.options;
    var value = '';
    for(var index = options.length - 1; index >= 0; index --){
        if(options[index].selected){
            var elOptNew = document.createElement('option');

            elOptNew.text = options[index].text;

            elOptNew.value = options[index].value;
            value =  options[index].value + options[index].text;
            elOptNew.selected='selected';
            try{

                rightSelectList.add(elOptNew, null);
            }catch(ex) {
                rightSelectList.add(elOptNew, rightSelectList.selectedIndex); // IE only
            }
            leftSelectList.remove(index);
        }
    }
}


// Select training_category, authorization, certification
function movetoleft(optiontype){
    var leftSelectListName = 'select_available_' + optiontype;
    var rightSelectListName = 'select_chosen_' + optiontype;

    // Add to right list.
    var leftSelectList = document.getElementById(leftSelectListName);
    var rightSelectList = document.getElementById(rightSelectListName);
    var options = rightSelectList.options;
    for(var i = options.length - 1; i >= 0; i --){
        if(options[i].selected){
            var elOptNew = document.createElement('option');
            elOptNew.text = options[i].text;
            elOptNew.value = options[i].value;
            value =  options[i].value + '@' + options[i].text;
            try{
                leftSelectList.add(elOptNew, null);

            }catch(ex) {
                leftSelectList.add(elOptNew, leftSelectList.selectedIndex); // IE only
            }
            rightSelectList.remove(i);

        }
    }
}

// Submit command for Registration step 3
function submitRegistrationCommand(formName,cmd){
    for(var index=0; index< document.forms[formName].selectedTopics.options.length; index ++)
    {
        document.forms[formName].selectedTopics.options[index].selected = true;
    }
    for(var index1=0; index1< document.forms[formName].selectedCerts.options.length; index1 ++)
    {
        document.forms[formName].selectedCerts.options[index1].selected = true;
    }
    for(var index2=0; index2< document.forms[formName].selectedAuthProviders.options.length; index2 ++)
    {
        document.forms[formName].selectedAuthProviders.options[index2].selected = true;
    }
    document.forms[formName].command.value=cmd;
    document.forms[formName].submit();
}

//Describe Course Delivery Format. 
function checkDelivery(){
    alert("Instructor led - public --->  Choose this for searching all the public courses.\n\nInstructor led -Private ----> Choose this for searching the private courses offered by provider,\nwhere course schedule is not finalised yet.\n\nOnline Learning - Courses Delivered through Internet.\n\nCD-ROM - Courses Delivered through CD-ROM.");
}

// Edit action
function Edit(formName, action){
    var form_name =document.forms[formName];
    form_name.action.value = action ;
    form_name.submit();
}

function registerUser(formName, action){
    var form_name =document.forms[formName];
    form_name.action.value = action ;
    form_name.submit();
}

//Add course or update course
function Create(formName, action){
    var form_name = document.forms[formName];
    form_name.action.value = action;
    form_name.ajax.value = false;
    form_name.submit();
}

function textCounter(formName,field,datalength) {
    limit = datalength;
    var data = document.forms[formName][field].value;
    if (data.length >= limit)
    {
        data = data.substring(0, limit-10);
        document.forms[formName][field].value = data;
        alert('Too much data in the text box!');
    }
}

//Add other location 
function submitSelectedString(cmd,formName,selectItem) {
    var i;
    var j = 0;
    var outlist = "";
    for (i = 0; i < document.forms[formName].elements[selectItem].options.length; i++) {
        if (document.forms[formName].elements[selectItem].options[i].selected) {
            outlist = outlist + document.forms[formName].elements[selectItem].options[i].value;
            outlist = outlist + "|";
        }
    }
    document.forms[formName].command.value=cmd;
    document.forms[formName].selectedOption.value=outlist;
    document.forms[formName].submit();
}

//remove keyword.
function submitSelectedItem(cmd,formName,selectItem) {
    var i;
    var outlist = "";
    for (i = 0; i < document.forms[formName].elements[selectItem].options.length; i++) {
        if (document.forms[formName].elements[selectItem].options[i].selected) {
            outlist = outlist + document.forms[formName].elements[selectItem].options[i].value;
            outlist = outlist + "|";
        }
    }
    for(var index=0; index< document.forms[formName].selectedTopics.options.length; index ++)
    {
        document.forms[formName].selectedTopics.options[index].selected = true;
    }
    for(var index=0; index< document.forms[formName].selectedCerts.options.length; index ++)
    {
        document.forms[formName].selectedCerts.options[index].selected = true;
    }
    for(var index=0; index< document.forms[formName].selectedAuthProviders.options.length; index ++)
    {
        document.forms[formName].selectedAuthProviders.options[index].selected = true;
    }
    document.forms[formName].command.value=cmd;
    document.forms[formName].selectedOption.value=outlist;
    document.forms[formName].submit();
}

  
//Home page submit
function submitHomeCommand(formName,cmd, currentPage) {
    if(cmd == "homeSearch" || cmd == "GetPage"){
        document.forms[formName].elements["command"].value = cmd ;
        document.forms[formName].elements["currentPage"].value = currentPage ;
        document.forms[formName].elements["ajax"].value = 'false';
        document.forms[formName].submit();
    }
}

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, 'name', 'toolbar=0,scrollbars=0,addressbar=0,location=0,statusbar=0,menubar=0,resizable=1,width=575,height=650,left = 229.5,top = 84');");
}

function popUp2(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, 'name', 'toolbar=0,scrollbars=0,addressbar=0,location=0,statusbar=0,menubar=0,resizable=1,width=590,height=600,left = 229.5,top = 84');");
}
function popUp3(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, 'name', 'toolbar=0,scrollbars=0,addressbar=0,location=0,statusbar=0,menubar=0,resizable=1,width=590,height=540,left = 229.5,top = 84');");
}

function submitDemandAggregation(formName,cmd){
    var status="true";
    if(document.forms[formName]["user.firstName"].value == "")
    {
        status="false";
    }
    if(document.forms[formName]["selectedTopics"].value == "" || document.forms[formName]["selectedTopics"].length<=0 )
    {
        status="false";
    }
    if(document.forms[formName]["user.mobile"].value == ""){
        status="false";
    }
    if( document.forms[formName]["user.email"].value == ""){
        status="false";
    }
    if(document.forms[formName]["user.state"].value == ""){
        status="false";
    }
    if(document.forms[formName]["user.city"].value == ""){
        status="false";
    }
  
    if(document.forms[formName]["user.courseName"].value == ""){
        status="false";
    }
    if(cmd == "validate") {
        if(document.forms[formName]["loginId"].value == ""){
            status="false";
        }
        if(document.forms[formName]["password"].value == ""){
            status="false";
        }
    }
    if(status == "false"){
        alert("Please enter mandatory fields");
    }else{
        var form_name = document.forms[formName];
        form_name.action.value = cmd;
        form_name.submit(); 
    }

}

//get selected page data.
function submitPage(formName,currentPage) {
    document.forms[formName].elements["page"].value = currentPage ;
    document.forms[formName].submit();
}
//cancel search agent.
function submitSearchAgentForm(click){
    if(click=="Cancel"){
        document.courseSearchForm.action.value="LIST";
    }
}
function yesClicked(type)
{
    var contextPath = document.getElementById('contextPath').value;
    if(type == 'vendor'){
        window.location=contextPath+"/editVendorRegistration?"
    }else{
        window.location=contextPath+"/editStudentRegistration?"
    }
}
function noClicked()
{
    var contextPath = document.getElementById('contextPath').value;
    window.location=contextPath+"/index.jsp"
}
//Used display edit location.
function submitPageCommand(formName,cmd, currentPage) {
    if(cmd == "GetPage"){
        document.forms[formName].elements["command"].value = cmd ;
        document.forms[formName]["currentPage"].value= currentPage;
        document.forms[formName].submit();
    }
}

function submitCoursePage(formName,cmd,currentPage,page) {
    if(page == "Next"){
        ++ document.forms[formName]["next"].value;
        ++ document.forms[formName]["previous"].value;
        currentPage = document.forms[formName]["next"].value;
    } else if(page == "Previous") {
        -- document.forms[formName]["next"].value;
        -- document.forms[formName]["previous"].value;
        currentPage = document.forms[formName]["next"].value;
    } else if(page == "First" || page == "Last") {
        document.forms[formName]["next"].value = currentPage;
        document.forms[formName]["previous"].value = currentPage - 1;;
       
    }
    if(cmd == "GetPage"){
        document.forms[formName].elements["command"].value = cmd ;
        document.forms[formName]["currentPage"].value= currentPage;
        document.forms[formName].submit();
    }
}


function confirmCourse()
{
    var contextPath = document.getElementById('contextPath').value;
    answer = confirm("Do you want to add more courses ?")
    if (answer !=0){
        location = contextPath+"/UL/editCourse/Manage%20Course/Add%20Course"
    }
}
//provider preview plan.
function submitData(formName, cmd) {
    var form_name = document.forms[formName];
    form_name.command.value = cmd;
    form_name.submit();
}

function submitCommand(formName,cmd){
    document.forms[formName].command.value=cmd;
    document.forms[formName].submit();
}
//Registraion steps complete and submit user data.
function submitRegCommand(formName, cmd , type) {
    var frm = document.forms[formName];
    var userType;
    if (cmd == 'SubmitUser') {
        for (i = 0; i < frm.length; i++) {
            if (frm.elements[i].type == "checkbox") {
                if(type == "student"){
                    userType = 'student.agreedToTermsAndCondition';
                }else if(type == "vendor"){
                    userType = 'vendor.agreedToTermsAndCondition';
                }
                if (frm.elements[i].name == userType) {
                    if (frm.elements[i].checked == false) {
                        alert("Please read and select terms and condition check box");
                        return;
                    }
                }
            }
        }
    }
    frm.command.value = cmd;
    frm.submit();
}
// Open terms and conditions page.
function NewWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left='
    + winl + ',scrollbars=' + scroll + ',resizable'
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) {
        win.window.focus();
    }
}
// for blink support in IE.
var dispVal=true;
function blink(){
    if (dispVal) {
        dispVal=false;
        document.getElementById("viewTrainingList").style.visibility="visible";
    }
    else {
        dispVal=true;
        document.getElementById("viewTrainingList").style.visibility="hidden";
    }
}
//Email send to friend.
function showMessage(contextPath, action){ 
    if(document.forms[0]["name"].value == null || document.forms[0]["email"].value == null || document.forms[0]["name"].value == "" || document.forms[0]["email"].value == ""){
        alert("Please enter mandatory fields");
    } else {
        alert("Your Email has been sent to a particular recipients");
        callAjax_send_mail(contextPath + action +"?command=sendMail&name="+this.document.forms[0]["name"].value+"&email="+this.document.forms[0]["email"].value); 
        window.close();
} 
    
}

//For rating provider.
var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// Rollover for image Stars //
function rating(num){
    sMax = 0;	// Isthe maximum number of stars
    for(n=0; n<num.parentNode.childNodes.length; n++){
        if(num.parentNode.childNodes[n].nodeName == "A"){
            sMax++;
        }
    }

    if(!rated){
        s = num.id.replace("_", ''); // Get the selected star
        a = 0;
        for(i=1; i<=sMax; i++){
            if(i<=s){
                document.getElementById("_"+i).className = "on";
                document.getElementById("rateStatus").innerHTML = num.title;
                holder = a+1;
                a++;
            }else{
                document.getElementById("_"+i).className = "";
            }
        }
    }
}

// For when you roll out of the the whole thing //
function off(star){
    if(!rated){
        if(!preSet){
            for(i=1; i<=sMax; i++){
                document.getElementById("_"+i).className = "";
                document.getElementById("rateStatus").innerHTML = star.parentNode.title;
            }
        }else{
            rating(preSet);
            document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
        }
    }
}

// When you actually rate something //
function rateIt(star){
    if(!rated){
        document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML + " :: "+star.title;
        var form_name = window.document.vendorForm;
        form_name["ratings"].value = star.title;
        preSet = star;
        rated=1;
        sendRate(star);
        rating(star);

    }
}
function popUp1(url)
{
    newwindow=window.open(url,'name','height=485,width=583,resizable=yes,scrollbars=yes');
}
 
function setLocationHash(){
//  alert(document.getElementById('search_result').innerHTML= window.location.hash);
}
