var req = createXMLHttpRequest();
var str = '';

function createXMLHttpRequest() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function overlay2() 
{
	var docHeight = document.height || document.body.offsetHeight;
	
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}

	el = document.getElementById("overlay");
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
	el2 = document.getElementById("lightbox");
	el2.style.visibility = (el2.style.visibility == "visible") ? "hidden" : "visible";
	if(el.style.visibility == "visible")
	{
		el.style.display="block";
		//document.getElementById("overlay").style.height=yWithScroll+"px";	
		document.getElementById("lightbox").style.top=document.documentElement.scrollTop + 20 + "px";
		
	}
	else
	{
		el.style.display="none";
		//document.body.style.overflow="auto";
		document.getElementById("overlay").style.height="100%";
		document.getElementById("outerContainer").style.width="350px";
		document.getElementById("outerContainer").style.height="150px";
	}
	
	if(el2.style.visibility == "visible")
	{
		el2.style.display="block";
	}
	else
	{
		el2.style.display="none";
	}
	
}

function tellFriend()
{
	var tmp='';
	
	tmp = '<table border="0" cellpadding="1" cellspacing="1" width="100%">';
	tmp += '<tr valign="top">';
	tmp += '<td align="left" style="padding-top:8px;padding-left:6px;border-bottom:1px solid #353535;">';
	tmp += '<font style="font-size:12px;"><b>Tell A Friend</b></font>';
	tmp += '</td>';
	tmp += '</tr>';
	tmp += '<tr valign="top">';
	tmp += '<td align="left" style="padding-top:8px;padding-left:8px;">';
	tmp += 'Please fill in the information below.';
	tmp += '</td>';
	tmp += '</tr>';
	tmp += '<tr valign="top">';
	tmp += '<td align="left" style="padding-top:8px;padding-left:8px;">';
		tmp += '<table border="0" cellpadding="2" cellspacing="0">';
		tmp += '<tr valign="top">';
		tmp += '<td align="left" valign="middle">';
		tmp += '<b>Your Name:</b>';
		tmp += '</td>';
		tmp += '<td align="left" style="padding-left:8px;">';
		tmp += '<input type="text" id="Name1" name="Name1" size="30">';
		tmp += '</td>';
		tmp += '</tr>';
		tmp += '<tr valign="top">';
		tmp += '<td align="left" valign="middle">';
		tmp += '<b>Your Email:</b>';
		tmp += '</td>';
		tmp += '<td align="left" style="padding-left:8px;">';
		tmp += '<input type="text" id="email1" name="email1" size="30">';
		tmp += '</td>';
		tmp += '</tr>';
		tmp += '<tr valign="top">';
		tmp += '<td align="left" valign="middle">';
		tmp += '<b>Friends Email:</b>';
		tmp += '</td>';
		tmp += '<td align="left" style="padding-left:8px;">';
		tmp += '<input type="text" id="email2" name="email2" size="30">';
		tmp += '</td>';
		tmp += '</tr>';
		tmp += '<tr valign="top">';
		tmp += '<td align="left">';
		tmp += '<b>Comments:</b>';
		tmp += '</td>';
		tmp += '<td align="left" style="padding-left:8px;">';
		tmp += '<textarea id="Comments1" name="Comments1" cols="25" rows="6"></textarea>';
		tmp += '</td>';
		tmp += '</tr>';
		tmp += '</table>';
	tmp += '</td>';
	tmp += '</tr>';
	tmp += '<tr valign="top">';
	tmp += '<td align="center" style="padding-top:8px;">';
	tmp += '<input type="button" name="cancelbtn" id="cancelbtn" value=" Cancel " onclick="overlay2();">&nbsp;';
	tmp += '<input type="button" name="sendbtn" id="sendbtn" value=" Send " onclick="tellFriendAction();">&nbsp;';
	tmp += '</td>';
	tmp += '</tr>';
	tmp += '</table>';	
	
	overlay2();
	
	document.getElementById("outerContainer").style.width="400px";
	document.getElementById("outerContainer").style.height="320px";
	document.getElementById("innerContainer").innerHTML=tmp;
}
function tellFriendAction()
{
	var name1=document.getElementById("Name1");
	var email1=document.getElementById("email1");
	var email2=document.getElementById("email2");
	var cmts=document.getElementById("Comments1");
	
	if(name1.value == '')
	{
		alert('Please enter your name!')
		name1.focus();
		return false;
	}
	else if(email1.value == '')
	{
		alert('Please enter your email address!')
		email1.focus();
		return false;
	}
	else if(email2.value == '')
	{
		alert('Please enter a recipient email address!')
		email2.focus();
		return false;
	}
	else
	{
		if (req)
		{
			// try to connect to the server
			try
			{
				// make asynchronous HTTP request to retrieve new message
				params = "n="+encodeURIComponent(name1.value)+"&e1="+encodeURIComponent(email1.value)+"&e2="+encodeURIComponent(email2.value)+"&c="+encodeURIComponent(cmts.value);
				req.open("POST", "inc/tellFriend.php", true);
				req.setRequestHeader("Content-Type", 
	                                 "application/x-www-form-urlencoded");
				req.onreadystatechange = handletellFriend;
				req.send(params);
				//alert(params);
			}
			catch(e)
			{
				alert(e.toString());
			}
		}
	}	
}

