var site_url = "http://www.expressionsart.com/";

function changeSrc(url,imgsrc,imgtitle,imgsize,issold){
	document.getElementById('itemDetails').innerHTML = imgtitle+'<br />'+imgsize;
	document.getElementById('itemId').value = url;
	document.getElementById('itemTitle').value = imgtitle;
	document.getElementById('artistImage').style.backgroundImage = "url('http://www.expressionsart.com/_media/userfiles/items/418/"+imgsrc+"')";
	if(issold){
		obj('divSold').style.display='';
	}else{
		obj('divSold').style.display='none';
	}
}

function newsletter_sent(data){
	document.getElementById('mailingTbl').style.display='';
	document.getElementById('mailingReport').innerHTML = data;	
}

function obj(id,type,parent){
	switch(type){
		case "tag":
			if(parent.getElementsByTagName(id)){
				return parent.getElementsByTagName(id);
			}
			break;
		case "name":
			if(document.getElementsByName(id)){
				return document.getElementsByName(id)[0];
			}
			break;
		case "id":
		default:
			if(document.getElementById(id)){
				return document.getElementById(id);
			}
	}
}

function ChkStrBy(sText, sValidChars){
	var ret = true;
	if (sText.length==0) return (false);
	for (i = 0 ; i < (sText.length) && (ret==true) ; i++){ 
		if (sValidChars.indexOf(sText.charAt(i)) == -1){
			ret = false;
		}
	}
	return (ret);
}

function IsEmail(sText) {
	var at="@"
	var dot="."
	var lat=sText.indexOf(at)
	var lstr=sText.length
	var ldot=sText.indexOf(dot)
	if (sText=="" || sText==null){ return false }
	if (sText.indexOf(at)==-1 || sText.indexOf(at)==0 || sText.indexOf(at)==lstr){ return false }
	if (sText.indexOf(dot)==-1 || sText.indexOf(dot)==0 || sText.indexOf(dot)==lstr){ return false }
	if (sText.indexOf(at,(lat+1))!=-1){ return false }
	if (sText.substring(lat-1,lat)==dot || sText.substring(lat+1,lat+2)==dot){ return false }
	if (sText.indexOf(dot,(lat+2))==-1){ return false }
	if (sText.indexOf(" ")!=-1){ return false }
 	return true					
}

function IsPhone(sText){
	return (ChkStrBy(sText,"0123456789-"));
}

function purchaseItem(id){
	var msg = "";
	var nameTxt = document.frmPurchase.nameTxt.value;
	var emailTxt = document.frmPurchase.emailTxt.value;
	var phoneTxt = document.frmPurchase.phoneTxt.value;
	var contentTxt = document.frmPurchase.contentTxt.value;
	if(emailTxt == "" && phoneTxt == "") msg = "Please insert email and/or phone";
	else {		
		if (IsEmail(emailTxt) && IsPhone(phoneTxt)) msg = "";
		else {
			if (!IsEmail(emailTxt)) msg += "Email not valid";	
			if (!IsPhone(phoneTxt)) msg += "<br>Phone not valid";	
		}
	}
	if (IsEmail(emailTxt) || IsPhone(phoneTxt)) msg = "";
	if (msg == "") {
		doAjax('purchase.ajax.php',purchase_sent	, "&nameTxt="+nameTxt+"&emailTxt="+emailTxt+"&phoneTxt="+phoneTxt+"&contentTxt="+contentTxt+"&itemid="+id);		
	} else {
		document.getElementById('errMsg').innerHTML = msg;	
		document.frmPurchase.emailTxt.focus();
	
	}
}

function purchase_sent(data){
	document.getElementById('purchaseTbl').style.display='';
	document.getElementById('errMsg').innerHTML = data;	
}

function getCenterizedScreen(elemWidth, elemHieght){
	var centerX=(screen.width/2-elemWidth/2);
	var centerY=(screen.height/2-elemHieght/2)-80;
	return {'x': centerX, 'y': centerY};
}

var dlgWin='';
function newWindow(mypage,myname,w,h,scroll){
	if (typeof scroll == 'undefined') scroll='auto';
	var POS=getCenterizedScreen(w,h);
	settings='width='+w+',height='+h+',top='+POS.y+',left='+POS.x+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	if (dlgWin){ dlgWin.close(); }
	dlgWin=window.open(mypage,myname,settings);
}

function pngFix(){
	navinfo = navigator.userAgent;
	if(navinfo.indexOf("MSIE 6.0")!=(-1)){
		if (document.all && document.body.filters){
			tofix = document.getElementsByTagName('img');
			for(var i=0; i<tofix.length; i++){
				var img = tofix[i];
				var imgName = img.src.toUpperCase()
				if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
					var imgID = (img.id) ? "id='" + img.id + "' " : "";
					var imgClass = (img.className) ? "class='" + img.className + "' " : "";
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
					var imgStyle = "display:inline-block;" + img.style.cssText ;
					if (img.align == "left") imgStyle = "float:left;" + imgStyle;
					if (img.align == "right") imgStyle = "float:right;" + imgStyle;
					var imgClick = (img.onclick != null) ? "onclick=\"" + img.onclick + "; anonymous();\"" : "";
					if (img.parentElement.href) imgStyle = "cursor:pointer;" + imgStyle;
					var strNewHTML = "<span " + imgID + imgClass + imgTitle + imgClick 
					+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
					img.outerHTML = strNewHTML;
					i = i-1;
				};
			};
		};
	};
};

window.onload = function(){
	pngFix();
}