jQuery(document).ready(function($) {
   	  
	var COOKIE_NAME = 'Expand ' + document.title;
    var options = {
        expires : 10,
        path : '/'
    };
   
   /* CourseFinder v3 Search Button */
    if ($(".search-icon")) {
        $(".search-icon").click(function() {
            $("#search").submit();
        });
    }
   
    if ($('a.tips')) {
      	$('a.tips').cluetip({arrows: true});
    }
   
   
    if ($('#houdini')) {  
    	  $('#houdini').cluetip({
    		    splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
    		                     // ...and split the contents into separate divs where there is a "|"
    		    showTitle: true
    			// hide the clueTip's heading
    	  });    
    }
   
    if ($(".cHeadSlow, .cHeadFast, .cHeadInstant")) {  
	    $(".cHeadSlow, .cHeadFast, .cHeadInstant").each(function() {
	
	        if ($.cookie(COOKIE_NAME + " - " + $(this).attr("id")) == 'X') {
	
	            $("+ .cBody ", this).show();
	            $(this).toggleClass("cPlus");
	        }
	    });
    }
    
    if ($(".cHeadSlow2, .cHeadFast2, .cHeadInstant2")) {  
	    $(".cHeadSlow2, .cHeadFast2, .cHeadInstant2").each(function() {
	
	        if ($.cookie(COOKIE_NAME + " - " + $(this).attr("id")) == 'X') {
	
	            $("+ .cBody ", this).show();
	            $(this).toggleClass("cPlus2");
	        }
	    });
    }
    
    if ($(".cHeadSlow")) {
	    $(".cHeadSlow").click(function() {
	
	        $("+ .cBody ", this).slideToggle("slow");
	        $(this).toggleClass("cPlus");
	
	        if ($(this).attr("id")) {
	
	            if (!$(this).hasClass("cPlus")) {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
	            } else {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
	            }
	        }
	    });
    }
    if ($(".cHeadSlow2")) {
	    $(".cHeadSlow2").click(function() {
	
	        $("+ .cBody ", this).slideToggle("slow");
	        $(this).toggleClass("cPlus2");
	
	        if ($(this).attr("id")) {
	
	            if (!$(this).hasClass("cPlus2")) {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
	            } else {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
	            }
	        }
	    });
    }
    if ($(".cHeadFast")) {
	    $(".cHeadFast").click(function() {
	
	        $("+ .cBody ", this).slideToggle("fast");
	        $(this).toggleClass("cPlus");
	
	        if ($(this).attr("id")) {
	
	            if (!$(this).hasClass("cPlus")) {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
	            } else {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
	            }
	        }
	    });
    }
    if ($(".cHeadFast2")) {
	    $(".cHeadFast2").click(function() {
	
	        $("+ .cBody ", this).slideToggle("fast");
	        $(this).toggleClass("cPlus2");
	
	        if ($(this).attr("id")) {
	
	            if (!$(this).hasClass("cPlus2")) {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
	            } else {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
	            }
	        }
	    });
    }
    
    if ($(".cHeadFast3")) {
        $(".cHeadFast3").click(function() {
    
            $("+ .cBody ", this).slideToggle("fast");
            $(this).toggleClass("cPlus3");
    
            if ($(this).attr("id")) {
    
                if (!$(this).hasClass("cPlus3")) {
                    $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
                } else {
                    $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
                }
            }
        });
    }
    
    if ($(".cHeadInstant")) {
	    $(".cHeadInstant").click(function() {
	
	        $("+ .cBody ", this).toggle();
	        $(this).toggleClass("cPlus");
	
	        if ($(this).attr("id")) {
	
	            if (!$(this).hasClass("cPlus")) {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), 'X', options);
	            } else {
	                $.cookie(COOKIE_NAME + " - " + $(this).attr("id"), '', options);
	            }
	        }
	    });
    }
    
    /** Check if file uploads are overwritten **/
    $("#submit", ".editformContainer").click(function() {
    //$("#COURSEWORKGUIDANCEPDF").change(function () {
        if ($("#COURSEWORKGUIDANCEPDFDELETE").val() == "false" &&  $("#COURSEWORKGUIDANCEPDF").val() != '') {
            jConfirm("There is an existing Coursework Guidance PDF already uploaded.<br/>"+
                     "Are you sure you wish to overwrite the old PDF<br/>"+
                     "with this one: <strong>" + $("#COURSEWORKGUIDANCEPDF").val()  + "</strong>?", 
                     "Coursework Guidance PDF File Exists", function (r) {
                        if (r) {
                           $("#COURSEWORKGUIDANCEPDFDELETE").val('true');    
                           $("#COURSEWORKGUIDANCEPDFDISPLAY").hide();                       
                           return true;
                        } else {
                           $("#COURSEWORKGUIDANCEPDFDELETE").val('');                                                       
                        }  
                     });
            return false;
        }   
    });
      
});

