var MAX_LENGTH =4294967295-1000
String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};

function showPreview() {   
    if(validateForm()==true) {
        document.editForm.action="/wiki/wiki/wiki_preview"
        document.editForm.submit()
    }
       
}

function  getSectionForm() {
        
    div_id++;
    //alert(div_id)
    var section_form="<h6 style='border-top:1px solid black;font-size:10pt;'>Choose a pre-set section title or type in your own</h6>"+
    "<select id='cmb_section_"+div_id+"' name='cmb_section_"+div_id+"'><option value=''>Choose a section</option>"+
    		options+
    	    "<option value='0'>Other</option></select>"+   
    "&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onclick='removeSection("+div_id+")'>delete</a>"+   
    "<br /><input type='text' id='txt_section_"+div_id+"' style='width:150px;'><br /><br />"+
    "Write the content for the section here"+
    "<br /><textarea id='content_"+div_id+"' name='content_"+div_id+"'  style='width:400px; height:250px;'></textarea><br/><br/>"+
    "<input type='hidden' name='section_"+div_id+"' id='section_"+div_id+"'/></div>"
    
    return section_form
}
function addSection() {
    if(getContentLength() >=MAX_LENGTH ) {
        alert("Wiki Content is too large")
    }else {
        dv=document.createElement('div');     
        dv.innerHTML=getSectionForm();
        dv.id='wiki_section_'+div_id;
        document.getElementById('section_div').appendChild(dv);
        tinyMCE.execCommand("mceAddControl", true, 'content_'+div_id);
    }
   
}
function removeSection(id) {
    div=document.getElementById('section_div')
    div.removeChild(document.getElementById('wiki_section_'+id))
}
function getContentLength() {
    var content_length=0;
    for(var i=0;i<=div_id;i++) {
        if(document.getElementById('cmb_section_'+i)) {
            if(document.getElementById('cmb_section_'+i).value == '0') {
                document.getElementById('section_'+i).value= document.getElementById('txt_section_'+i).value
                
            } else {
                document.getElementById('section_'+i).value=document.getElementById('cmb_section_'+i).value
            }  
            content_length+= document.getElementById('section_'+i).value.length
        }
        if(document.getElementById('content_'+i)) {               
            content_length+= tinyMCE.getInstanceById('content_'+i).getDoc().body.innerHTML.length
        }
     }
     //content_length+=document.getElementById('subject').value.length
     if (document.getElementById('subject')){
       if(document.getElementById('subject').value!=null && document.getElementById('subject').value!=""){
         content_length+=document.getElementById('subject').value.length;
       }
     }
     content_length+=tinyMCE.getInstanceById('introduction').getDoc().body.innerHTML.length
     return content_length
                      
}
function validateForm(action) {  
    if(getContentLength() >= MAX_LENGTH ) {
        alert("Wiki Content is too large")
        return false
    }else {
        try{
            document.editForm.action="/wiki/wiki/"+action;
            for(var i=0;i<=div_id;i++) {
                if(document.getElementById('cmb_section_'+i)) {
                    if(document.getElementById('cmb_section_'+i).value == '0') {
                        document.getElementById('section_'+i).value= document.getElementById('txt_section_'+i).value
                    } else {
                        document.getElementById('section_'+i).value=document.getElementById('cmb_section_'+i).value
                    }  
                   
                 }
                //alert(document.getElementById('section_'+i).value)
               
            }
            document.getElementById('section_count').value=div_id
            //alert(document.getElementById('section_count').value)
            var msg=''
            //if(document.getElementById("school_id") && document.getElementById("school_id").value==0) {
              //  msg="Please Select a School"
            //}
           // else if(document.getElementById('introduction').value.trim().length > 500) {
          //      msg="Maximum allowed characters is 500"
          //  }
            //if {
                for(var i=0;i<=div_id;i++) {
                    if(document.getElementById('cmb_section_'+i)) {
                      if(document.getElementById('cmb_section_'+i).value == '0' &&
                      document.getElementById('txt_section_'+i).value.trim().length==0) {               
                        msg="Please Select a section"
                            break;
                      }
                      
                      /*if(document.getElementById('content_'+i).value.trim().length==0) {               
                        msg="Please add contents for section "
                           break;
                      }*/
                    }
                } 
            //}
            //alert("no "+msg)
            if(msg !='') {
                alert(msg)
                return false
            } else 
            return true
            //return false
        }catch(e) {
            alert(e)
        }
     }
}

function go_to_wiki_section() {
  page_name = document.getElementById('wiki_page').value;
  section_name = document.getElementById('wiki_section').value;
  window.location.href = '/wiki/wiki/show/'+page_name+'#h5_section_'+section_name;
}

function wiki_search() {
  search_query = document.getElementById('query').value;
  parent.location.href="/wiki/wiki/search?query="+search_query;
}

function search_wiki_section() {
  page_name = document.getElementById('wiki_section').value;
  section_name = document.getElementById('wiki_section')[document.getElementById('wiki_section').selectedIndex].text;
  if (page_name.length > 0) {
    window.location.href = '/wiki/wiki/show/'+page_name+'#h5_section_'+section_name;
  }
  else {
    alert('Please select a topic')
  }
}
function openConfirm() {
    Dialog.confirm("This wiki already exists ", 
       { width:250, className: "alphacube", okLabel: "Edit", cancelLabel:"View",
       cancel:function(win) {
            if($("from").value=='') {
                parent.location.href="/wiki/wiki/show/"+$("subject").value 
            }else {
                parent.location.href="/wiki/wiki/revisions_vcms/"+$("subject").value 
            }
            
       }, 
       ok:function(win) {
            if($("from").value=='') {
                parent.location.href="/wiki/wiki/edit/"+$("subject").value
            }else {
                parent.location.href="/wiki/wiki/edit_vcms/"+$("subject").value
            }
               
       } 
       
       })
}
function checkSchoolName() {
    new Ajax.Request('/wiki/wiki/get_school_name/check', {
      method: 'get',
      onComplete: function(transport) {
            if(transport.responseText =='1') {
                openConfirm()
            }
      },
      
      /*onSuccess: function(transport){
        var response = transport.responseText || "no response text";
        alert("Success! \n\n" + response);
     },
     onFailure: function(){ alert('Something went wrong...') },*/
     parameters: {school_name: $("subject").value}
     });

}
