﻿
// JScript File
function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250');");
}

function winOpen(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "');");
}

function setFocus(controlName) {
    var control = document.getElementById(controlName);
    if (control != null) {
        control.focus();
    }
}

function clearEmailText() {
    var control = document.getElementById('txtEmail'); 
    if( control == null ) {
        control = document.getElementById('EmailListControl_txtEmail'); 
    }
    if( control == null ) {
        control = document.getElementById('ctl04_txtEmail'); 
    }
    if( control != null ) {
        if( control.value == 'Enter Your e-Mail' )
            control.value = '';
    }
} 

function ResourceSearch() {

    var strValue;
    var subjectControl = document.getElementById('ddSubject');
    
    if( subjectControl == null ) {
        subjectControl = document.getElementById('ctl00_ddSubject');
    }
    
    if( subjectControl != null ) {
        strValue = subjectControl.options[subjectControl.selectedIndex].text;            
        if( strValue == '--' ) {                          
            alert('Please select a Subject.');
            return false;        
        }
    }
    
    return true;    
}

function WorksheetSearch() {

    var qs = '';
    var strValue;
   
    var countryControl = document.getElementById('ddCountries');
    if( countryControl != null ) {
        strValue = countryControl.value;
        if( strValue != 'All' ) {                                   
            qs = qs + "country=" + strValue + "&";        
        }
    }
    
    var subjectControl = document.getElementById('ddSubject');
    if( subjectControl != null ) {
        strValue = subjectControl.value;            
        if( strValue != 'All' ) {            
            qs = qs + "subject=" + strValue + "&";
        }
    }
    
    var gradeControl = document.getElementById('ddGradeLevel');
    if( gradeControl != null ) {
        strValue = gradeControl.value;            
        if( strValue != 'All' ) {            
            qs = qs + "gradeLevel=" + strValue;
        }
    }
        
    window.location = "/worksheets/search-results.aspx?" + qs;    
}  

function HandoutSearch() {

    var qs = '';
    var strValue;
   
    var countryControl = document.getElementById('ddCountries');
    if( countryControl != null ) {
        strValue = countryControl.value;
        if( strValue != 'All' ) {                                   
            qs = qs + "country=" + strValue + "&";        
        }
    }
    
    var subjectControl = document.getElementById('ddSubject');
    if( subjectControl != null ) {
        strValue = subjectControl.value;            
        if( strValue != 'All' ) {            
            qs = qs + "subject=" + strValue + "&";
        }
    }
    
    var gradeControl = document.getElementById('ddGradeLevel');
    if( gradeControl != null ) {
        strValue = gradeControl.value;            
        if( strValue != 'All' ) {            
            qs = qs + "gradeLevel=" + strValue;
        }
    }
        
    window.location = "/handouts/search-results.aspx?" + qs;    
}

function searchEnter(e)
{
    var characterCode
    if(e && e.which)
    {
        e=e
        characterCode = e.which
    }
    else
    {
        e = event
        characterCode = e.keyCode
    }

    if(characterCode == 13)
    {
        Search_OnClick()
        return false
    }
    else 
    {
        return true
    }
 }
 
 function productEnter(e)
{
    var characterCode
    if(e && e.which)
    {
        e=e
        characterCode = e.which
    }
    else
    {
        e = event
        characterCode = e.keyCode
    }

    if(characterCode == 13)
    {
        ProductSearch_OnClick()
        return false
    }
    else
    {
        var strValue
        var searchTextBox = document.getElementById('productSearch')
        
        if( searchTextBox != null ) 
        {
            strValue = trim(searchTextBox.value)   
            if( strValue == 'search') 
            {            
                searchTextBox.value=""
                return false
            }
        }
        return true
    }
 }

function Email_OnClick() {
    var control = document.getElementById('txtEmail');
    if( control != null ) {
        var strValue = trim(control.value);            
        if( strValue != 'Enter Your e-Mail' && strValue.length > 0 ) {            
            window.location = "index.aspx";
        }
    }
}  