function TextAreaCounter(currentDom, limit) {
	
	if (tinyMCE.activeEditor.id == 'WHATYOUWILLSTUDY')
		return;
	
	//var temp = currentDom.getBody().innerHTML;
    var temp = currentDom.contentDocument.documentElement.textContent;
    var strip = '';
    if (temp) {
        strip = temp.replace(/(&lt;([^&gt;]+)&gt;)/ig,"");
        strip = jQuery.trim(strip);    
    }        
	var text = "";
	var countval = 0;
	if (strip != '') {
		//countval = strip.split(' ').length;				
        countval = strip.length;                
	}
	//tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
	
	if (countval > limit+1) {
		jAlert("Please limit your input to "+limit+" characters minimum.", jQuery(currentDom).attr('id'));	
		jQuery("input[name='SubmitValid']").val("failed");
	} else {
		jQuery("input[name='SubmitValid']").val("success");		
	}
	
	
}
function DescriptionCounter(currentDom, limit) {
    
    //var temp = currentDom.getBody().innerHTML;
    var temp = currentDom.contentDocument.documentElement.textContent;
    var strip = '';
    if (temp) {
        strip = temp.replace(/(&lt;([^&gt;]+)&gt;)/ig,"");
        strip = jQuery.trim(strip);    
    }   
    var text = "";
    var countval = 0;
    if (strip != '') {
        countval = strip.length;                
    }
    //tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
    
    if (countval > limit) {
        jAlert("Display character count limit of "+limit+" characters reached!<br/>Remaining content is visible only by clicking the<br/><a href='#'>more</a> link in the Browse page.", "Description of Course");
        jQuery("input[name='SubmitValid']").val("failed");
    } else {
        jQuery("input[name='SubmitValid']").val("success");        
    }
    
    
}
function deleteImage(name, div_id) {
    
    if (confirm('Are you sure you wish to delete this image?')) {
        jQuery("input[id='"+name+"DELETE']").val(true);
        jQuery("#"+div_id+"").slideToggle('fast');
        jQuery("#"+name+"DELETEIMAGE").hide();
        jQuery("#"+name+"IMAGE_OLD").val('');
        jQuery(".info-box").hide();        
        jQuery("#IMAGEDELETEINFO").show();        
        return true;
    } else {
        return false;
    }
}
function deleteProgram(name, div_id) {
    
    //alert(jQuery("input[name='"+name+"']").val());
        
    if (confirm('Are you sure you wish to delete this pdf program?')) {    	
        jQuery("input[id='"+name+"DELETE']").val(true);
        jQuery("#"+div_id+"").slideToggle('fast');
        jQuery("#"+name+"DELETEIMAGE").hide();
        jQuery("#"+name+"_OLD").val('');
        jQuery(".info-box").hide();        
        jQuery("#PROGRAMDELETEINFO").show();        
        return true;
    } else {
        return false;
    }
}

function deleteFile(name, div_id) {
    
    //alert(jQuery("input[name='"+name+"']").val());
        
    if (confirm('Are you sure you wish to delete this pdf file?')) {        
        jQuery("input[id='"+name+"DELETE']").val(true);
        jQuery("#"+div_id+"").slideToggle('fast');
        jQuery("#"+name+"DELETEIMAGE").hide();
        jQuery("#"+name+"_OLD").val('');
        jQuery(".info-box").hide();        
        jQuery("#FILEDELETEINFO").show();        
        return true;
    } else {
        return false;
    }
}

