// Written by Duncan Gray
// Contains many functions and AJAX code for use on our web-sites
// Copywrite Ultrabizz 2002 onwards


var privacy='<p class=lightsmall>PRIVACY STATEMENT: The email addresses you enter will not be sold, given away etc to anyone.  We do log the email addresses in case of spam abuse so that we can track down the guilty parties.  From time to time (many weeks usually) we may use these logged addresses to send out a mailing to advice all our customers and visitors of new product lines or other important changes which would be of interest to our clientele.';

function restoreMem(pid)
{
  var mem2ID = this['mem2'+pid];
  var divID = 'rside'+pid;
	document.getElementById(divID).innerHTML=mem2ID;
}
function alertme(pid)
{
  var memID = this['mem'+pid];
  var divID = 'rside'+pid;
	var explainID = 'explain'+pid;
	var privID='priv'+pid;
	var FSid='FSec'+pid;
	this['mem2'+pid] = document.getElementById(divID).innerHTML;
	document.getElementById(divID).innerHTML = '<center><h3><u>Product Alert</u></h3></center>'+memID;
	document.getElementById(explainID).innerHTML = 'Enter your name and email address and we will send you an email whenever this product becomes a front page special';
	document.getElementById(privID).innerHTML = privacy;
	document.getElementById(FSid).style.visibility = 'hidden';
	document.getElementById('s2fclick'+pid).style.display = 'none';

}
function alertmeB(pid,cid,pc)
{		
  var image=0;
  var divID = 'rside'+pid;
  var memID = this['mem'+pid];
	var explainID = 'explain'+pid;
	var privID='priv'+pid;
	var FSid='FSec'+pid;
  for(loop=0;loop<itemCounter;loop++)
  {
  	  if(pageArray1[loop]==pid)
  		  {image=pageArray2[loop];break;}
  }
	var sender = document.getElementById('sender'+pid).value;
	var sender_email = document.getElementById('sender_email'+pid).value;
  var url = 'productAlert.php?pID='+pid+'&pic='+image+'&cID='+cid+'&sender='+sender+'&sender_email='+sender_email+'&alt=1';
	var xmlHttp = new XMLHttpRequest;
  xmlHttp.open("GET",url,true);
  xmlHttp.onreadystatechange=function() 
  { 
     if (this.readyState == 4) 
  	 { 
  	    xmlHttp.onreadystatechange=null;
    	  var reply=xmlHttp.responseText;
    		if(reply==1)
				{
				  document.getElementById(divID).innerHTML='<font color=blue>An email was sent to your email address to verify your desire to receive alerts for this product. <p>You HAVE to click the confirmation link in that email or no alerts will be sent.</font>';
					setTimeout(function(){document.getElementById(divID).innerHTML=this['mem2'+pid];}, 10000);
				}
				else
    		if(reply==2)
				{
				  document.getElementById(divID).innerHTML='<font color=blue>Your email address is already on the alert list for this item.</font>';
					setTimeout(function(){document.getElementById(divID).innerHTML=this['mem2'+pid];}, 10000);
				}
				else
    		if(reply==3)
				{
				  document.getElementById(divID).innerHTML='<font color=blue>Your email address is already on the alert list for this item but you never confirmed it.<p>A new confirmation email has been sent to you.</font>';
					setTimeout(function(){document.getElementById(divID).innerHTML=this['mem2'+pid];}, 10000);
				}
				else
				if(reply==0)
				{
  				var message = '<center><font color=red>BAD EMAIL ADDRESS.<br>Please try again</font></center><br>'+memID;
          document.getElementById(divID).innerHTML=message;
        	document.getElementById(explainID).innerHTML = 'Enter your name and email address and we will send you an email whenever this product become a front page special';
        	document.getElementById(privID).innerHTML = privacy;
        	document.getElementById(FSid).style.visibility = 'hidden';
        	document.getElementById('s2fclick'+pid).style.display = 'none';
				}
				else
				{
  				var message = '<center><font color=red>An Error Occured<br>Please try again or contact us</font></center><br>';
          document.getElementById(divID).innerHTML=message+memID;        	
					document.getElementById(explainID).innerHTML = 'Enter your name and email address and we will send you an email whenever this product become a front page special';
        	document.getElementById(privID).innerHTML = privacy;
        	document.getElementById(FSid).style.visibility = 'hidden';
        	document.getElementById('s2fclick'+pid).style.display = 'none';
				}
  	 } 
  } 
  xmlHttp.send(null);
}


