// JavaScript Document

var whichGC = new Array();
var whichGCEmail = new Array();

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function downloadGiftCert(orderID, product, from, giftCertCounter, numGC){
	document.getElementById('icontainer' + giftCertCounter).innerHTML = '<iframe src="/download.php?orderID=' + orderID + '-' + numGC + '&product=' + product + '&from=' + from + '" width="1" height="1" style="visibility:hidden"></iframe>';

	return false; }

//function downloadGiftCert(orderID, product, from, giftCertCounter, qty, currentGiftCert){
//	document.getElementById('icontainer' + giftCertCounter).innerHTML = '<iframe src="/download.php?orderID=' + orderID + '-' + currentGiftCert + '&product=' + product + '&from=' + from + '" width="1" height="1" style="visibility:hidden"></iframe>';
//
//	return false; }
	
function emailQuote(orderID, product, from, giftCertCounter, numGiftCerts, currentGiftCert){ //alert(currentGiftCert);
	var hasError = false;
	var name = document.getElementById('name' + giftCertCounter).value;
	var email = document.getElementById('email' + giftCertCounter).value;
	
	if(name == ''){
		hasError = true;
		document.getElementById('nameField' + giftCertCounter).style.color = 'red';
		document.getElementById('nameField' + giftCertCounter).style.fontStyle = 'italic';
	}else{
		document.getElementById('nameField' + giftCertCounter).style.color = 'black';
		document.getElementById('nameField' + giftCertCounter).style.fontStyle = 'normal';
	}
	if(email == ''){
		hasError = true;
		document.getElementById('emailField' + giftCertCounter).style.color = 'red';
		document.getElementById('emailField' + giftCertCounter).style.fontStyle = 'italic';
	}else{
		document.getElementById('emailField' + giftCertCounter).style.color = 'black';
		document.getElementById('emailField' + giftCertCounter).style.fontStyle = 'normal';
	}
	
	if(!hasError){
		document.getElementById('emailQuoteDiv' + giftCertCounter).innerHTML = '<p style="text-align:center"><br><br>Sending Email...<br><img src="/giftCert_framework/images/loading.gif" alt="sending email" width="220" height="19"></p>';
		
		xmlhttp.open("GET", "/ajaxHandler.php?action=emailGiftCert&orderID=" + orderID + '-' + currentGiftCert + '&product=' + product + '&from=' + from + '&email=' + email + '&name=' + name );
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				response = xmlhttp.responseText;
				//alert(response);
				document.getElementById('emailQuoteDiv' + giftCertCounter).innerHTML = '<p style="text-align:center"><br><br>Your Gift Certificate has been sent.</p>';
				
				if(currentGiftCert == numGiftCerts){
					setTimeout("tb_remove()",2500);
				}else{
					currentGiftCert++;
					document.getElementById('emailQuoteDiv' + giftCertCounter).innerHTML = '<h4>Email Gift Certificate #' + currentGiftCert + '</h4><div><span id="nameField' + giftCertCounter + '">Name:</span> <input name="name" id="name' + giftCertCounter + '" style="width: 200px;" type="text"><br>&nbsp;</div><div><span id="emailField' + giftCertCounter + '">Email:</span> <input name="email" id="email' + giftCertCounter + '" style="width: 200px;" type="text"></div><div><br><input name="button" value="  Email Gift Certificate  " onclick="emailQuote(' + orderID + ',\'' + product + '\',\'' + from + '\', ' + giftCertCounter + ', ' + numGiftCerts + ',' + currentGiftCert + ')" type="button"></div>';
				}
			}
		}
		xmlhttp.send(null);
	}
}

function errorCheckForm(){
	var passedCheck = true;
	var firstName = document.getElementById('firstName').value;
	var lastName = document.getElementById('lastName').value;
	var phone = document.getElementById('phone').value;
	var email = document.getElementById('email').value;
	
	if(firstName == '' || firstName == 'First Name'){
		document.getElementById('firstName').style.color = 'red';
		passedCheck = false;
	}
	if(lastName == '' || lastName == 'Last Name'){
		document.getElementById('lastName').style.color = 'red';
		passedCheck = false;
	}
	if(phone == '' || phone == 'Phone Number'){
		document.getElementById('phone').style.color = 'red';
		passedCheck = false;
	}
	if(email == '' || email == 'Email Address'){
		document.getElementById('email').style.color = 'red';
		passedCheck = false;
	}	
	
	return passedCheck; }

function submitForm(){
	if(errorCheckForm()){
		var type = document.getElementById('type').value;
		var name = document.getElementById('firstName').value + ' ' + document.getElementById('lastName').value;
		var phone = document.getElementById('phone').value;
		var email = document.getElementById('email').value;
		var date1 = document.getElementById('date1').value;
		var time1 = document.getElementById('time1').value;
		var date2 = document.getElementById('date2').value;
		var time2 = document.getElementById('time2').value;
		var date3 = document.getElementById('date3').value;
		var time3 = document.getElementById('time3').value;
		var wantPro = document.getElementById('wantPro').value;
		
		document.getElementById('sending').style.display = 'block';
		document.getElementById('sending').innerHTML = '<p style="text-align:center; padding-top:240px;"><br/><br/><em><strong>Sending Email...</strong></em><br/><img src="/giftCert_framework/images/loading.gif" alt="sending email" width="220" height="19" /></p>';
		
		xmlhttp.open("GET", "/ajaxHandler.php?action=scheduleAppointment&type=" + type + '&name=' + name + '&phone=' + phone + '&email=' + email + '&date1=' + date1 + '&time1=' + time1 + '&date2=' + date2 + '&time2=' + time2 + '&date3=' + date3 + '&time3=' + time3 + '&wantPro=' + wantPro );
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				response = xmlhttp.responseText;
				//alert(response);
				document.getElementById('sending').innerHTML = '<p style="text-align:center; padding-top:240px;"><br/><br/><br/><em><strong>Your appointment request has been sent.</strong></em></p>';
				setTimeout("closeform()",2500);
			}
		}
		xmlhttp.send(null);
	}
}

function closeform(){
	document.getElementById('sending').style.display = 'none';
	tb_remove();
}

function setDefault(input, defaultValue){
	var inputValue = trim(input.value);
	if(inputValue == ''){
		input.value = defaultValue;
		input.style.color = '#999999';
		input.style.fontStyle = 'italic';
	}
}



function clearInput(input){ //alert(input.value);
	var value = input.value;
	switch(value){
		case 'First Name':
		case 'Last Name':
		case 'City':
		case 'Time':
		case 'Date':
		case 'Phone Number':
		case 'Email Address':
			input.value = '';
			input.style.color = 'black';
			input.style.fontStyle = 'normal';
			break;
	}
}


var openedForm = '';
function showGiftCertForm(id){
	if(openedForm != id){
		if(openedForm != '') jQuery("#" + openedForm).slideUp("medium");
		
		jQuery("#" + id).slideDown("medium");
		openedForm = id;
	}
	
	return false; }

function setType(value){
	document.getElementById('type').value = value;
}

jQuery(document).ready(function(){ 
	if(document.getElementById('nav2') != null){
		document.getElementById('nav').innerHTML = document.getElementById('nav2').innerHTML;
	}
});