// Removes leading whitespaces
function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {	
	return LTrim(RTrim(value));	
}

// This function is called by the rating control
function UpdateRatingControl( starNum ) {

    var star1 = document.getElementById('star1');
    var star2 = document.getElementById('star2');
    var star3 = document.getElementById('star3');
    var star4 = document.getElementById('star4');
    var star5 = document.getElementById('star5');
    var hidStarValue = document.getElementById('rateClickValue');
    if( hidStarValue == null ) {
        hidStarValue = document.getElementById('ctl00$rateClickValue');
    }
    
    if( star1 != null && star2 != null && star3 != null && star4 != null && star5 != null && hidStarValue != null ) {        

        if( starNum == 1 ) {
            star1.src = "/images/star_o_on.gif";
            star2.src = "/images/star_o_off.gif";
            star3.src = "/images/star_o_off.gif";
            star4.src = "/images/star_o_off.gif";
            star5.src = "/images/star_o_off.gif";
            hidStarValue.value = 1;
        }
        
        if( starNum == 2 ) {
            star1.src = "/images/star_o_on.gif";
            star2.src = "/images/star_o_on.gif";
            star3.src = "/images/star_o_off.gif";
            star4.src = "/images/star_o_off.gif";
            star5.src = "/images/star_o_off.gif";
            hidStarValue.value = 2;
        }
        
        if( starNum == 3 ) {
            star1.src = "/images/star_o_on.gif";
            star2.src = "/images/star_o_on.gif";
            star3.src = "/images/star_o_on.gif";
            star4.src = "/images/star_o_off.gif";
            star5.src = "/images/star_o_off.gif";
            hidStarValue.value = 3;
        }
        
        if( starNum == 4 ) {
            star1.src = "/images/star_o_on.gif";
            star2.src = "/images/star_o_on.gif";
            star3.src = "/images/star_o_on.gif";
            star4.src = "/images/star_o_on.gif";
            star5.src = "/images/star_o_off.gif";
            hidStarValue.value = 4;
        }
        
        if( starNum == 5 ) {
            star1.src = "/images/star_o_on.gif";
            star2.src = "/images/star_o_on.gif";
            star3.src = "/images/star_o_on.gif";
            star4.src = "/images/star_o_on.gif";
            star5.src = "/images/star_o_on.gif";
            hidStarValue.value = 5;
        }
    }
}

function StarOn(num) {

//    var star1 = document.getElementById('star1');
//    var star2 = document.getElementById('star2');
//    var star3 = document.getElementById('star3');
//    var star4 = document.getElementById('star4');
//    var star5 = document.getElementById('star5');

//    if( num == '1' ) {
//       star1.src = "/images/star_b_on.gif";
//    }
//    
//    if( num == '2' ) {
//        star1.src = "/images/star_b_on.gif";
//        star2.src = "/images/star_b_on.gif";
//    }
//    
//    if( num == '3' ) {
//        star1.src = "/images/star_b_on.gif";
//        star2.src = "/images/star_b_on.gif";
//        star3.src = "/images/star_b_on.gif";
//    }
//    
//    if( num == '4' ) {
//        star1.src = "/images/star_b_on.gif";
//        star2.src = "/images/star_b_on.gif";
//        star3.src = "/images/star_b_on.gif";
//        star4.src = "/images/star_b_on.gif";
//    }
//    
//    if( num == '5' ) {
//        star1.src = "/images/star_b_on.gif";
//        star2.src = "/images/star_b_on.gif";
//        star3.src = "/images/star_b_on.gif";
//        star4.src = "/images/star_b_on.gif";
//        star5.src = "/images/star_b_on.gif";
//    }
}

function StarOff(control, num) {
    //control.src = "/images/star_b_off.gif";        
}

function emailcheck(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 address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid e-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid e-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid e-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid e-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid e-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid e-mail address")
		    return false
		 }

 		 return true					
	}

