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 checkGift1()
{
	var tmp=0;
	var deliv;
	
	for(i=0;i<document.getElementsByName('format').length;i++)
	{
		if(document.getElementsByName('format')[i].checked)
		{
			deliv=document.getElementsByName('format')[i].value;
			tmp++;
		}
	}
	
	if(tmp == 0)
	{
		alert('Please select how you want to send the gift certificate!');
		return false;
	}
	
	if(deliv == 'email')
	{
		if(document.getElementById("demail").value == '')
		{
			alert('Please enter a recipient\'s email address.');
			document.getElementById("demail").focus();
			return false;
		}
	}
	
	if(document.getElementById("amount").value == '')
	{
		alert('Please enter an amount!');
		document.getElementById("amount").focus();
		return false;
	}
	
	else if(IsNumeric(document.getElementById("amount").value) == false)
	{
		alert('Your donation amount contains an invalid character.\nWhole numbers only!');
		document.getElementById("amount").value = '';
		document.getElementById("amount").focus();
		return false;
	}
	
	else if(document.getElementById("amount").value < 25)
	{
		alert('Please enter an amount greater than $25');
		document.getElementById("amount").focus();
		return false;
	}
	
	else if(document.getElementById("amount").value > 1000)
	{
		alert('Please enter an amount less than $1000');
		document.getElementById("amount").focus();
		return false;
	}
	
	if(document.getElementById("fname").value == '')
	{
		alert('Please enter the recipient\'s first name!');
		document.getElementById("fname").focus();
		return false;
	}
	
	if(document.getElementById("lname").value == '')
	{
		alert('Please enter the recipient\'s last name!');
		document.getElementById("lname").focus();
		return false;
	}
	
	if(document.getElementById("to").value == '')
	{
		alert('Please enter a recipient\'s name!');
		document.getElementById("to").focus();
		return false;
	}
	
	if(document.getElementById("from").value == '')
	{
		alert('Please enter a sender\'s name!');
		document.getElementById("from").focus();
		return false;
	}
}

function checkFields()
{	
	if(document.getElementById("bfname").value == '')
	{
		alert('Please enter your first name.');
		document.getElementById("bfname").focus();
		return false;
	}
	
	if(document.getElementById("blname").value == '')
	{
		alert('Please enter your last name.');
		document.getElementById("blname").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;
	}
	
	if(document.getElementById("email1").value == '')
	{
		alert('Please enter your email address.');
		document.getElementById("email1").focus();
		return false;
	}
	
	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 checkOther()
{
	if(document.getElementById("damount").value == 'X')
	{
		document.getElementById("oamount").disabled = false;
		document.getElementById("oamount").focus();
	}
	else
	{
		document.getElementById("oamount").disabled = true;
	}
}

function checkDelivType(v)
{
	if(v== 'email')
	{
		document.getElementById("delivemail").style.display="block";
	}
	else
	{
		document.getElementById("delivemail").style.display="none";
	}
}

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 checkBox(name, id) {
if (document.form1.name.checked)
{
document.form1.name.value=0;
}
else
{
document.form1.name.value=1;
window.location='function.php?_action=redeem&id='+id;
}
}
function reprintGo(id) {

window.open('../getCertificate.php?o='+id,'GiftWin','width=720,height=470,directories=no,location=no,menubar=no,resizable=no,scrollbars=1,status=no, toolbar=no');
}

function checkG() {
if (document.form1.Gsearch.value=='')
{
alert('Please enter a gift certificat number');
document.form1.Gsearch.focus();
return false;
}
}