var debug = 0;

function printpage() { 
	window.print(); 
}

function printpageAll() { 
	newwindow=window.open(window.location.href+"&printall=1");
	if (window.focus) {newwindow.focus()}
	return false;
}


isSafari3 = false;
if(window.devicePixelRatio) isSafari3 = true;
if(!isSafari3) {
	document.write('<style type="text/css">select {/* hide overflow:hidden from IE5/Mac *//* \*/border: 1px solid #C3C0C0;/* */}</style>');
} else {
	document.write('<style type="text/css">select {margin-top:9px;}</style>');
}

function genericCopy(frm_id, from, to){
	var frm = document.getElementById(frm_id);
	if (frm != null){
		for (var i = 0; i < frm.length; i++){
			if ((frm.elements[i].type) && (frm.elements[i].id.indexOf(from) == 0)){
				var fromelem = frm.elements[i];
				var elem_postfix = fromelem.id.split(from)[1];
				var toelem = document.getElementById(to+elem_postfix);
				
				if (toelem != null){
					if (toelem.type && fromelem.type){
						if (toelem.type == 'text' && fromelem.type == 'text'){
							toelem.value = fromelem.value;
						}else if (toelem.type == 'select-one' && fromelem.type == 'select-one'){
							if (toelem.options.length >= fromelem.selectedIndex){
								toelem.selectedIndex = fromelem.selectedIndex;
							}
						}
					}
				}
			}
		}
	}
}

function canSubmit() {
	if (hasClicked) {
		alert("Your order is already being processed");
		return false;
	} else {
		hasClicked = true;
		return true;
	}
}

var xmlhttp = null;

function loadurl(dest,method,args,func) { 
	xmlhttp = null;
	
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest( );
	}else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (! xmlhttp){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	
	if (xmlhttp == null) {
		alert("Error creating request object");
		return false;
	}
	
	args += '&jsReq=1';
	
	// the xmlhttp object triggers an event everytime the status changes 
	// triggered() function handles the events  
	xmlhttp.onreadystatechange = func;
	// open takes in the HTTP method and url.  
	
	// send the request. if this is a POST request we would have  
	if(method=="POST"){
		xmlhttp.open(method, dest,true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send(args);
	} else {
		// Moz is fine with just send(); but  
		// IE expects a value here, hence we do send(null);  
		xmlhttp.open(method, dest + "?" + args,true); 
		xmlhttp.send(null); 
	}
	
	return true;
}

function validateForm(frm_id){
	var frm = document.getElementById(frm_id);
	if (frm != null){
		var args = '';
		
		for (var i = 0; i < frm.length; i++){
			if (frm.elements[i].type){
				var val = '';
				
				if (frm.elements[i].type == 'text'){
					val = frm.elements[i].name+'='+escape(frm.elements[i].value)
				}else if (frm.elements[i].type == 'select-one'){
					if (frm.elements[i].options.length > 0){
						val = frm.elements[i].name+'='+escape(frm.elements[i].options[frm.elements[i].selectedIndex].value);
					}
				}else if (frm.elements[i].type == 'checkbox'){ 
					if (frm.elements[i].checked == true){
						val = frm.elements[i].name+'='+escape(frm.elements[i].value)
					}
				}else{
					val = frm.elements[i].name+'='+escape(frm.elements[i].value)
				}
				
				if ((val != '') && (args != '')){
					args += '&';
				}
				args += val;
			}
		}
		
		args += '&frmid='+frm_id;
		alert(args);
		loadurl(frm.action,frm.method.toUpperCase(),args,function(){evalReq()});
	}
	return false;
}

function evalReq(){
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			if (xmlhttp.getResponseHeader('Content-Type') == 'text/javascript'){
				eval(xmlhttp.responseText);
			}else{
				if (debug == 1){
					alert(xmlhttp.responseText);
				}
			}
		}else if (xmlhttp.status == 500){
			alert("Internal Server Error:\r\n"+xmlhttp.responseText.replace(/(<([^>]+)>)/ig,""));
		} else {
			if (debug == 1){
				alert(xmlhttp.status);
			}
		}
	}
}

function searchPostcode(pcode,listid){
	var d = document.getElementById(pcode);
	var ls = document.getElementById(listid);
	if ((d != null) && (ls != null)){
		loadurl("/searchPostcode.asp","GET","pcode="+d.value+"&listid="+listid,function(){evalReq()});
	}
}