function s2f(pid)
{
  var memID = this['mem'+pid];
  var divID = 'rside'+pid;
	var explainID = 'explain'+pid;
	var privID='priv'+pid;
	var FSid='FSec'+pid;
	this['mem2'+pid] = document.getElementById(divID).innerHTML;
	document.getElementById(divID).innerHTML = '<center><h3><u>Send to Friend</u></h3></center>'+memID;
	document.getElementById(explainID).innerHTML = 'Enter your details and your friends details below and we will send them a picture-link to this product.';
	document.getElementById(privID).innerHTML = privacy;
	document.getElementById(FSid).style.visibility = 'visible';
	document.getElementById('alertclick'+pid).style.display = 'none';

}
function s2fB(pid,cid,pc)
{		
  var image=0;
  var divID = 'rside'+pid;
  var memID = this['mem'+pid];
	var explainID = 'explain'+pid;
	var privID='priv'+pid;
	var FSid='FSec'+pid;
  for(loop=0;loop<itemCounter;loop++)
  {
  	  if(pageArray1[loop]==pid)
  		  {image=pageArray2[loop];break;}
  }
	var sender = document.getElementById('sender'+pid).value;
	var sender_email = document.getElementById('sender_email'+pid).value;
	var friend = document.getElementById('friend'+pid).value;
	var friend_email = document.getElementById('friend_email'+pid).value;
  var url = 's2f.php?sendtofriend_result=1&pID='+pid+'&pic='+image+'&cID='+cid+'&sender='+sender+'&sender_email='+sender_email+'&friend='+friend+'&friend_email='+friend_email;
	var xmlHttp = new XMLHttpRequest;
  xmlHttp.open("GET",url,true);
  xmlHttp.onreadystatechange=function() 
  { 
     if (this.readyState == 4) 
  	 { 
  	    xmlHttp.onreadystatechange=null;
    	  var reply=xmlHttp.responseText;
    		if(reply==1)
				{
				  document.getElementById(divID).innerHTML='<center><h3>The email was sent successfully</h3></center>';
					setTimeout(function(){document.getElementById(divID).innerHTML=this['mem2'+pid];}, 10000);
				}
				else
				if(reply==0)
				{
  				var message = '<center><font color=red>BAD EMAIL ADDRESS.<br>Please try again</font></center><br>'+memID;
          document.getElementById(divID).innerHTML=message;
        	document.getElementById(explainID).innerHTML = 'Enter your details and your friends details below and we will send them a picture-link to this product.';
        	document.getElementById(privID).innerHTML = privacy;
        	document.getElementById(FSid).style.visibility = 'visible';
        	document.getElementById('alertclick'+pid).style.display = 'none';
				}
				else
				{
  				var message = '<center><font color=red>An Error Occured<br>Please try again or contact us</font></center><br>';
          document.getElementById(divID).innerHTML=message+memID;
        	document.getElementById(explainID).innerHTML = 'Enter your details and your friends details below and we will send them a picture-link to this product.';
        	document.getElementById(privID).innerHTML = privacy;
        	document.getElementById(FSid).style.visibility = 'visible';
        	document.getElementById('alertclick'+pid).style.display = 'none';
				}
  	 } 
  } 
  xmlHttp.send(null);
}


function CastVote(ido,productID)
{
  var mark = ido.selectedIndex;
  var url="vote.php";
  url=url+"?productID="+productID;
  url=url+"&mark="+mark;
	var xmlHttp = new XMLHttpRequest;
  xmlHttp.open("GET",url,true);
  xmlHttp.onreadystatechange=function() 
  { 
     if (this.readyState == 4) 
  	 { 
  	    xmlHttp.onreadystatechange=null;
    	  var reply=xmlHttp.responseText.split("|");
    		var divid=reply[0];
    		var vote=reply[1];
    		var numvotes=reply[2];
    		var mystring='';		
        for(i=0;i<5;i++)
          if (i < vote)mystring=mystring+"<img src=\"images/redstar_big.gif\">";
    	    else mystring=mystring+"<img src=\"images/blackstar_big.gif\">";
			
        var vs = numvotes>1?' votes':' vote';	 
        mystring += '<br>'+numvotes+vs;
      	document.getElementById(divid).innerHTML=mystring;
  	 } 
  } 
  xmlHttp.send(null);
}
function drawVoteResults(vote,numvotes)
{
 for(i=0;i<5;i++)
   if (i < vote)document.write("<img src=\"images/redstar_big.gif\">");
	 else document.write("<img src=\"images/blackstar_big.gif\">");
 var vs = numvotes>1?' votes':' vote';	 
 document.write('<br>',numvotes,vs);	 
}