function HtmlToEntities(obj)
{	
    var ratingValue = document.getElementById('rateClickValue');
    if( ratingValue == null ) {
        ratingValue = document.getElementById('ctl00_rateClickValue');
    }
    if( ratingValue.value == '' ) {
      alert('Please rate the item before submitting.');
      return false;
    }
    var comment = document.getElementById('userComment');
    if( comment == null ) {
        comment = document.getElementById('ctl00_userComment');
    }
    if (trim(comment.value) == '')
    {
      alert('Please comment on the item before submitting.');
      return false;
    }
    var submit = document.getElementById('btnSubmit');
    if( submit == null ) {
        submit = document.getElementById('ctl00_btnSubmit');
    }
    DisableSubmit(submit);
    var re=/[<>"'&]/g;
    comment.value = comment.value.replace(re, function(m){return ReplaceChar(m)});
    //obj.form.submit();    
}

function ReplaceChar(match)
{
    if (match == "<")
      return "&lt;";
    else if (match == ">")
      return "&gt;";
    else if (match == "\"")
      return "&quot;";
    else if (match == "'")
      return "&#039;";
    else if (match == "&")
      return "&amp;";
}

function DisableSubmit(button)
{
    if (button.value != '')
    {
      //button.disabled = true;
      button.value = 'Submitting...';
    }
}

function CreateAlert()
{
    var countryControl = document.getElementById('ddCountries');
    var subjectControl = document.getElementById('ddSubject');
    var gradeControl = document.getElementById('ddGradeLevel');
    var typeControl = document.getElementById('ddType');
    
    if( countryControl != null && subjectControl != null && gradeControl != null && typeControl != null)
    {
        if( countryControl.value == 'All' && subjectControl.value == 'All' && gradeControl.value == 'All' && typeControl.value == 'All' ) 
        {
            alert('Warning: With the settings you have choosen you may receive a lot of emails.');
            return;
        }
    }
}

function ExpandCollapse(divID)
{
    var divElement = document.getElementById(divID);
    
    if( divElement != null )
    {
        if(divElement.style.display == "none")
        {            
            // div is hidden, so let's show it
            divElement.style.display = "";
        }
        else
        {
            // div is not hidden, so let's hide it
            divElement.style.display = "none";
        }    
    }
}

function SearchSubmit()
{
    var subject = document.getElementById('ddSubjects').options[document.getElementById('ddSubjects').selectedIndex].value;
    var grade = document.getElementById('ddGrades').options[document.getElementById('ddGrades').selectedIndex].value;
    var level = document.getElementById('ddLevels').options[document.getElementById('ddLevels').selectedIndex].value;
    var divFound = false;
    
//    // if no comments for the selected search criteria exists then alert user
//    if( document.getElementById(subject + grade + level) == null )
//    {
//        alert("No report card comments were found for grade " + grade + " " + subject + " level " + level);
//        return;
//    }
    
    var parentDiv = document.getElementById("divGeneralComments");
   
    if( parentDiv != null )
    {
        var childDivs = parentDiv.getElementsByTagName("div");
        for (var i = 0; i < childDivs.length; i++)
        { 
            // if all inputs are empty then show all
            if( subject == "" && grade == "" && level == "" ) 
            {
                //childDivs[i].style.visibility = "visible";
                childDivs[i].style.display = "";
                divFound = true;
            }
            else
            {                
                if( childDivs[i].id.indexOf(subject) != -1 && childDivs[i].id.indexOf(grade) != -1 && childDivs[i].id.indexOf(level) != -1 )  
                {                    
                    //childDivs[i].style.visibility = "visible";
                    childDivs[i].style.display = "";
                    divFound = true;
                }
                else 
                {
                    //childDivs[i].style.visibility = "hidden";
                    childDivs[i].style.display = "none";
                }
            }
        }
        
        if( divFound == false )
        {
            alert('Sorry but no comments were found for your search selection.');
        }
    }
}
