// JavaScript Document
function createObject() { 
   var req; 
   if(window.XMLHttpRequest){ 
      // Firefox, Safari, Opera... 
      req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
      // Internet Explorer 5+ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
      alert('Problem creating the XMLHttpRequest object'); 
   } 
   return req; 
} 

// Make the XMLHttpRequest object 
var http = createObject(); 


function SaveImage(x, y, image_path, bg_path, mask_path, h, w, id, key){
var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', 'actions.php?action=save_image&y='+y+'&x='+x+'&image_path='+image_path+'&bg_path='+bg_path+'&mask_path='+mask_path+'&h='+h+'&w='+w+'&id='+id+'&key='+key+'');
   	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) { 
				//document.getElementById('output').style.display = 'block'; 
				document.getElementById('output').innerHTML = http.responseText; 
      		} 
   		}
	} 
   	http.send(null); 
}

function SaveImageButton(x, y, image_path, bg_path, mask_path, h, w, id, key, pid, pnumid){
var ran_no=(Math.round((Math.random()*9999)));
	http.open('get', 'actions.php?action=save_image_button&y='+y+'&x='+x+'&image_path='+image_path+'&bg_path='+bg_path+'&mask_path='+mask_path+'&h='+h+'&w='+w+'&id='+id+'&key='+key+'&pid='+pid+'&pnumid='+pnumid);
   	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) { 
				//document.getElementById('output').style.display = 'block'; 
				document.getElementById('output').innerHTML = http.responseText;
      		} 
   		}
	} 
   	http.send(null); 
}


function AffiliateImageButton(x, y, image_path, bg_path, mask_path, h, w, id, key, pid){
var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', 'actions.php?action=affiliate_save_image&y='+y+'&x='+x+'&image_path='+image_path+'&bg_path='+bg_path+'&mask_path='+mask_path+'&h='+h+'&w='+w+'&id='+id+'&key='+key+'&pid='+pid);
   	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) { 
				//document.getElementById('output').style.display = 'block'; 
				document.getElementById('output').innerHTML = http.responseText; 
      		} 
   		}
	} 
   	http.send(null); 
}


function remove_image(image_id, token, product_id){
var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', 'actions.php?action=remove_image&id='+image_id+'&token='+token+'&pid='+product_id+'');
   	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) {
				
				if(response == 'error') {
				document.getElementById('warning_'+image_id).style.color = '#F30C29';
				document.getElementById('warning_'+image_id).style.backgroundColor = '#F8EF06';
				document.getElementById('warning_'+image_id).innerHTML = "This item must be customized, please return to product page and upload your images.";
				//document.getElementById('warning_'+image_id).style.display = "block";
				document.getElementById('checkout_link').style.display = 'none';
				}
				document.getElementById('image_'+image_id).innerHTML = '';
				document.getElementById('image_'+image_id).style.display = 'none';
				//location.href = 'cart-view.php';//temp hack
				//document.getElementById('output').style.display = 'block'; 
				//document.getElementById(''+image_id+'').style.display = 'none'; 
      		} 
   		}
	} 
   	http.send(null); 
}

function textCounter( field, maxlimit) {
	var counter;	
	if (field.value.length > maxlimit){ // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	}else{
		counter = maxlimit - field.value.length;
		document.getElementById('word_limit').innerHTML = counter;
	}
}

function save_engraving(engraving, pid, token, pnum){
var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', 'actions.php?action=save_engraving&engraving='+engraving+'&token='+token+'&pid='+pid+'&ran='+ran_no+'&pnum='+pnum);
   	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) { 
				document.getElementById('engrave_output_'+pnum).innerHTML = http.responseText; 
      		} 
   		}
	} 
   	http.send(null); 
}
