
// MENU: imitates the button up on mouse click
function buttonup(obj) {
obj.style.borderStyle="outset";
}
// MENU: imitates the button down on a mouse click
function buttondown(obj) {
obj.style.borderStyle="inset";
}

// ALL: custom trim funtion so javascript can make sure check that no required filed is left blank
function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

// SHIPPING FORM: if the "CheckForm()" verifies a field as not blank this will change the image to a checked box
function validimg(str) {
	document.getElementById(str).src = 'valid.jpg';
	document.getElementById(str).title = 'Validated';
}

// SHIPPING FORM: if the "CheckForm()" verifies a field is blank this will change the image to an X'ed box
function invalidimg(str) {
	document.getElementById(str).src = 'invalid.jpg';
	document.getElementById(str).title = 'Invalid';
}

// SHIPPING FORM: array's for "CheckForm()" to loop through the input fields that must be filed in
var input_array=["cname", "pphone", "sphone" , "street1", "city", "state", "country", "zipcode", "notes"];
var valimg_array=["nameimg", "phoneimg", "phoneimg", "streetimg", "cityimg", "stateimg", "countryimg", "zipimg", "notesimg"];

// SHIPPING FORM: check that required fields are not blank
function CheckForm() {

var finput = 0;
	while (finput <= 8) {

		if((input_array[finput] == 'pphone' || input_array[finput] == 'sphone') && (trim(document.getElementById('pphone').value) != '' || trim(document.getElementById('sphone').value) != '')) {
			validimg('phoneimg');
		}
		else if(trim(document.getElementById(''+input_array[finput]+'').value) != '') {
			validimg(valimg_array[finput]);
		}
		else {
			invalidimg(valimg_array[finput]);
		}
	finput++;
	}
}

// SHIPPING FORM: checks the form one last time before creating the data for AJAX to send to sedemail.php
function Shippingvalidate() {
var error = '';
var finput = 0;
	while (finput <= 8) {

		if(document.getElementById(''+valimg_array[finput]+'').title == 'Invalid') {
			error = 'Invalid';
		}
	finput++;
	}
	if (error != '') {
	document.getElementById('proccess').innerHTML = 'One or more required fields was left blank<br>Please check and resubmit the form';
	document.getElementById('proccess').style.display = 'block';
	CheckForm();
	}
	else {
	document.getElementById('proccess').style.display = 'none';
	
	document.getElementById('printwindow').innerHTML ='<iframe id="printframe" name="printframe" scrolling="no" frameborder="0" width="0" height="0" src="printform.php"></iframe>';
	
	// SHIPPING FORM: data for send to the AJAX function
	var params = "formtype="+document.getElementById('formtype').value;
	params += "&fname="+document.getElementById('cname').value;
	params += "&fpphone="+document.getElementById('pphone').value;
	params += "&fsphone="+document.getElementById('sphone').value;
	params += "&fgift="+document.getElementById('gift').value;
	params += "&femail="+document.getElementById('email').value;
	params += "&fstreet1="+document.getElementById('street1').value;
	params += "&fstreet2="+document.getElementById('street2').value;
	params += "&fstreet2="+document.getElementById('city').value;
	params += "&fstate="+document.getElementById('state').value;
	params += "&fcountry="+document.getElementById('country').value;
	params += "&fzip="+document.getElementById('zipcode').value;
	params += "&fnotes="+document.getElementById('notes').value;
	
	sendmail(params);
	}
}

// CONTACT FORM: checks the form one last time before creating the data for AJAX to send to sedemail.php
function Contactvalidate() {
var error;

	if(trim(document.getElementById('email').value) == '') {
		error += 'Please fill in the E-mail field, as we would like to be able ot reply to your email<br>';
	if(trim(document.getElementById('notes').value) == ''){
		error += 'Please fill in the Message field<br>';
	}

	if(error != '')
	document.getElementById('proccess').innerHTML = error;
	document.getElementById('proccess').style.display = 'block';
	}
	else {
	document.getElementById('proccess').style.display = 'none';
	
	// CONTACT FORM: data for send to the AJAX function
	var params = "formtype="+document.getElementById('formtype').value;
	params += "&fname="+document.getElementById('name').value;
	params += "&femail="+document.getElementById('email').value;
	params += "&fsubject="+document.getElementById('subject').options[document.getElementById('subject').selectedIndex].value;
	params += "&fsubject2="+document.getElementById('subject2').value;
	params += "&fnotes="+document.getElementById('notes').value;
	
	sendmail(params);
	}
}

