﻿//contact.js

function create_object(){
	var request_type;
	
	try{
		// Opera 8.0+, Firefox, Safari
		request_type = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			request_type = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				request_type = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// other
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return request_type;
}

var http_contact = create_object();

function contact(){
	
	var result_el = document.getElementById('result');
	result_el.innerHTML = '';
	
	var url = "assets/en-us/contact/contact.php";
	
	//get form details
	var name = encodeURI(document.getElementById('contact_name').value);
	var email = encodeURI(document.getElementById('contact_email').value);
	var phone = encodeURI(document.getElementById('contact_phone').value);
	var cellphone = encodeURI(document.getElementById('contact_cellphone').value);
	var homepage = encodeURI(document.getElementById('contact_homepage').value);
	var topic = encodeURI(document.getElementById('contact_topic').value);
	var details = encodeURI(document.getElementById('contact_details').value);
	
	
	if (check_contact_form(name, email, topic, details)){
		
		http_contact.open("POST", url, true);
		
		var params = 'name='+name+'&email='+email+'&phone='+phone+'&cellphone='+cellphone+'&homepage='+homepage+'&topic='+topic+'&details='+details;
	
		//Send the proper header information along with the request
		http_contact.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_contact.setRequestHeader("Content-length", params.length);
		http_contact.setRequestHeader("Connection", "close");
	
		//Call a function when the state changes
		http_contact.onreadystatechange = function() {
			if(http_contact.readyState == 4 && http_contact.status == 200) {
				var response = http_contact.responseText;
				//alert(response);
				switch (response){
				
					case '1':
						//reset form
						
						document.form_contact.reset();
						result_el.innerHTML = "<span style='font-size:medium;color:green;'>The message was successfully sent.</span>";
						//IE bug fix
						if (detect_old_IE()){
							alert("The message was successfully sent.");
						}
						break;
					case '2':
						result_el.innerHTML = "<span style='font-size:medium;color:red;'>The E-mail address is not valid.</span>";
						//IE bug fix
						if (detect_old_IE()){
							alert("The E-mail address is not valid.");
						}
						break;
					case '0':
						result_el.innerHTML = "<span style='font-size:medium;color:red;'>An error has occurred during the process, Please try again.</span>";
						//IE bug fix
						if (detect_old_IE()){
							alert("An error has occurred during the process, Please try again.");
						}
						break;
					default:
						result_el.innerHTML = "<span style='font-size:medium;color:red;'>An error has occurred during the process, Please try again.</span>";
						//IE bug fix
						if (detect_old_IE()){
							alert("An error has occurred during the process, Please try again.");
						}
						break;
						
				}
				
			}
		}
		http_contact.send(params);
	}

}

function check_contact_form(name, email, topic, details){

	var result_el = document.getElementById('result');
	
	if (name.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Name' field is required.</span>";
		return false	
	}
	if (email.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'E-mail' field is required.</span>";
		return false
	}
	if (topic.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Subject' field is required</span>";
		return false
	}
	if (details.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Message' field is required..</span>";
		return false
	}
	return true;

}



var http_friend = create_object();
var nocache = 0;

function contact_friend(){

	/*document.getElementById('result').innerHTML = '';*/
	
	//get form details
	var name = encodeURI(document.getElementById('contact_name').value);
	var email = encodeURI(document.getElementById('contact_email').value);
	var friend_name = encodeURI(document.getElementById('friend_name').value);
	var friend_email = encodeURI(document.getElementById('friend_email').value);
	var details = encodeURI(document.getElementById('contact_details').value);
	if (check_friend_form (name, email, friend_name, friend_email)){
		http_friend.open('get', 'assets/en-us/contact/contact_friend.php?name='+name+'&email='+email+'&friend_name='+friend_name+'&friend_email='+friend_email+'&details='+details+'&nocache='+nocache);
		http_friend.onreadystatechange = contact_friendReply;
		http_friend.send(null);	
	}

}

function contact_friendReply() {

var result_el = document.getElementById('result'); 

if(http_friend.readyState == 4){
	var response = http_friend.responseText;

	switch (response){
		case '1':
			//reset form
			document.form_friend.reset();
			result_el.innerHTML = "<span style='font-size:medium;color:green;'>The message was successfully sent.</span>";
			//IE bug fix
			if (detect_old_IE()){
				alert("The message was successfully sent.");
			}
			break;
		case '2':
			result_el.innerHTML = "<span style='font-size:medium;color:red;'>The E-mail address is not valid.</span>";
			//IE bug fix
			if (detect_old_IE()){
				alert("The E-mail address is not valid.");
			}
			break;
		case '3':
			result_el.innerHTML = "<span style='font-size:medium;color:red;'>The friend's E-mail is not valid.</span>";
			//IE bug fix
			if (detect_old_IE()){
				alert("The friend's E-mail is not valid.");
			}
			break;
		case '0':
			result_el.innerHTML = "<span style='font-size:medium;color:red;'>An error has occurred during the process, Please try again.</span>";
			//IE bug fix
			if (detect_old_IE()){
				alert("An error has occurred during the process, Please try again.");
			}
			break;
		default:
			result_el.innerHTML = "<span style='font-size:medium;color:red;'>An error has occurred during the process, Please try again.</span>";
			//IE bug fix
			if (detect_old_IE()){
				alert("An error has occurred during the process, Please try again.");
			}
			break;
	}
}

}


function check_friend_form(name, email, friend_name, friend_email){

	var result_el = document.getElementById('result');
	
	if (name.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Name' field is required.</span>";
		return false	
	}
	if (email.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'E-mail' field is required.</span>";
		return false
	}
	if (friend_name.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Friend's name' field is required.</span>";
		return false
	}
	if (friend_email.length == 0){
		result_el.innerHTML = "<span style='font-size:medium;color:red;'>'Friend's E-mail' field is required.</span>";
		return false
	}
	return true;

}