function confirmDiscontinue(name) {
    jConfirm("Are you sure you wish to discontinue this course:<br/><strong>" + name + "</strong>?", "Confirm Discontinue", function(result){
        //alert(name+'\r\n'+form+'\r\n'+result);
        if (result) {
            jQuery("form[id='EditAll']").submit();
            return true;
        } else {
            return false;
        }
  });
  //return jConfirm("Are you sure you wish to discontinue this course:<br/><strong>" + name + "</strong>?", "Confirm Discontinue");
  
}
function confirmRestore(name) {
    jConfirm("Are you sure you wish to restore this course:<br/><strong>" + name + "</strong>?", "Confirm Discontinue", function(result){
        //alert(name+'\r\n'+form+'\r\n'+result);
        if (result) {
            jQuery("form[id='EditAll']").submit();
            return true;
        } else {
            return false;
        }
  });
  //return jConfirm("Are you sure you wish to discontinue this course:<br/><strong>" + name + "</strong>?", "Confirm Discontinue");
  
}

function toggleCheck(checkbox) {
    
    if (jQuery("#"+checkbox+":checked").val() != null) {
        jQuery("#"+checkbox).attr('checked', true);                
        jQuery("#"+checkbox).val('1');           
        //jQuery("input[type='text'], input[type='file'], select").hide();
        //jQuery("*[id^=UAC], *[id^=DESC], *[id^=KEY], *[id^=UAC], *[id^=KEY], *[id^=SUITABLE],").hide();
        
        if (checkbox == 'ISALIAS') {
            jQuery("dt, dd").hide();     
            jQuery("*[id^=CourseInfoLabel], *[id^=AREAOFINTEREST], *[id^=Html4], *[id^=submit]").show();                                                                            
        }
    } else {
        jQuery("#"+checkbox).attr('checked', false);       
        jQuery("#"+checkbox).removeAttr('checked');
        jQuery("#"+checkbox).val('0');                               
        
        if (checkbox == 'ISALIAS') {
            jQuery("dt, dd").show(); 
            jQuery("*[id^=CourseInfoLabel], *[id^=AREAOFINTEREST], *[id^=Html4], *[id^=submit]").show();            
        }
    }   
    
}

function reloadUrl(anchor) {
    var pathname = '';
    pathname += jQuery(location).attr('href');
    var from = pathname.search("#");
    if (from > 0) {
        pathname = pathname.substring(0, from);
    }
   window.location = pathname + '#' + anchor;  
   return true;
}

function comparePasswords() {
    if (jQuery("#newpassword").val() == jQuery("#confirmpassword").val()) {
        return true;
    } else {
        return false;
    }
    
}

function selectSearch(year, form) {
    if (year.val() == '2010') {       
        jQuery("#SearchText").val( jQuery("#query").val() );
        
        if (jQuery("[#meta_v_sand]:checked").val() == "Postgraduate") {
            jQuery("#Undergraduate").val("0");    
        }
        jQuery(form).attr("action","http://www.mq.edu.au/findacourse2.php?Faculty=");
        
    } else {        
        //Production: jQuery(form).attr("action","http://mq-search.funnelback.com/search/search.cgi");
        //UAT        
        jQuery(form).attr("action","https://uat.funnelback.com/search/search.cgi");
    }
}

function printpage(url)
{
    if (url) {       
       print_window = window.open(url, 'print_window', 'menubar=0,location=0,width=800,height=600,toolbar=0,scrollbars=1,resizable=1');       
    } else {
       window.print();    
    }   
}