// EMAILING DATA: the AJAX function
function sendmail(params) {

var url = 'sendemail.php'

	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.open("POST", url, true);
		req.setRequestHeader('User-Agent','XMLHTTP/1.0');
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Connection", "close");
		req.onreadystatechange = processReqChange;
		req.send(params);
	}
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.open("POST", url, true);
			req.setRequestHeader('User-Agent','XMLHTTP/1.0');
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Connection", "close");
			req.onreadystatechange = processReqChange;
			req.send(params);
		}
	}
}

// EMAILING DATA: once data is returned to the AJAX function this process XML sent from sendemail.php
function processReqChange() {

	if (req.readyState == 4) {

		if (req.status == 200) {

		var mailresponse  = req.responseXML.documentElement;

			try {

				formstatus(mailresponse.getElementsByTagName("status")[0].childNodes[0].nodeValue);
			}
			catch (error) {

				formstatus('error');
			}
		}
	}
}

// EMAILING DATA: displays user freindly success or errors reported to the AJAX function
function formstatus(status) {

	if((document.getElementById('formtype').value == 'Shipping Form') && (status == 'error')){
	
		document.getElementById('proccess').innerHTML = 'An Error Has Occured! Please try resubmiting the form.<br>If the Error persists please feel free to call us';
		document.getElementById('proccess').style.display = 'block';
		
		return true;
	}
	if((document.getElementById('formtype').value == 'Shipping Form') && (status == 'success')){
	
		document.getElementById('proccess').style.display = 'none';
		
		return true;
	}
	else if((document.getElementById('formtype').value == 'Contact Form') && (status == 'error')){
	
		document.getElementById('proccess').innerHTML = 'An Error Has Occured! Please try sending the email again.<br>If the Error persists please feel free to call us';
		document.getElementById('proccess').style.display = 'block';
		
		return true;
	}
	else if((document.getElementById('formtype').value == 'Contact Form') && (status == 'success')){
	
		document.getElementById('proccess').innerHTML = 'Thank you ' + document.getElementById('name').value + ' for contacting us.<br>Your ' + document.getElementById('subject').options[document.getElementById('subject').selectedIndex].value + ' has been sent to <b>info@bookrepair.us</b>';
		document.getElementById('proccess').style.display = 'block';
	
		return true;
	}
	else{
		return false;
	}
}

// SHIPPING FORM: once the email was successful fill in the data in printform.php and open the users print window
function PrintForm() {

	document.getElementById('pfcname').innerHTML = parent.window.document.getElementById('cname').value;
	document.getElementById('pfpphone').innerHTML = parent.window.document.getElementById('pphone').value;
	document.getElementById('pfsphone').innerHTML = parent.window.document.getElementById('sphone').value;
	document.getElementById('pfgift').innerHTML = parent.window.document.getElementById('gift').value;
	document.getElementById('pfemail').innerHTML = parent.window.document.getElementById('email').value;
	document.getElementById('pfstreet1').innerHTML = parent.window.document.getElementById('street1').value;
	document.getElementById('pfstreet2').innerHTML = parent.window.document.getElementById('street2').value;
	document.getElementById('pfcity').innerHTML = parent.window.document.getElementById('city').value;
	document.getElementById('pfstate').innerHTML = parent.window.document.getElementById('state').value;
	document.getElementById('pfcountry').innerHTML = parent.window.document.getElementById('country').value;
	document.getElementById('pfzipcode').innerHTML = parent.window.document.getElementById('zipcode').value;
	document.getElementById('pfnotes').innerHTML = parent.window.document.getElementById('notes').value;
	
window.print();
}