var result

function GoToSection(validation, nextSection, currentSection) {		
    if (validation.length == 0 || Page_ClientValidate(validation)) {
	    document.getElementById(currentSection).className="Hidden";
	    document.getElementById(nextSection).className="Visible";
	    document.getElementById("MainMenu").style.visibility="hidden";
	}
}

function CloseApplication(cover,currentSection) {	
   var answer = window.confirm("Select 'OK' to close your application");

   if (answer)
   {
	   document.getElementById(cover).className="HideCoverDiv";
	   document.getElementById(currentSection).className="Hidden";
	   document.getElementById("MainMenu").style.visibility="visible";
   }
}


function SubmitApplication(currentSection) {	
    document.getElementById(currentSection).className="Hidden";
    createProgressDialog();
    
    return false;
}

function createProgressDialog() {
   var waitDiv = document.createElement("DIV"); 
   waitDiv.id = "waitDiv";  
   waitDiv.className = "WaitBox";
   waitDiv.innerHTML = "<div>Submitting...</div><img src='/images/paws.gif' alt='Please Wait...' />";
    
   document.getElementsByTagName("BODY")[0].appendChild(waitDiv); 
}

function LimitTextInput(textArea,maxLength)
{   
    try
    {   
        if(textArea.value.length > (maxLength - 1))
        {
            //textArea.value = textArea.value.substring(0, maxLength);
            return false;   
        }
    }
    catch(e)
    {
        return false;   
    }   
}

function getWindowHeight() {
  var height = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    height = document.documentElement.clientHeight;
  } 
  return height;
}

function getWindowWidth() {
  var width = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    width = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    width = document.documentElement.clientWidth;
  } 
  return width;
}

function SizeImage() {
   document.getElementById('ImagePlace').style.height = "auto";  
   document.getElementById('ImagePlace').style.width = "auto";
   var height = document.getElementById('ImagePlace').height;
	var width = document.getElementById('ImagePlace').width;	
	var maxWidth = getWindowWidth() - 10;
	if(width > maxWidth) {
	   height = (maxWidth/width)*height;
	   width = maxWidth; 
	   document.getElementById('ImagePlace').style.height = height + "px";  
	   document.getElementById('ImagePlace').style.width = width + "px";
	}
	
	var maxHeight = getWindowHeight() - 10;
	if(height > maxHeight) {
	   width = (maxHeight/height)*width;
	   height = maxHeight;   
	   document.getElementById('ImagePlace').style.height = height + "px"; 
	   document.getElementById('ImagePlace').style.width = width + "px";	   
	}

	document.getElementById('photoDiv').style.width = width + "px";
	document.getElementById('photoDiv').style.height = height + "px";
	document.getElementById('photoDiv').style.marginTop = "-" + height/2 - 3 + "px";
	document.getElementById('photoDiv').style.marginLeft = "-" + width/2 - 3 + "px";	
	
	document.getElementById('photoDiv').style.visibility = "visible";
	document.getElementById('waitDiv').style.visibility = "hidden";
	
}

function ShowItem(item,category) {		
	document.getElementById(category).style.visibility = "hidden";
	document.getElementById(item).style.visibility = "visible";
}

function ShowCategory(category) {		
	createCoverDiv();
	document.getElementById(category).style.visibility = "visible";
}

function CloseStoreWindow(windowDiv) {		
	removeCoverDiv();
	document.getElementById(windowDiv).style.visibility = "hidden";
}



function ShowDonation() {		
	createCoverDiv();
	document.getElementById('donation').style.visibility = "visible";
}

function CloseDonation() {		
	removeCoverDiv();
	document.getElementById('donation').style.visibility = "hidden";
}

function ShowShelterChallenge() {		
	createCoverDiv();
	document.getElementById('shelterChallenge').style.visibility = "visible";
}

function CloseShelterChallenge() {		
	removeCoverDiv();
	document.getElementById('shelterChallenge').style.visibility = "hidden";
}

function ShowImage(image) {		
   createCoverDiv();
   createProgressDialog();
   createPhotoDiv();
   
   document.getElementById('ImagePlace').src = image; 
}

