
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
	return;
	}

//function create_cookie(name,value) {
//	order = value + "; ";
//	var date = new Date();
//	date.setTime(date.getTime()+(360*24*60*60*1000));
//	var expires = "; expires="+date.toGMTString();
//	document.cookie = name+"="+order+expires+"; path=/; domain=.clipartof.com;";
//	return;
//	}



function create_cookie(name,value) {
//	alert(name);
//	alert(value);
	if(name == 'images_per_page'){
		//alert(document.images_per_page_form.images_per_page.value);
		value = '';
		value = document.images_per_page_form.images_per_page.value;
		}
	else if(name == 'display_image_size'){
		value = '';
		value = document.display_image_size_form.display_image_size.value;
		}
	else if(name == 'order_of_images'){
		value = '';
		value = document.order_of_images_form.order_of_images.value;
		}
	else if(name == 'satellite_site'){
		value = '';
		value = document.satellite_site_form.satellite_site.value;
		}
	else if(name == 'image_dimensions'){
		value = '';
		value = document.image_dimensions_form.image_dimensions.value;
		}
		
	order = value + "; ";
	var date = new Date();
	date.setTime(date.getTime()+(360*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+order+expires+"; path=/; domain=.clipartof.com;";
	
	if(name == 'images_per_page'){
		//document.write(location.href);
		var destination = location.href;

//		destination = destination.replace(/search\/(.*)\/.*/, "search/$1");
		destination = destination.replace(/new\/(.*)\/\d+/, "new/$1/1");
		destination = destination.replace(/portfolio\/(.*)\/.*/, "portfolio/$1");
		
		window.location = destination;
		}
	else if(name == 'display_image_size'){
		window.location = location.href;
		}
	else if(name == 'order_of_images'){
		var destination = location.href;
		destination = destination.replace(/portfolio\/(.*)\/(.*)\/\d+/, "portfolio/$1/$2");
		destination = destination.replace(/portfolio\/(.*)\/\d+/, "portfolio/$1");
		destination = destination.replace(/new\/(.*)\/\d+/, "new/$1/1");
		
		window.location = destination;
		}
	else if(name == 'satellite_site'){
		window.location = location.href;
		}
	else if(name == 'image_dimensions'){
		window.location = location.href;
		}

	return;
}




	
function kill_cookie(name) {
	var order = "; ";
	var date = new Date();
	date.setTime(date.getTime()+(-1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+order+expires+"; path=/; domain=.clipartof.com;";
	}

function getXMLHTTPRequest(){
	var req = false;
	try{req = new XMLHttpRequest();} /*  e.g. Firefox  */
	catch(err1){
		try{req = new ActiveXObject("Msxm12.XMLHTTP");} /*  some versions IE  */
		catch(err2){ 
			try{req = new ActiveXObject("Microsoft.XMLHTTP");} /*  some versions IE  */
			catch(err3){req = false;}
			}
		}
	return req;
	}
var myRequest = getXMLHTTPRequest();

	
function chat(){
	if(document.chat_form.message.value == '' || document.chat_form.message.value == 'Please enter a question.' || document.chat_form.message.value == 'Question' || document.chat_form.message.value == 'Need help? Ask a question to begin speaking with a representative.'){document.chat_form.message.value = "Please enter a question."; return;}
	var name_of_chatter = 'Customer';
	var chatters_color = 'blue';
	var webmaster_cookie = readCookie("webmaster");
	if(typeof(webmaster_cookie)!="undefined"){ 
		name_of_chatter = webmaster_cookie;
		chatters_color = 'green';
		}
	var url = "/cgi-bin/chat.pl?message=" + encodeURIComponent(document.chat_form.message.value);
	document.chat_form.message.value = '';
	var myRandom=parseInt(Math.random()*99999999);
	myRequest.open("GET", url + "&rand=" + myRandom, true);
	myRequest.onreadystatechange = chat_response;
	myRequest.send(null);
	}
function chat_response(){
	if(myRequest.readyState == 4){
		document.getElementById('waiting').innerHTML = '';
		//alert("0");
		if(myRequest.status == 200){
			var value = myRequest.responseText; 
			var value_array = value.split('||');
			var chat_id = value_array[0];
			var update_visitor_cookie_id_field = value_array[1];
			var message = value_array[2];
			//alert("1");
			
			

			//if visitor cookie does not exist
			if(document.cookie && document.cookie.indexOf('visitor') == -1 || update_visitor_cookie_id_field == 'y'){
				create_cookie('visitor', chat_id);
				}
				
			//post message to screen
			document.getElementById('feedback').innerHTML = message;
			}
		else{
			alert("An error has occurred: " + myrequest.statusText);
			}
		}
	else{
		//alert("2");
		document.getElementById('waiting').innerHTML = '<a href="/"></a>';
		}
	}
	
	
function get_messages(){
	var cookie_results = readCookie("visitor");
	//alert(cookie_results);

	if(cookie_results >= 1){ 
		var url = "/cgi-bin/chat.pl?do=GetMessages";
		var myRandom=parseInt(Math.random()*99999999);
		myRequest.open("GET", url + "&rand=" + myRandom, true);
		myRequest.onreadystatechange = get_messages_response;
		myRequest.send(null);
		}
	else{return;}
	}
function get_messages_response(){
	if(myRequest.readyState == 4){
		document.getElementById('waiting').innerHTML = '';
		if(myRequest.status == 200){
			var value = myRequest.responseText; 
			//alert(value);
			if(value == ''){kill_cookie("visitor");}
			document.getElementById('feedback').innerHTML = value;
			}
		else{
			alert("An error has occurred: " + myrequest.statusText);
			}
		}
	else{
		document.getElementById('waiting').innerHTML = '<a href="/"></a>';
		}
	}