function handletellFriend() 
{
	// when readyState is 4, we are ready to read the server response
	if (req.readyState == 4) 
	{
		// continue only if HTTP status is "OK"
		if (req.status == 200) 
		{
			try
			{
				// do something with the response from the server
				gettellFriend();
			}
			catch(e)
			{
				// display error message
				alert(e.toString());
			}
		}	 
		else
		{
			// display error message
			alert(req.statusText);   
		}
	}
}

function gettellFriend()
{
	var response = req.responseText;

	overlay2();
	document.getElementById("innerContainer").innerHTML=response;
}

function checkFields()
{

	if(document.getElementById("fname").value == '')
	{
		alert('Please enter your first name.');
		document.getElementById("fname").focus();
		return false;
	}
	
	if(document.getElementById("lname").value == '')
	{
		alert('Please enter your last name.');
		document.getElementById("lname").focus();
		return false;
	}
	
	if(document.getElementById("addr1").value == '')
	{
		alert('Please enter your address.');
		document.getElementById("addr1").focus();
		return false;
	}
	
	if(document.getElementById("city").value == '')
	{
		alert('Please enter your city.');
		document.getElementById("city").focus();
		return false;
	}
	
	if(document.getElementById("state").value == '')
	{
		alert('Please select a state.');
		document.getElementById("state").focus();
		return false;
	}
	
	if(document.getElementById("zip").value == '')
	{
		alert('Please enter your Zip Code.');
		document.getElementById("zip").focus();
		return false;
	}

	
	document.getElementById("submitbtn").disabled=true;
	document.f1.submit();
}

function checkFields2()
{
	
	if(document.getElementById("cardtype").value == '')
	{
		alert('Please select your credit card type.');
		document.getElementById("cardtype").focus();
		return false;
	}
	
	if(document.getElementById("ccnum").value == '')
	{
		alert('Please enter your credit card number.');
		document.getElementById("ccnum").focus();
		return false;
	}
	
	if(document.getElementById("ccmonth").value == '')
	{
		alert('Please enter your credit card expiration month.');
		document.getElementById("ccmonth").focus();
		return false;
	}
	
	if(document.getElementById("ccyear").value == '')
	{
		alert('Please enter your credit card expiration year.');
		document.getElementById("ccyear").focus();
		return false;
	}
	
	if(document.getElementById("cvv").value == '')
	{
		alert('Please enter your credit card security code.');
		document.getElementById("cvv").focus();
		return false;
	}
	
	document.getElementById("submitbtn").disabled=true;
	document.f1.submit();
}

function checkOrder()
{
	if(document.getElementById("shipping").value == '')
	{
		alert('Please select a shipping method.');
		document.getElementById("shipping").focus();
		return false;
	}
}

function checkOther()
{
	if(document.getElementById("damount").value == 'X')
	{
		document.getElementById("oamount").disabled = false;
		document.getElementById("oamount").focus();
	}
	else
	{
		document.getElementById("oamount").disabled = true;
	}
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
        {
			IsNumber = false;
        }
    }
	return IsNumber;
}

function getDirections(toAddress)
{
	Over = document.getElementById("innerContainer");
  	tmp = '<table border="0" cellspacing="0" cellpadding="5" width="100%">';
  	tmp += '<tr><td align="left" style="border-bottom:1px solid #353535;">';
  	tmp += '<font style="font-family:verdana;font-size:12px;color#353535;"><b>Enter your starting point:</b>';
	tmp += '</td></tr>';
	tmp += '<tr><td align="left" style="padding-top:8px;">';
	tmp += '<font style="font-family:verdana;font-size:12px;color#353535;">Address:&nbsp;<input type="text" id="addr" name="addr" size="30">';
	tmp += '</font>';
	tmp += '</td></tr>';
	tmp += '<tr><td align="left" style="padding-top:6px;">';
	tmp += '<b>Please enter your city and state or zip code.</b>';
  	tmp += '</td></tr>';
	tmp += '<tr><td align="left" style="padding-top:1px;">';
	tmp += '<font style="font-family:verdana;font-size:12px;color#353535;">City:&nbsp;<input type="text" id="city" name="city" size="20">';
	tmp += '&nbsp;&nbsp;&nbsp;&nbsp;';
	tmp += 'State:&nbsp;<input type=text id="state" name="state" size="3" maxsize="2">';
	tmp += '&nbsp;&nbsp;Zip Code:&nbsp;<input type="text" id="zip" name="zip" size="12">';
	tmp += '</font>';
  	tmp += '</td></tr>';
	tmp += '<tr><td align="center" style="padding-top:8px;">';
	tmp += '<input type="button" name="cancel_btn" id="cancel_btn" value=" Cancel " onclick="overlay2();">';
	tmp += '&nbsp;<input type="button" name="go_btn" id="og_btn" value=" Submit " onclick="getDirections2(\''+toAddress+'\');">';
	tmp += '</td></tr>';
  	tmp += '</table>';
	document.getElementById("outerContainer").style.width="575px";
	document.getElementById("outerContainer").style.height="200px";
  	Over.innerHTML = tmp;
	overlay2();
}

function getDirections2(toAddress)
{
	var fromAddress;
			
	fromAddress=document.getElementById("addr").value+","+document.getElementById("city").value+","+document.getElementById("state").value+" "+document.getElementById("zip").value;
	
	window.open('directions.php?t='+toAddress+'&f='+fromAddress, 'Directions', 'location=0,status=0,scrollbars=1,menubar=0,resizable=1,width=675,height=600');
	overlay2();
}