function addPostcode(listobj, frm_id, prefix){
	if (listobj.selectedIndex >= 0){
		var arrLines = listobj.options[listobj.selectedIndex].value.split("::");
		var frm = document.getElementById(frm_id);
		if (frm != null){
			var d;
			
			d = document.getElementById(prefix+'address1')
			if (d != null){
				d.value = arrLines[0];
			}
			
			d = document.getElementById(prefix+'address2')
			if (d != null){
				d.value = arrLines[1];
			}
			
			d = document.getElementById(prefix+'Town')
			if (d != null){
				d.value = arrLines[2];
			}
			
			d = document.getElementById(prefix+'County')
			if (d != null){
				d.value = arrLines[3];
			}
			
			d = document.getElementById(prefix+'Zip')
			if (d != null){
				d.value = arrLines[4];
			}
		}
	}
}

function generateErr(msg, id){
	var obj = '';
	var err = msg;
	var d = document.getElementById(id);

	if (d != null){
		var lbl = d.parentNode
		if ((lbl != null) && (lbl.nodeName == 'LABEL')){
			obj = String (lbl.innerHTML);
			obj = obj.stripTags();
			obj = obj.replace(/\n|\r|\t|:|\*/g,'');
			obj = obj.trim();
			
			obj = ' \''+obj+'\'';
		}
		
		alert(err+obj);
		d.focus();
	} else {
		alert(err);
	}
}

//Taken from the prototype v1.4.0 code
String.prototype.stripTags = function(){
	return this.replace(/<\/?[^>]+>/gi, '');
}


String.prototype.trim = function(){
	var sString = this
	
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

var state = 'none';

/*function showhide(layer_ref, state) {
	if (state == 'block') {
		state = 'none';
	}
	else {
		state = 'block';
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}
	if (document.getElementById &&!document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}*/

function showhide(layer_ref, state) {
	if (state == 'block') {
		state = 'none';
	} 
	else {
		state = 'block';
	}
	if (parent.document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "parent.document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		parent.document.layers[layer_ref].display = state;
	}
	if (parent.document.getElementById &&!parent.document.all) {
		hza = parent.document.getElementById(layer_ref);
		hza.style.display = state;
	}
}

function addArt(artist, title, productcode, edition, size, medium, artistid, price, quantity) {
	var selectOptions = parent.document.getElementById('prod_artist').getElementsByTagName('option');   
	for (var i = 0; i < parent.document.getElementById('prod_artist').options.length; i++) {  
		var arrayartistid = parent.document.getElementById('prod_artist').options[i].value;  
		if (arrayartistid == artistid)   
		{   
			parent.document.getElementById('prod_artist').options[i].selected = true;
		}   
	}
	parent.document.getElementById('prod_title').value = decodeURI(title).replace(/\+/g, ' ');
	parent.document.getElementById('prod_code').value = decodeURI(productcode).replace(/\+/g, ' ');
	parent.document.getElementById('prod_edition_size').value = decodeURI(edition).replace(/\+/g, ' ');
	parent.document.getElementById('prod_dimensions').value = decodeURI(size).replace(/\+/g, ' ');
	parent.document.getElementById('prod_medium').value = decodeURI(medium).replace(/\+/g, ' ');
	parent.document.getElementById('prod_retail_price').value = decodeURI(price).replace(/\+/g, ' ');
	parent.document.getElementById('prod_quantity').value = decodeURI(quantity).replace(/\+/g, ' ');
	
	showHideDiv('product_add_enable');
	showHideDiv('search_enable');
	showHideDiv('artistList');
	checkBox('product_add_enable_checkbox', true);
	checkBox('search_enable_checkbox', false);
	window.location.hash="addproduct"
}

function checkBox (id, checked) {
	if (x = parent.document.getElementById(id)) {
		x.checked = checked;
	} else if (x = document.getElementById(id)) {
		x.checked = checked
	}
	return false;
}


function confirmation(alertMsg, cancelMsg) {
	var answer = confirm(alertMsg)
	if (answer){
		return true;
	}
	else{
		alert(cancelMsg)
		return false;
	}
}

function showHideDiv(id)
{
	el = document.getElementById(id);
	if (el.style.display == 'none')
	{
		el.style.display = '';
	} else {
		el.style.display = 'none';
	}
}

function viewImage(prod_id) {
	prd_parts = prod_id.split('_')
	if(prd_parts[0]=="d") {
		prd_parts[0]="products";
	} else {
		prd_parts[0]="crm_products";
	}
	window.open('/art-detail.asp?id='+prd_parts[1]+'&tab='+prd_parts[0], 'popup', 'height=600,width=410,toolbar=no,location=no,scrollbars=yes');
	return false;
}

function email_customers(elm) {
	done = false;
	inputs = document.getElementsByTagName("input")
	for(i=0; i<inputs.length; i++) {
		if(inputs[i].type=="checkbox" && inputs[i].checked && inputs[i].name=="customer_email[]" && inputs[i].value!="") {
			if(done!=false) {
				elm.href += ",";
			}
			elm.href += inputs[i].value;
			done = true;
		}
	}
}