// Create global vars
var root_url ='';
var page_title ='';
var this_dir ='';
var request = makeObject();
// On page load add javascript functions to links with
// a classname of cart_button
window.onload = createCartLinks;



var http_request = false;

// This function adds the addToCart function to links
// with a classname of cart_button
// Essentially making them <a href = "#" onclick = "addToCart(...);" />
// Unobtrusive Javascript at its best.
function createCartLinks() {	
	if (!document.getElementsByTagName) {		
		return;
	}
	var anchors = document.getElementsByTagName("a");	
	for (var i=0; i<anchors.length; i++) {		
		var anchor = anchors[i];
		if (anchor.className == 'cart_button') {
			anchor.onclick = function(){addToCart(this);return false;} //attach the onclick to each link
		}
	}
	//checkCart();
}

// Here we set all the globals defined above
// We are receiving the url,title,dir from the header.inc
// We also perform a checkCart to see if any items are in
// the cart.
function setPageVars(url,title,dir) {
	root_url = url;
	page_title = escape(title);
	this_dir = dir;
	checkCart();
}

// This function is called on page load from the setPageVars
// function.  This simply calls the checkout funcs file via
// ajax and displays the cart if it has contents.
function checkCart() {
	var poststr = 'show_cart' + "=" + '1' + 
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	var url = root_url+'/includes/shoppingCartAjaxFuncs.php';

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }


   http_request.onreadystatechange = ajaxUpdateCart;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);

}

// This function sends the product id to the checkout funcs
// file via ajax and adds products to the cart
function addToCart(the_product_id) {
	var product_id = the_product_id.id;
	var url = root_url+'/includes/shoppingCartAjaxFuncs.php';
	var poststr = "add_product=" + product_id +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }


   http_request.onreadystatechange = ajaxUpdateCart;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);

}

// This function sends the product id to the checkout funcs
// file via ajax and removes products to the cart
function removeFromCart(the_product_id) {
	var product_id = the_product_id;
	var poststr = 'remove_product' + "=" + product_id + 
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	var url = root_url+'/includes/shoppingCartAjaxFuncs.php';

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }


   http_request.onreadystatechange = ajaxUpdateCart;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);
}

// This function is called by checkCart,addToCart,removeFromCart
// This function displays the output from the ajax request.
function ajaxUpdateCart(){	
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		document.getElementById('shopping_cart_contents').innerHTML = answer;		
	}
}

// This function creates a http xml object. (AJAX)
// All common browsers are accounted for down to IE 5
// If a user has IE 4 or an unsupported browser they
// will receive an alert telling them to contact us.
function makeObject(){
	var xmlHttp;
	try 
	{
		// Firefox, Opera 8.0+, Safari, Chrome, Internet Explorer 8
		xmlHttp=new XMLHttpRequest();
	} 
	catch (e) 
	{
		// Internet Explorer 7+
		try 
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			// Internet Explorer 5 and 6
			try 
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
				// Internet Explorer < 4, other
				alert("Your browser does not accept ajax. Please contact support at 773 529 7755 ext. 13");
				return false;
			}
		}
	}
	return xmlHttp;
}

// This function checks the parent email to see if it exists
// if the email exists the user is prompted to enter their
// password
function checkStudentEmail(studentEmail) {
	var getstr = "";
	var the_contents = document.getElementById(studentEmail);
	poststr = 'checkStudentEmail' + "=" + the_contents.value + 
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }


	var url = root_url+'/includes/checkoutAjaxFuncs.php';
	request.url = url;

	http_request.onreadystatechange = processCheck;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);


}

// This is the handler for the checkStudentEmail function
function processCheck(){
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		if(answer == 'smartLogin') {
			document.getElementById('email').style.border = "1px solid #600"; 
			document.getElementById('loginFields').innerHTML =
			'<div style="clear:both;"></div>'+
			'<span class="form_note" style="color:#600;font-size:110%;">'+
			'This email is already in use.  Please login <a href="'+root_url+'login.php?url=online/checkout.php">here</a>.'+
			'</span>';
					
			document.getElementById('login_password').focus();
			document.getElementById('loginFieldsNote').style.display = 'none';
		} else {
			//document.getElementById('parent_password').focus();
			document.getElementById('loginFields').innerHTML = '';
			//document.getElementById('loginFieldsNote').style.visibility = 'visible';
			
		}	
	}
}

// This function takes all the info from the parent address fields and
// inserts it into the student address fields
function preFillStudentAddress() {
	var parent_address = document.getElementById('parent_address').value;
	var parent_city = document.getElementById('parent_city').value;
	var parent_state = document.getElementById('parent_state').value;
	var parent_zip = document.getElementById('parent_zip').value;
	document.getElementById('address1').value = parent_address;
	document.getElementById('address_city').value = parent_city;
	document.getElementById('address_state').value = parent_state;
	document.getElementById('address_zip').value = parent_zip;
}