function hideshowRenameCourse() {
	
	jQuery("#COURSECREATEDINFO").hide();
	jQuery("#COURSEPUBLISHEDINFO").hide();
	
	var course = jQuery("#NAME").val();
	if (course.indexOf('(Copy)') > 0) {
		jAlert("A Course Copy can not be renamed.","Rename Failed");
		return;
	}
	if (jQuery("#title").is(':hidden')) {
		if (jQuery("#renamedCourse").val() != jQuery("#NAME").val()) {			
			jQuery("#RENAMEDCOURSEINFO").show();
			jQuery("#CourseName").css('color', '#9F141A');
			jQuery("#CourseName").html(jQuery("#renamedCourse").val());
			jQuery("#NAME").val(jQuery("#renamedCourse").val());
		}
		jQuery("#title").show();
		jQuery("#renameCourseName").hide();
	} else {
		jQuery("#title").hide();
		jQuery("#renameCourseName").show();
	}

}

function ValidateCourseName(BasePath) {

    var values = {};
    values.Edition = jQuery("#EDITION").val();
    values.Name = jQuery("#NAME").val();
    values.Type = jQuery("#TYPE").val();
    values.SubType = jQuery("#SUBTYPE").val();
    values.Code = jQuery("#CODE").val();
    
    jQuery.post(BasePath + "/admin/courses/validate", values,
            function(result){
                if (result) { 
                    jQuery("#VALIDATECOURSEINFO").html(result);                   
                    jQuery("#VALIDATECOURSEINFO").show();
                    jQuery("#submit").attr('disabled', true);                    
                } else {
                    jQuery("#VALIDATECOURSEINFO").hide();
                    jQuery("#submit").attr('disabled', false);
                }                                              
            }
    ); 
}

function GetParentCourses(BasePath, Type) {
	
	var values = {};
	
	values.Year = jQuery("#EDITION").val();	
	values.Type = Type;
    values.ID = jQuery("#COURSEFINDERID").val();
    values.Name = jQuery("#NAME").val();
    values.Code = jQuery("#CODE").val();
    values.Subtype = jQuery("#SUBTYPE").val();
	
    jQuery('#PARENTCOURSEINFO').html("<div style=\"text-align:center; float:center;\">Loading Parent Courses ... Please wait <img src=\"/images/wait.gif\" class=\"loading\" style=\"display:none;\"/></div>"); 
    jQuery('#PARENTCOURSEINFO').show();
    jQuery('#PARENTCOURSEINFO img.loading').show();
    
	jQuery.post(BasePath + "/admin/courses/getparent", values,
            function(result){
        		if (result) { 
            		jQuery("#PARENTCOURSEINFO").html(result);       			
					jQuery("#PARENTCOURSEINFO").show();										
                    jQuery('#parent-course-actions').show();
        		} else {
        			jQuery("#PARENTCOURSEINFO").hide();        			
        		}              	                            
            }
    );
   
}
/*
function GetFaculties(BasePath) {
    
    var values = {};
    
    values.Edition = jQuery("#EDITION").val();        
    values.ID = jQuery("#COURSEFINDERID").val();
    values.Name = jQuery("#NAME").val();
    values.Faculties = jQuery("#OFFEREDBYFACULTY").val();
        
    jQuery('#FACULTYINFO').html("<div style=\"text-align:center; float:center;\">Loading Faculties... Please wait <img src=\"/images/wait.gif\" class=\"loading\" style=\"display:none;\"/></div>"); 
    jQuery('#FACULTYINFO').show();
    jQuery('#FACULTYINFO img.loading').show();
    
    jQuery.post(BasePath + "/admin/courses/getfaculty", values,
            function(result){
                if (result) { 
                    jQuery("#FACULTYINFO").html(result);                   
                    jQuery("#FACULTYINFO").show();                                                            
                } else {
                    jQuery("#FACULTYINFO").hide();                    
                }                                              
            }
    );
   
}*/

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

function checkToggle(id, status) {    
    jQuery("#"+id+" input").each( function() {
        jQuery(this).attr("checked", status);
    });
}

function doCustomLoad() //added by mostafa
{
//jQuery("#programInfo").css("border","3px solid red");
jQuery("li#tab-01.selected").removeClass("selected");
jQuery("li#tab-02").addClass("selected");
jQuery("#tab-content-01").removeClass("selected");
jQuery("#tab-content-01").css("display","none");

jQuery("#tab-content-02").addClass("selected");
jQuery("#tab-content-02").css("display","block");


}