function placeInCart(pid)
{
  var divID = 'rside'+pid;
	var cs1ID = 'cs1_'+pid;
	var cs2ID = 'cs2_'+pid;
	var cs3ID = 'cs3_'+pid;
	var cs1 = document.getElementById(cs1ID);
	var cs2 = document.getElementById(cs2ID);
	var cs3 = document.getElementById(cs3ID);
	var choices=0;
	if(cs1)if(cs1.selectedIndex>0)choices++;
	if(cs2)if(cs2.selectedIndex>0)choices++;
	if(cs3)if(cs3.selectedIndex>0)choices++;
	if(choices==0)
	{
    alert('Please select a color and size first.');
		return; 	
	}
	
	if(choices>1)
	{
    alert('Please select only ONE color and size source.');
		if(cs1)cs1.selectedIndex=0;
		if(cs2)cs2.selectedIndex=0;
		if(cs3)cs3.selectedIndex=0;
		return; 	
	}
	var d1,d2,d3;
	if(cs1)d1=cs1.selectedIndex; else d1=0;
	if(cs2)d2=cs2.selectedIndex; else d2=0;
	if(cs3)d3=cs3.selectedIndex; else d3=0;
	var url = 'add2cart.php?add2cart='+pid+'&color_sizeoption='+d1+'&color_size_auzoption='+d2+'&color_size_oursoption='+d3;
	var xmlHttp = new XMLHttpRequest;
  xmlHttp.open("GET",url,true);
  xmlHttp.onreadystatechange=function() 
  { 
     if (this.readyState == 4) 
  	 { 
  	    xmlHttp.onreadystatechange=null;
        document.getElementById('cartinfo').innerHTML=xmlHttp.responseText;
				var mem = document.getElementById(divID).innerHTML;
        document.getElementById(divID).innerHTML='<span style="color:green;font-size: 14px;font-weight:700;">This item is now in your cart.</span> <p>When you are ready to check-out you can view/edit the contents of your shopping cart by clicking the <p><a  class=cat href="index.php?shopping_cart=yes" onMouseOver="xpe(\'coo\',\'_checkout_\');" onMouseOut="xpe(\'con\',\'_checkout_\');" onMouseDown="xpe(\'coc\',\'_checkout_\');"><img id="id_checkout_co" src="images/btcout_y1.gif" name="vb_checkout_co" width="40" height="40" border=0 alt="View shopping cart and check-out"></a><p> button which can also be found on the top left section of this web-site.<p><p><b>This message will vanish in 15 seconds.</b>';
				setTimeout(function(){document.getElementById(divID).innerHTML=mem;}, 15000);
  	 } 
  } 
  xmlHttp.send(null);
}
function resetContents(divID,data)
{
 document.getElementById(divID).innerHTML=data;
}

// folowing for use with the buttons

vbo1m57n=xppr('o1m57_0.gif');
vbo1m57o=xppr('o1m57_1.gif');
vbo1m57c=xppr('o1m57_0.gif');
vbw1m57n=xppr('w1m57_0.gif');
vbw1m57o=xppr('w1m57_1.gif');
vbw1m57c=xppr('w1m57_2.gif');
vb41m57n=xppr('41m57_0.gif');
vb41m57o=xppr('41m57_1.gif');
vb41m57c=xppr('41m57_2.gif');

vbcsn=xppr('cs1.gif');
vbcso=xppr('cs2.gif');
vbcsc=xppr('cs2.gif');

vbptcn=xppr('ptc1.gif');
vbptco=xppr('ptc2.gif');
vbptcc=xppr('ptc2.gif');

vbpon=xppr('po1.gif');
vbpoo=xppr('po2.gif');
vbpoc=xppr('po2.gif');

vbndn=xppr('nd1.gif');
vbndo=xppr('nd2.gif');
vbndc=xppr('nd2.gif');

vbcon=xppr('cout_y1.gif');
vbcoo=xppr('cout_y2.gif');
vbcoc=xppr('cout_y2.gif');

vbsrchn=xppr('srch_y1.gif');
vbsrcho=xppr('srch_y2.gif');
vbsrchc=xppr('srch_y2.gif');

var btIdPref='vb';
function xppr(im)
{
var i=new Image();
i.src='images/bt'+im;
return i;
};
function xpe(id,name)
{
x=id.substring(0,id.length-1);
idn='id'+name+x;
if(document.getElementById(idn))document.getElementById(idn).src=eval(btIdPref+id+'.src');
return false;
};


function open_window(link,w,h) //opens new window
{
		var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
		newWin = window.open(link,'newWin',win);
		newWin.focus();
}

function validate_custinfo() //validate customer information
{
 if (document.contact_form.sender.value=="" )
 {
		alert("{/literal}{$smarty.const.ERROR_INPUT_NAME}{literal}");
		return false;
 }
		if (document.contact_form.sender_email.value=="" )
		{
			alert("{/literal}{$smarty.const.ERROR_INPUT_EMAIL}{literal}");
			return false;
		}
 }

	function validate_custinfo() //validate customer information
	{
		if (document.custinfo_form.first_name.value=="" || document.custinfo_form.last_name.value=="")
		{
			alert("Please enter both your first and family name.");
			return false;
		}

		if (document.custinfo_form.email.value=="")
		{
			alert("Please enter your email address.");
			return false;
		}

		if (document.custinfo_form.country.value=="")
		{
			alert("Please select a country.");
			return false;
		}

		if (document.custinfo_form.state.value=="")
		{
			alert("Please select a state or province.");
			return false;
		}

		if (document.custinfo_form.zip.value=="")
		{
			alert("Please enter a post code or zip");
			return false;
		}

		if (document.custinfo_form.city.value=="")
		{
			alert("Please enter your suburb or city.");
			return false;
		}

		if (document.custinfo_form.address.value=="")
		{
			alert("Please enter your address number and street or a P.O. Box");
			return false;
		}
		return true;
}
