﻿
function set_cookie(name, value)
{
    if(name != '')
    {
		expires = new Date( 2009, 01, 01 );
		new_cookie = name + '=' + value + "; path=/;";
		document.cookie = new_cookie;
    }
}

function get_cookie(name)
{
    if(name == '')
        return('');

    name_index = document.cookie.indexOf(name + '=');

    if(name_index == -1)
        return('');

    cookie_value =  document.cookie.substr(name_index + name.length + 1, document.cookie.length);

    end_of_cookie = cookie_value.indexOf(';');
    if(end_of_cookie != -1)
        cookie_value = cookie_value.substr(0, end_of_cookie);

    space = cookie_value.indexOf('+');
    while(space != -1)
    { 
        cookie_value = cookie_value.substr(0, space) + ' ' + 
        cookie_value.substr(space + 1, cookie_value.length);
        			 
        space = cookie_value.indexOf('+');
    }

    return(cookie_value);
}

function clear_cookie(name)
{
    expires = new Date();
    expires.setYear(expires.getYear() - 1);
    document.cookie = name + '=null' + '; expires=' + expires + "; path=/;";
}
         
function clear_cookies()
{
    Cookies = document.cookie;
    Cookie = Cookies;
    expires = new Date();
    expires.setYear(expires.getYear() - 1);

    while(Cookie.length > 0)
    {
        Cookie = Cookies.substr(0, Cookies.indexOf(';'));
        Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);
        if(Cookie != '')
            document.cookie = Cookie + '; expires=' + expires + "; path=/;";
        else
            document.cookie = Cookies + '; expires=' + expires + "; path=/;"; 
    }
}
         
function SignIn(form, rootpath)
{
    clear_cookies();
    clear_cookie("user");
    clear_cookie("pass");
	set_cookie( "user", form.user.value );
    set_cookie( "pass", form.pass.value );
	window.location=rootpath;
}

function SignOut( rootpath )
{
    clear_cookies();
    window.location = "/Piglet/?act=signout";
}

function ResetPassword(form,rootpath)
{
    clear_cookies();
    set_cookie( "user", form.user.value );
	window.location=rootpath;
}

function CreateAccount(form, rootpath)
{    
    if ( EmailAddressIsValid( form.email1.value ) == false )
    {
        alert( "Email address is invalid" );
        form.email1.focus();
        form.email1.select();
        return;
    }
    
    if ( form.email1.value != form.email2.value )
    {
        alert( "Email addresses must match" );
        form.email2.focus();
        form.email2.select();
        return;
    }
    
    if ( encodeURIComponent(form.name.value) == '' )
    {
        alert( "Name should not be left blank" );
        return;
    }
    set_cookie( "user", encodeURIComponent(form.email1.value) );
    set_cookie( "name", encodeURIComponent(form.name.value) );
    window.location = rootpath + "/?act=finish_creating";
}

function SendFeedback(form)
{
	set_cookie( "comment_type", form.type.value );
	set_cookie( "comment_feedback", form.feedback.value );
	set_cookie( "comment_email", form.my_email.value );
	window.location = "/Piglet/?act=feedback_thanks";
}

function UpdateKid(form, rootpath)
{
    set_cookie( "kidid", encodeURIComponent(form.kid_id.value) );
    set_cookie( "kidname", encodeURIComponent(form.kid_name.value) );
    set_cookie( "allowance", encodeURIComponent(form.kid_allowance_amount.value) );
    set_cookie( "irate", encodeURIComponent(form.kid_weekly_interest.value) );
    set_cookie( "kidemail", encodeURIComponent(form.kid_email.value) );
    if ( form.kid_id.value == '-1' )
        window.location = rootpath + "/?act=addkid";
    else
        window.location = rootpath + "/?act=updatekid";
}

function DeleteKid(kidid, rootpath)
{
	if ( confirm( "Are you sure you want to delete this child?") == false )
		return;
    set_cookie( "kidid", kidid );
	window.location = rootpath + "/?act=deletekid";
}

function UpdateParent(form, rootpath)
{
    if ( form.parent_password1.value != '' )
    {
        if ( form.parent_password1.value != form.parent_password2.value )
        {
            alert( "Passwords must match" );
            return;
        }
        if ( form.parent_email.value != 'test@piggybankinc.com' )
        {
            set_cookie( "oldpwd", get_cookie("pass" ) );
            set_cookie( "pass", form.parent_password1.value );
        }
    }
    set_cookie( "new_name", encodeURIComponent(form.parent_name.value) );
    window.location = rootpath + "/?act=update_settings";
}

function EmailAddressIsValid(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(address);
}

function TransactionFormInputNeedsFixing(form)
{
    if ( ( form.transact_amount.value == '' ) || ( form.transact_description.value == '' ) )
    {
        alert( "Please enter amount and description" );
        return true;
    }

   if (isNaN(form.transact_amount.value) == true)
   {
       alert("Please enter a NUMERIC value for the amount");
       form.transact_amount.focus();
       form.transact_amount.select();
       return true;
   }
   
    return false;
}

function MakeDeposit(form, rootpath)
{
    if ( TransactionFormInputNeedsFixing( form ) )
        return;
    set_cookie( "amt", encodeURIComponent(form.transact_amount.value) );
    set_cookie( "dsc", encodeURIComponent(form.transact_description.value) );
    set_cookie( "kid_id", encodeURIComponent(form.kid_id.value) );
    window.location = rootpath + "/?act=complete_transaction&goto=" + window.location;
}

function MakeWithdrawal(form, rootpath)
{
    if ( TransactionFormInputNeedsFixing( form ) )
        return;
    set_cookie( "amt", "-" + encodeURIComponent(form.transact_amount.value) );
    set_cookie( "dsc", encodeURIComponent(form.transact_description.value) );
    set_cookie( "kid_id", encodeURIComponent(form.kid_id.value) );
    window.location = rootpath + "/?act=complete_transaction&goto=" + window.location;
}

function togglePopUp(szDivID, isVisible)
{
	var obj = document.layers ? document.layers[szDivID] :
		document.getElementById ?  document.getElementById(szDivID).style : document.all[szDivID].style;
	obj.visibility = document.layers ? (isVisible ? "show" : "hide") : (isVisible ? "visible" : "hidden");
	document.getElementById(szDivID).style.display= isVisible ? 'block' : 'none';
	document.getElementById('fade').style.display= isVisible ? 'block' : 'none';	
}

