﻿// JScript File
//==========================================
// LinkedTO: Default.aspx in YorkieContact
//==========================================
//------------------------------------------
// Contains all javascript code for this
//  web page.
//------------------------------------------

// elements id, color, class, ternary operator

//------------------------------------------
// ChangeColor - changes color of elements
//  text.
//------------------------------------------
function ChangeColor(id, colorChange, color)
{
    var theElement = document.getElementById(id);
    theElement.style.color = (theElement.style.color == colorChange) ? color :  colorChange;

}   //ends function

//------------------------------------------
// ShowHide - change visibility of element.
//------------------------------------------
function ShowHide(id, ddl)
{
    var theElement = document.getElementById(id);
    var dropDownList = document.getElementById(ddl);
    theElement.style.visibility = (dropDownList.options[dropDownList.selectedIndex].value == 'Yes') ? 'visible' : 'hidden';
}   //ends function

//------------------------------------------
// SetBorder - change class for border on image.
//------------------------------------------
function SetBorder(id)
{
    var theElement = document.getElementById(id);
    theElement.className = (theElement.className == 'setImgBorder') ? '' : 'setImgBorder';

}   //ends function

//------------------------------------------
// Validate - validate page.
// use array to hold element id's that need validation
// check each type value property.
//------------------------------------------
var i= GetCookie();
//alert("i value is " + i)
function Validate()
{
    var theElements = new Array("txtbx_name","txtbx_email","txtbx_location");
    var theNames = new Array("your \"Name\"", "your \"Email Address\"", "\"Where your from\"");
    var theAsterisks = new Array("asterisk1", "asterisk2", "asterisk3");
    
    if(theElements != null)
    {
        for(; i < theElements.length;)
        {
            var theElement = document.getElementById(theElements[i]);
            var theSpanTag = document.getElementById(theAsterisks[i]);
            if(theElement.value == "" || theElement.value == null || theElement.length == 0)
            { 
                alert("Please fill in " + theNames[i] + ", thanks.");
                theSpanTag.style.display = (theSpanTag.style.display == "none") ? 'inline' : 'none';
                theElement.focus(); 
                
                // increment i before it's saved
                i++;
                //alert("save i as " + i);
                CreateCookie(i);
                
                //alert(document.cookie);
                
                return false;
                break;
            }
            else
            {
               //TODO: allow submit
               return true;
            }
        }
    }
    else
    {
        alert("There are no elements to validate, please fill in the array.");
    }
}   //ends function

// save i - cycles from 0 - 3 validating
// the 1st three fields, once it reaches
// 3 it resets
function CreateCookie(i)
{
	var expireDate = new Date();
	expireDate.setDate(expireDate.setDate(365));
	document.cookie = "validationCounter = " + i + "; expires=" + expireDate;
}

// get i
function GetCookie()
{
	if(document.cookie.length > 0)
	{
		var start = document.cookie.indexOf("validationCounter=");
		
		if(start != -1)
		{
			start = start + 18;
			var end = document.cookie.indexOf(";", start);
			
			if(end == -1)
				end = document.cookie.length;
			
			if(document.cookie.substring(start, end) >= 3)
			{
				CreateCookie(0);
				return 0;
			}
			return document.cookie.substring(start, end);
		}
	}
	return 0;
}