function ImageFailed() {		
	CloseImage();
	alert("That image could not be found.");
}

function CloseImage() {		
	removeCoverDiv();
	removeProgressDialog();
	removePhotoDiv();
}

function removeCoverDiv() {   
   if (document.getElementById("cDiv")) {
      document.getElementsByTagName("BODY")[0].removeChild(document.getElementById('cDiv'));
   }
}
  
function removeProgressDialog() {   
   if (document.getElementById("waitDiv")) {
      document.getElementsByTagName("BODY")[0].removeChild(document.getElementById('waitDiv'));
   }   
}

function removePhotoDiv() {
   if (document.getElementById("photoDiv")) {
      document.getElementsByTagName("BODY")[0].removeChild(document.getElementById('photoDiv'));
   }   
}
  
function createCoverDiv() {  
   var cDiv = document.createElement("DIV"); 
   cDiv.className = "CoverDiv";
   cDiv.id = "cDiv";  
   document.getElementsByTagName("BODY")[0].appendChild(cDiv);
}

function createPhotoDiv() {
   var photoDiv = document.createElement("DIV"); 
   photoDiv.className = "PhotoBox";
   photoDiv.id = "photoDiv"; 
   
   var imgPlace = document.createElement("IMG"); 
   imgPlace.className = "ImagePlace";
   imgPlace.id = "ImagePlace"; 
   photoDiv.appendChild(imgPlace);  
   document.getElementsByTagName("BODY")[0].appendChild(photoDiv); 

   document.getElementById('photoDiv').onclick = CloseImage;
   document.getElementById('ImagePlace').onload = SizeImage;
   document.getElementById('ImagePlace').onerror = ImageFailed;  
}


function QuestionValidation_ClientValidate(sender, args) {
    var radioButtons = document.getElementsByName(sender.question);
    for (var x = 0; x < radioButtons.length && args.IsValid; x ++) {
        if (radioButtons[x].checked && radioButtons[x].value == "Yes") {
            args.IsValid = document.getElementById(sender.explaination).value.length > 0;
        }
    }
    
    return args
}

         
function AtLeastOnePhone_ClientValidate(sender, args) {
    var homePhone = "";
    var cellPhone = "";
    var workPhone = "";
    
    if (sender.home != null)
        homePhone = document.getElementById(sender.home);
    if (sender.cell != null) 
        cellPhone = document.getElementById(sender.cell);
    if (sender.work != null)
        workPhone = document.getElementById(sender.work);
  
    args.IsValid = (homePhone.value.length > 0 || cellPhone.value.length > 0 || workPhone.value.length > 0);
  
    return args;
}

function AtLeastOneReason_ClientValidate(sender, args) {
    var own_enjoyment = document.getElementById(sender.own_enjoyment);
    var required_community_service = document.getElementById(sender.required_community_service);
    var require_service_learning = document.getElementById(sender.require_service_learning);
    var required_other = document.getElementById(sender.required_other);
  
    args.IsValid = (own_enjoyment.checked || required_community_service.checked || require_service_learning.checked || required_other.checked);
  
    return args;
}

function OtherReason_ClientValidate(sender, args) {
    var other_program = document.getElementById(sender.other_program);
    var required_other = document.getElementById(sender.required_other);
  
    args.IsValid = (!required_other.checked || other_program.value.length);
  
    return args;
}
         
function AtLeastOneContact_ClientValidate(sender, args) {              
   if(document.getElementById('Email').value.length == 0 &&
      document.getElementById('phone').value.length == 0) {
      args.IsValid = false;            
   }
   else {
      args.IsValid = true;            
   }          
   
   return args;
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber = true;
   var NumberOfDecimals = 0;
   
   for (i = 0; i < sText.length && IsNumber; i++) 
   { 
      if(sText.charAt(i) == '.')
         NumberOfDecimals++;
            
      IsNumber = (NumberOfDecimals < 2) && (ValidChars.indexOf(sText.charAt(i)) != -1);
   }
   
   return IsNumber;
}