// This function takes all the info from the parent address fields and
// inserts it into the billing address fields
function preFillBillingAddress() {
	var parent_first_name = document.getElementById('parent_first_name').value;
	var parent_last_name = document.getElementById('parent_last_name').value;
	var parent_address = document.getElementById('parent_address').value;
	var parent_city = document.getElementById('parent_city').value;
	var parent_state = document.getElementById('parent_state').value;
	var parent_zip = document.getElementById('parent_zip').value;
	document.getElementById('billing_first_name').value = parent_first_name;
	document.getElementById('billing_last_name').value = parent_last_name;
	document.getElementById('billing_address').value = parent_address;
	document.getElementById('billing_city').value = parent_city;
	document.getElementById('billing_state').value = parent_state;
	document.getElementById('billing_zip').value = parent_zip;
}

// This functions shows a list of cities based on the chosen state
function getCityList() {
	var state = document.getElementById('student_school_state').value;
	
	var poststr = "";
	poststr = 'student_school_state' + "=" + '1' + 
	"&" + 'student_school_state_value' + "=" + state +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	var url = root_url+'includes/checkoutAjaxFuncs.php';

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = processCityCheck;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);

}

// This is the handler for the getCityList function
function processCityCheck(){
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		document.getElementById('cityList').innerHTML = answer;
		getSchoolList();
	}
}

// This functions shows a list of schools based on the chosen city
function getSchoolList() {
	var city = document.getElementById('student_school_city').value;
	
	poststr = 'student_school_city' + "=" + '1' + 
	"&" + 'student_school_city_value' + "=" + city +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	var url = root_url+'includes/checkoutAjaxFuncs.php';
	
	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = processSchoolCheck;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);

	
}

// This is the handles for the getSchoolList function
function processSchoolCheck(){
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		document.getElementById('schoolList').innerHTML = answer;
	}
}

// This function checks the school type and hides the school info if the school
// type is not standard
function checkSchoolDetails() {
	if(document.getElementById('student_school_type')) {
		var student_school_type = document.getElementById('student_school_type').value;
		if(student_school_type != 'standard') {
			document.getElementById('schoolDetailsLists').style.display='none';
		} else {
			document.getElementById('schoolDetailsLists').style.display='block';
		}
	}
}


// This function checks if the discount code is valid.  if it is it calls
// the apply code function and applies it to the cart.
function checkDiscountCode() {
	var discount_code = document.getElementById('discount_code').value;
	poststr = 'discount_code' + "=" + discount_code + 
	"&" + 'check_discount_code' + "=" + '1' +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	var url = root_url+'includes/shoppingCartAjaxFuncs.php';
	
	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = processDiscountCheck;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);
	
	

}

// This is the handles for the getSchoolList function
function processDiscountCheck(){	
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		if(answer == 'Valid') {
			applyDiscountCode();
			document.getElementById('discountFieldsNote').style.color = 'green';
			document.getElementById('discountFieldsNote').innerHTML = 'Discount Applied';
			checkForFreeCart();
		} else {	
			checkCart();		
			document.getElementById('payment_details_box').style.display = 'block';
			document.getElementById('discountFieldsNote').style.color = 'red';
			document.getElementById('discountFieldsNote').innerHTML = answer;
		}
	}
}

// This function checks if the discount made the products free.  if it 
// is it hides the payment details.
function checkForFreeCart() {
	var discount_code = document.getElementById('discount_code').value;

	poststr = 'discount_code' + "=" + discount_code + 
	"&" + 'apply_discount_code' + "=" + '1' +
	"&" + 'check_for_free' + "=" + '1' +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	
	var url = root_url+'includes/shoppingCartAjaxFuncs.php';

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

	http_request.onreadystatechange = processFreeCartCheck;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);

}

// This is the handles for the getSchoolList function
function processFreeCartCheck(){	
	if(http_request.readyState == 4){
		var answer = http_request.responseText;
		if(answer == '0') {
			document.getElementById('payment_details_box').style.display = 'none';
		}
		applyDiscountCode();
	}
}


// This function checks if the discount code is valid.  if it is it calls
// the apply code function and applies it to the cart.
function applyDiscountCode() {
	var discount_code = document.getElementById('discount_code').value;

	poststr = 'discount_code' + "=" + discount_code + 
	"&" + 'apply_discount_code' + "=" + '1' +
	"&path_to_root=" + root_url +
	"&page_title=" + page_title +
	"&this_dir=" + this_dir;
	var url = root_url+'includes/shoppingCartAjaxFuncs.php';

	http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }


   http_request.onreadystatechange = ajaxUpdateCart;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", poststr.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(poststr);


}

//Ah do the checkout
function doCheckout() {
	document.getElementById('registration').submit();
	document.getElementById('checkout_button').innerHTML = '<img src="http://localhost:8888/trunk/app/images/ajax-loader.gif" /><br />PROCESSING...<br />Please be patient, do not refresh your browser or navigate away from this page.';
	//document.getElementById('checkout_button').style.display = 'none';
	//document.getElementById('checkout_button').style.visibility = 'hidden';
}
function submitApplication() {
	document.getElementById('application').submit();
}
