/***************************************************************************/
/*     PROGRAMMER     :  SDYA                                              */
/*     SCRIPT NAME    :  js.js			                                   */
/*     CREATED ON     :  18/AUG/2008                                       */
/*     LAST MODIFIED  :  22/AUG/2008                                       */
/*                                                                         */
/*     Java Script Functions For the Front End                             */
/***************************************************************************/

    /*----------------------------------------------------------------
    Description   :- function to validate an email id
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function isBadEmail(strg) {
		email_array = strg.split('@');
		if (email_array.length != 2) return true;
		if (email_array[1].split(".").length < 2) return true;
		if (email_array[1].split(".")[1].length < 1) return true;
		if (strg.indexOf('@') < 1) return true;
		if (strg.indexOf(' ') != -1) return true;
		if (email_array[1].indexOf('.') < 1) return true;
		if (strg.length < 5) return true;
		return false;
	}

    /*----------------------------------------------------------------
    Description   :- function to validate yellow page manager
    Programmer    :- SDYA    
	Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function LTrim(str)
	{
		var whitespace = new String(" \t\n\r");
		var s = new String(str);
		if (whitespace.indexOf(s.charAt(0)) != -1) {
			// We have a string with leading blank(s)...
			
			var j=0, i = s.length;
			
			// Iterate from the far left of string until we
			// don't have any more whitespace...
			while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
			
			// Get the substring from the first non-whitespace
			// character to the end of the string...
			s = s.substring(j, i);
		}
		return s;
	}

    /*----------------------------------------------------------------
    Description   :- function to validate yellow page manager
    Programmer    :- SDYA    
	Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function RTrim(str)
	{
		// We don't want to trip JUST spaces, but also tabs,
		// line feeds, etc.  Add anything else you want to "trim" here in Whitespace
		var whitespace = new String(" \t\n\r");
		
		var s = new String(str);
		
		if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
			// We have a string with trailing blank(s)...
			var i = s.length - 1;       // Get length of string
			
			// Iterate from the far right of string until we
			// don't have any more whitespace...
			while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
			
			// Get the substring from the front of the string to
			// where the last non-whitespace character is...
			s = s.substring(0, i+1);
		}
		return s;
	}

    /*----------------------------------------------------------------
    Description   :- function to validate yellow page manager
    Programmer    :- SDYA    
	Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function Trim(str)
	{
		return RTrim(LTrim(str));
	}

    /*----------------------------------------------------------------
    Description   :- function to validate the signup form
    Programmer    :- SDYA
    Last Modified :- 21/AUG/2008
    -------------------------------------------------------------------*/
	function signup_validate()
	{
    	//add code
    	if(document.frm_signup.txt_code.value=='')
        {
        	alert("Please enter the add code");
            document.frm_signup.txt_code.focus();
            return false;
        }
	}
	
	
	function signup_continue_validate()
	{
    	//first name
    	if(document.frm_register.txt_firstname.value=='')
        {
        	alert("Please enter your first name");
            document.frm_register.txt_firstname.focus();
            return false;
        }

		//last name
    	if(document.frm_register.txt_lastname.value=='')
        {
        	alert("Please enter your Last name");
            document.frm_register.txt_lastname.focus();
            return false;
        }

		//address
    	if(document.frm_register.txt_address.value=='')
        {
        	alert("Please enter your address");
            document.frm_register.txt_address.focus();
            return false;
        }

		//city
    	if(document.frm_register.txt_city.value=='')
        {
        	alert("Please enter the city");
            document.frm_register.txt_city.focus();
            return false;
        }
		//state
    	if(document.frm_register.txt_state.value=='')
        {
        	alert("Please select your state");
            document.frm_register.txt_state.focus();
            return false;
        }
		//zip
		if(document.frm_register.txt_zip.value=='')
        {
        	alert("Please select your zipcode");
            document.frm_register.txt_zip.focus();
            return false;
        }
		//email id
    	if(document.frm_register.txt_emailid.value=='')
        {
        	alert("Please enter your emailid");
            document.frm_register.txt_emailid.focus();
            return false;
        }
		else
		{
			//validate the email
			if(isBadEmail(document.frm_register.txt_emailid.value))
			{
				alert("Invalid Email Id");
				document.frm_register.txt_emailid.focus();
				return false;
			}
		}

        //user name
    	if(document.frm_register.txt_uname.value=='')
        {
        	alert("Please enter your user name");
            document.frm_register.txt_uname.focus();
            return false;
        }

        //password
    	if(document.frm_register.txt_password.value=='')
        {
        	alert("Please enter your password");
            document.frm_register.txt_password.focus();
            return false;
        }

		//retype password
    	if(document.frm_register.txt_repassword.value=='')
        {
        	alert("Please re-type your password");
            document.frm_register.txt_repassword.focus();
            return false;
        }

        //confirm password
        if(document.frm_register.txt_password.value!=document.frm_register.txt_repassword.value)
        {
			alert("Please confirm your password");
            document.frm_register.txt_repassword.focus();
            return false;
        }
        return true;
    }
    /*----------------------------------------------------------------
    Description   :- function to validate the user login form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function login_validate()
	{
		//username
		if(document.frm_login.txt_login.value=="")
		{
			alert("Please enter the user name");
			document.frm_login.txt_login.focus();
			return false;
		}
		//password
		if(document.frm_login.txt_pwd.value=="")
		{
			alert("Please enter the password");
			document.frm_login.txt_pwd.focus();
			return false;
		}
	}

    /*----------------------------------------------------------------
    Description   :- function to validate the update password form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function update_pwd_validate()
	{
		//old password
		if(document.frm_password.txt_old.value=="")
		{
			alert("Please enter your existing password");
			document.frm_password.txt_old.focus();
			return false;
		}
		//new password
		if(document.frm_password.txt_new.value=="")
		{
			alert("Please enter your new password");
			document.frm_password.txt_new.focus();
			return false;
		}
		//retype password
		if(document.frm_password.txt_retype.value=="")
		{
			alert("Please enter retype the new password");
			document.frm_password.txt_retype.focus();
			return false;
		}
		//confirm password
		if(document.frm_password.txt_retype.value!=document.frm_password.txt_new.value)
		{
			alert("Please confirm your password");
			document.frm_password.txt_retype.focus();
			return false;
		}
		return true;
	}

    /*----------------------------------------------------------------
    Description   :- function to validate the update personal details form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function update_personal_validate()
	{
		//first name
		if(document.frm_personal.txt_fname.value=="")
		{
			alert("Please enter the first name");
			document.frm_personal.txt_fname.focus();
			return false;
		}
		//last name
		if(document.frm_personal.txt_lname.value=="")
		{
			alert("Please enter the last name");
			document.frm_personal.txt_lname.focus();
			return false;
		}
		//email id
		if(document.frm_personal.txt_emailid.value=="")
		{
			alert("Please enter your email id");
			document.frm_personal.txt_emailid.focus();
			return false;
		}
		//validate email id
		if(isBadEmail(document.frm_personal.txt_emailid.value))
		{
			alert("Please enter a valid email id");
			document.frm_personal.txt_emailid.focus();
			return false;
		}
		return true;
	}
	
   	/*----------------------------------------------------------------
    Description   :- function to validate the update user name form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function update_uname_validate()
	{
		//user name
		if(document.frm_uname.txt_uname.value=="")
		{
			alert("Please enter the user name");
			document.frm_uname.txt_uname.focus();
			return false;
		}
		return true;
	}

    /*----------------------------------------------------------------
    Description   :- function to validate the forgotpassword form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function forgotpassword_validate()
	{
		//user name
		if(document.form_forgot.txt_uname.value=="")
		{
			alert("Please enter your user name");
			document.form_forgot.txt_uname.focus();
			return false;
		}
		return true;
	}

    /*----------------------------------------------------------------
    Description   :- function to submit the form
    Programmer    :- SDYA
    Last Modified :- 18/AUG/2008
    -------------------------------------------------------------------*/
	function submit_me()
	{
		document.form.action	= "signup_confirm_validate.php?mode=bac";
		document.form.submit();
	}
	function submit_me1()
	{
		document.form.action	= "signup_confirm_validate.php";
		document.form.submit();
	}
	
    /*----------------------------------------------------------------
    Description   :- function to show the lesson details page
    Programmer    :- SDYA
    Last Modified :- 22/AUG/2008
    -------------------------------------------------------------------*/
	function show_lesson_details(lessonid)
	{
		var containerid	= "lesson_"+lessonid; 
		if(document.getElementById(containerid).style.display == "none" )
		{
			document.getElementById(containerid).style.display = "block";
			//ajaxpage(url,containerid);
		}
		else
			document.getElementById(containerid).style.display = "none";
	}
	function show_lesson_details_(lessonid)
	{
		url	= "lesson_details.php?lessonid="+lessonid;
		var containerid	= "lesson_"+lessonid; 
		if(document.getElementById(containerid).style.display == "none" )
		{
			document.getElementById(containerid).style.display = "block";
			ajaxpage(url,containerid);
		}
		else
			document.getElementById(containerid).style.display = "none";
	}
	
	/*----------------------------------------------------------------
    Description   :- function to open a new window

    Programmer    :- SDYA
    Last Modified :- 29/AUG/2008
    -------------------------------------------------------------------*/
   	function OpenWindow(url)
	{
       nw = open(url,'new','height=450,width=450,scrollbars=yes,resizable=1,top=230,left=230');
	   nw.focus();
	}
	
	/*----------------------------------------------------------------
    Description   :- function to agree the terms
    Programmer    :- SDYA
    Last Modified :- 25/SEP/2008
    -------------------------------------------------------------------*/
	function agree_me()
	{
		//var txt_code	= document.frm_signup_terms.txt_code.value;
		document.frm_signup_terms.action	= "signup_terms_validate.php";
		document.frm_signup_terms.submit();
	}
	
	/*----------------------------------------------------------------
    Description   :- function to dis agree the terms
    Programmer    :- SDYA
    Last Modified :- 25/SEP/2008
    -------------------------------------------------------------------*/
	function disagree_me()
	{
		window.location = "index.php";
	}

	function viewprev_lessons()
	{
		window.location = "index.php?act=prev_lesson&id="+document.getElementById('sel_prev').value;
	}
	
	function view_prevlessons()
	{
		//window.location = "index.php?act=lessons&id="+document.getElementById('sel_prev').value+"&dt=1";
		window.location = "index.php?act=lessons&id="+document.getElementById('sel_prev').value;
	}
	function view_search_prevlessons()
	{
		//window.location = "index.php?act=lessons&id="+document.getElementById('sel_prev').value+"&dt=1";
		window.location = "index.php?act=search_lessons&id="+document.getElementById('sel_prev').value+'&level='+document.getElementById('search_by_level').value+'&content_area='+document.getElementById('contentarea_dd').value+'&alpha='+document.getElementById('chk_order_by_lessons').checked+'&with_lesson='+document.getElementById('chk_with_lessons').checked;
	}
	
    /*----------------------------------------------------------------
    Description   :- function to validate the library user form
    Programmer    :- SDYA
    Last Modified :- 21/AUG/2008
    -------------------------------------------------------------------*/
	function libuser_validate()
	{
    	//name
    	if(document.frm_libuser.txt_name.value=='')
        {
        	alert("Please enter your name");
            document.frm_libuser.txt_name.focus();
            return false;
        }

		//school name
    	if(document.frm_libuser.txt_school.value=='')
        {
        	alert("Please enter your school name");
            document.frm_libuser.txt_school.focus();
            return false;
        }

		//school district
    	if(document.frm_libuser.txt_district.value=='')
        {
        	alert("Please enter your school district");
            document.frm_libuser.txt_district.focus();
            return false;
        }

		//email id
    	if(document.frm_libuser.txt_email.value=='')
        {
        	alert("Please enter your emailid");
            document.frm_libuser.txt_email.focus();
            return false;
        }
		else
		{
			//validate the email
			if(isBadEmail(document.frm_libuser.txt_email.value))
			{
				alert("Invalid Email Id");
				document.frm_libuser.txt_email.focus();
				return false;
			}
		}
        return true;
	}
	
	/*----------------------------------------------------------------
    Description   :- function to open a pop up
    Programmer    :- SDYA
    Last Modified :- 29/AUG/2008
    -------------------------------------------------------------------*/
   	function popitup(url)
	{
       nw = open(url,'new','height=650,width=900,scrollbars=yes,resizable=1,top=50,left=100');
	   nw.focus();
	}

