////////////////////////////////////////////////////////////////////////////////////////////////////
// Project: Dr. Wacky                                                                             //
// File: wacky.js                                                                                 //
// Author: Cody Garvin                                                                            //
// Date: 09/02/07                                                                                 //
// Description:                                                                                   //
// -- Main javascript library for dr. wacky's                                                     //
//                                                                                                //
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Misc functions
function countCheckboxes(checkBoxName) 
{
	var obj;
    var checks = 0;

    for (var count = 0; count < document.saveform.elements.length; count++) 
    {
    	obj = document.saveform.elements[count];
      	
      	if (obj.type == "checkbox" && obj.checked) 
      	{
			if (obj.name == checkBoxName) 
			{
				checks++;
			}
      	}
    }
    
    return checks;
} 

////////////////////////////////////////////////////////////////////////////////
// Selects all the messages
function checkAllItems()
{	
	if(document.saveForm.master_checkbox.checked)
	{
		var count = 0;
		while(count < 100)
		{
			// if a box is check add it to checked boxes
			var tempCheckBox = document.getElementById('product_'+count)
			if(tempCheckBox != null)
			{
				tempCheckBox.checked = true;
			}
			else
				break;
			count++;
		}
	}
	else
	{
		var count = 0;
		while(count < 100)
		{
			//if a box is check add it to checked boxes
			var tempCheckBox = document.getElementById('product_'+count)
			if(tempCheckBox != null)
			{
				tempCheckBox.checked = false;
			}
			else
				break;
			count++;
		}	
	}
}

////////////////////////////////////////////////////////////////////////////////
// Delete confirmations


////////////////////////////////////////////////////////////////////////////////
// Product view functions - Live

// function make the call to ajax / dojo to call tradeInCallback
function newsletterSignup(extraArgs) 
{
	var listURL = 'newslettersignup.php?'
  	+ 'email='+document.getElementById('newsletteremail').value;
  
  	if (extraArgs != undefined) 
  		listURL += "&"+extraArgs;
  
  	dojo.io.bind({ url: listURL,
                 handler: newsletterSignupCallback
                 });
}

// function to set the incoming html
function newsletterSignupCallback(type, data, evt) 
{
  	if(type == 'error') 
  	{
  		alert('Error when retrieving data from the server!'+data+evt);
	}
  	else
  	{
  		var newsletterDiv = document.getElementById('newsletterlayout');
  		newsletterDiv.innerHTML = '<div>'+data+'</div>';
  	}
}


////////////////////////////////////////////////////////////////////////////////
// Verifies if a string is a real number
function isDecimalNumber(string, extraChars)
{
	var digits = "0123456789.";
	if(extraChars != "")
		digits += extraChars;
	
	var count = 0;
	while(count < string.length)
	{
		var tempChar = string.charAt(count);
		// Check for hex numbers and fail on them
		if((tempChar == "A") || (tempChar == "a"))
			return false;
			
		if((digits.indexOf(tempChar) == -1) || (digits.indexOf(tempChar) > 13))
		{
			return false;
		}
		count++;
	}
	return true;
}


////////////////////////////////////////////////////////////////////////////////
// Strips letters out of a number
function stripPunctuation(string)
{
	var marks = ".,";
	
	for (i = 0; i < marks.length; i++) 
	{
		mark = marks.charAt(i);
		
		while (string.indexOf(mark) != -1) 
		{
			point = string.indexOf(mark);
			first_part = string.substring(0, point);
			second_part = string.substring(point + 1, string.length);
			string = first_part + second_part;
		}
	}
	return string;
}


////////////////////////////////////////////////////////////////////////////////
// Verify a number is a valid
function verifyDollarAmount(string)
{
	if(isDecimalNumber(string))
	{
		if(string.indexOf(".") != -1)
		{
			var numSides = split(".");
			if(numSides[1].length > 2)
			{
				alert("Sorry, you are only allowed 2 numbers after a decimal please.");
				return false;
			}
			else
				return true;
		}
		else
			return true;
	}
	else
		return false;
}


////////////////////////////////////////////////////////////////////////////////
// Used to expand a message box for live error checking
function showError(msg)
{
	var errorBox = document.getElementById('messageBox');
	var errorMsg = document.getElementById('errorMsg');
	
	errorBox.style.display = "";
	errorMsg.innerHTML = msg;
}

////////////////////////////////////////////////////////////////////////////////
// Functions used for mouseOver popups
var agt   = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function hide_mine(elmnt) 
{
	if(!is_ie) 
		return;
	var a = elmnt.getElementsByTagName("div");
	var div = a[0];
	elmnt.style.zIndex = 1;
	div.style.display = "none";
}

function show_mine(elmnt) 
{
	if(!is_ie) 
		return;
	var a = elmnt.getElementsByTagName("div");
	var div = a[0];
	elmnt.style.zIndex = 100;
	div.style.display = "block";
}

