// JavaScript Document

function addToPort(i_num, proj)
{
	document.getElementById('in_port['+i_num+']').innerHTML = 'processing...';
	JsHttpRequest.query(
		'/add_to_port.php',
		{ 'i_num': i_num, 'proj': proj },
		function(result, errors) { 
			document.getElementById('in_port_proj['+i_num+']').innerHTML = errors;
		},
		false
	);
}

function addToPortFromCart(i_num, proj){
	if(confirm('You are going to add the item to your portfolio!\n\nDo you want to continue?')){
		document.getElementById('loading').style.display = "";
		JsHttpRequest.query(
			'/add_to_port.php',
			{ 'i_num': i_num, 'proj': proj },
			function(result, errors) {
				loadMyCart();
			},
			true
		);
	} else {
		document.getElementById('add_to_port_proj['+i_num+']').value = "";
	}
}

function addToCart(i_num)
{
	document.getElementById('in_cart['+i_num+']').innerHTML = 'processing...';
	JsHttpRequest.query(
		'/add_to_cart.php',
		{ 'i_num': i_num },
		function(result, errors) { 
			document.getElementById('in_cart['+i_num+']').innerHTML = errors;
		},
		false
	);
}

function loadMyOrder(){
	document.getElementById('loading').style.display = "";
	JsHttpRequest.query(
		'/checkout/order_items.php',
		{ 'action':'load_my_order' },
		function(result, errors) {
			document.getElementById('myOrder').innerHTML = errors;
			document.getElementById('loading').style.display = "none";
		},
		true
	);
}

function loadMyCart(){
	document.getElementById('loading').style.display = "";
	JsHttpRequest.query(
		'/cart/cart_items.php',
		{ 'action':'load_my_cart' },
		function(result, errors) {
			document.getElementById('myCart').innerHTML = errors;
			document.getElementById('loading').style.display = "none";
		},
		true
	);
}

function loadMyProject(idProject){
	document.getElementById('loading').style.display = "";
	JsHttpRequest.query(
		'/projects/projects_manage.php',
		{ 'action':'load_project', 'idProject':idProject },
		function(result, errors) {
			document.getElementById('my_projects').innerHTML = errors;
			document.getElementById('loading').style.display = "none";
		},
		true
	);
}

function loadMyProjects(){
	document.getElementById('loading').style.display = "";
	JsHttpRequest.query(
		'/projects/projects_manage.php',
		{ 'action':'load_all' },
		function(result, errors) {
			document.getElementById('my_projects').innerHTML = errors;
			document.getElementById('loading').style.display = "none";
		},
		true
	);
}

function showAddNewPrForm(a){
	if(a=='s'){
		document.getElementById('main_btn').style.display='none';
		document.getElementById('new_pr_form').style.display='';
	} else {
		document.getElementById('main_btn').style.display='';
		document.getElementById('new_pr_form').style.display='none';
		document.getElementById('new_project').value='';
	}
}

function showEmailProdForm(a){
	if(a=='s'){
		document.getElementById('eml_to_smbd').style.display='none';
		document.getElementById('info_box').style.display='';
	} else {
		document.getElementById('eml_to_smbd').style.display='';
		document.getElementById('info_box').style.display='none';
		document.getElementById('to_name').value='';
		document.getElementById('to_email').value='';
	}
}

function sendEmailProd(i_num){

	document.getElementById('loading_eml').style.display = "";
	
	JsHttpRequest.query(
		'/email_prod.php',
		{ 'i_num':i_num, 'name':document.getElementById('to_name').value, 'email':document.getElementById('to_email').value, 'Comments':document.getElementById('Comments').value, 'name_f':document.getElementById('f_name').value, 'email_f':document.getElementById('f_email').value },
		function(result, errors) {
			document.getElementById('info_box').innerHTML = errors; 
		},
		true
	);
}

function createNewPr(){
	var title = document.getElementById('new_project').value;
	
	if(title!=''){
		document.getElementById('loading').style.display = "";
		JsHttpRequest.query(
			'/projects/projects_manage.php',
			{ 
				'action':'create', 
				'title':title 
			},
			function(result, errors) {
				loadMyProjects();
				showAddNewPrForm('h');
			},
			true
		);
	}
}

function renamePr(idProject, action){

	document.getElementById('pr_loader['+idProject+']').style.display = "";
	var title = (document.getElementById('new_pr_title['+idProject+']')) ? document.getElementById('new_pr_title['+idProject+']').value : '';
	
	JsHttpRequest.query(
		'/projects/projects_manage.php',
		{ 
			'action':action, 
			'idProject':idProject,
			'title':title 
		},
		function(result, errors) {
			document.getElementById('proj['+idProject+']').innerHTML = errors;
			if(document.getElementById('new_pr_title['+idProject+']')) document.getElementById('new_pr_title['+idProject+']').focus();
		},
		true
	);
}

function loadStates(country, states_box, states_list, css_class) {
	JsHttpRequest.query(
		'/load_states.php',
		{
			'country':country, 
			'states_list':states_list, 
			'class':css_class 
		},
		function(result, errors) {
			document.getElementById(states_box).innerHTML = errors;
		},
		true
	);
}
