// rebecca and penelope JavaScript functions
// 2005 06 14 DS
// global vars
//var i = 0
//images = new Array;

var zoom_count=0;

if(document.images){
	var up=new Image();
	up.src="images/t/b1.jpg";
	var ov=new Image();
	ov.src="images/t/b1_0.jpg";
}
function ov_button(){
	if(document.images){
		document["b0"].src=ov.src;
		document["b0"].border=1;
		//document["buttonOne"].border.borderColor=ov.bordercolor;
	}
}
function up_button(){
	if(document.images){
		document["b0"].src=up.src;
		document["b0"].border=1;
		//document["buttonOne"].border.borderColor=up.bordercolor;
	}
}

function sameimg(n,cat,prod)
{
	document.getElementById("main").src = images[n];
	document.getElementById("popup1").href = zoom_images[n];
	document.getElementById("popup2").href = zoom_images[n];
	document.getElementById("email-friend").href = "email-friend.php?KeepThis=true&cat="+cat+"&prod="+prod+"&index="+n+"&TB_iframe=true&height=380&width=395";
	//alert (string);
}

function showrollover(i){
	//document.write(roll_images[i]);
	//document.write(imagename[i]);
	/* document[imagenames[i]].src=roll_images[i];
	document[imagenames[i]].border=1; */
	//document[imagenames[i]].border.borderColor="#ffee11";
}

function hiderollover(i){
	//document.write(roll_images[i]);
	//document.write(imagename[i]);
	/* document[imagenames[i]].src=origimages[i]; */
}


function write_text(n){
	document.write(text[n]);
}


function createZoomLayer(i){
	var current = document.getElementById("zoomLayerContent");
	var parent = current.parentNode;
	var c = document.createElement("div");
	var before="<table><tr><td><img src=";
	var after="></td></tr></table>\n";
	
	
	
	var string=before+roll_images[i]+after;

	//alert(string);

	parent.replaceChild(c, current);
	c.setAttribute("id","zoomLayerContent");
	c.innerHTML=string;	

	return true;
}

function createlayer(i){
	var current = document.getElementById("layerContent");
	var parent = current.parentNode;
	var notify_url = "<input type=\"hidden\" name=\"notify_url\" value=\"http://www.rebeccaandpenelope.com/pp_php_ipn.php\" />";
	notify_url = "";
	var c = document.createElement("div");
	//var formopen="<form name=\"respForm\" method=\"POST\" action=\"rp_receive_form_data_test.php\">";
	//var formclose=" <input type=\"submit\" name=\"submit\" value=\"buy now\"></input> </form>";
	var formopen="<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">";

	var formclose_NOTIFY_URL="<p><input type=\"image\" src=\"images/products/buttons/Buy_Now_00.gif\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\" /><input type=\"hidden\" name=\"add\" value=\"1\" /><input type=\"hidden\" name=\"cmd\" value=\"_cart\" /><input type=\"hidden\" name=\"business\" value=\"RebeccaPenelope@aol.com\" /><input type=\"hidden\" name=\"no_note\" value=\"1\" /></p>"+notify_url+"</form>";
	var formclose="<p><input type=\"image\" src=\"images/products/buttons/Buy_Now_00.gif\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\" /><input type=\"hidden\" name=\"add\" value=\"1\"><input type=\"hidden\" name=\"cmd\" value=\"_cart\" /><input type=\"hidden\" name=\"business\" value=\"RebeccaPenelope@aol.com\" /><input type=\"hidden\" name=\"no_note\" value=\"1\" /></p></form>";

	//var formAdd_to_Basket="</td><td><input type=\"image\" src=\"images/buttons/Add_to_Basket_00.gif\" border=\"0\" name=\"submit\" alt=\"Payments with PayPal - fast, free and secure\"> <input type=\"hidden\" name=\"add\" value=\"0\"> <input type=\"hidden\" name=\"cmd\" value=\"_cart\"> <input type=\"hidden\" name=\"business\" value=\"RebeccaPenelope@aol.com\"> <input type=\"hidden\" name=\"no_note\" value=\"1\"></input></form></td></tr></table>";

	var formView_Basket="<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\"><p><input type=\"image\" src=\"images/products/buttons/View_Basket_00.gif\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\" /><input type=\"hidden\" name=\"cmd\" value=\"_cart\" /><input type=\"hidden\" name=\"business\" value=\"RebeccaPenelope@aol.com\" /><input type=\"hidden\" name=\"display\" value=\"1\" /></p></form>";

	//var cclogos=" <table> <tr> <td> <img src=\"images/logo_ccSolo.gif\"> </td> <td> <img src=\"images/logo_ccSwitch.gif\"> </td> <td> <img src=\"images/logo_ccElectron.gif\"> </td> <td> <img src=\"images/logo_ccVisa.gif\"> </td> <td> <img src=\"images/logo_ccMC.gif\"> </td> <td> <img src=\"images/logo_ccAmex.gif\"> </td> <td> <img src=\"images/logo_ccDelta.gif\"> </td> </tr> </table>";	
	
//	var string=formopen+copy[i]+formclose;
//	var string=formopen+copy[i]+formclose+formAdd_to_Basket;
//	var string=formopen+copy[i]+formclose+formView_Basket;
//	var string=formopen+copy[i]+formclose+formView_Basket+formAdd_to_Basket;
//	var string=formopen+copy[i]+"<br/><table><tr><td>"+formclose+"</td><td>"+formEmail_a_Friend+"</td><td>"+formView_Basket+"</td><td>"+formAdd_to_Basket+"</td><tr></table>"+cclogos;
	var string=copy[i]; //i is passed in from products.php, copy is the JS array in products.php
	parent.replaceChild(c, current);
	c.setAttribute("id","layerContent");
	c.innerHTML=string;	

	return true;
}

function flip_border_color(i){
        
	//td.border-top.color="#fdde08";	

	return true;
}

function isblank(s){
	for(var i=0;i<s.length;i++){
		var c = s.charAt(i);
		if((c!= ' ')&&(c != '\n')&&(c != '')){
			return false;
		}
	}
	return true;
}

function verify(f){
	var msg;
	var empty_fields="";
	var errors = "";

	for(var i=0;i<f.length;i++){
		var e=f.elements[i];
		if((    (e.type == "text") || 
			(e.type == "textarea") || 
			(e.type == "select-one") || 
			(e.type == "radio") || 
			(e.type == "checkbox")) && 
			(!e.optional)){
			if((e.value == null) || (e.value == "") || isblank(e.value)){
				empty_fields += "\n            " + e.id;
				continue;
			}
			if(e.numeric || (e.min != null) || (e.max != null)){
				var v = parseFloat(e.value);
				if(isNaN(v) ||
					((e.length < 5)))
			//		((e.min != null) && (v < e.min)) ||
		//			((e.max != null) && (v < e.max))) 
				{
					errors += " length of ZIP is less than 5 digits\n";
						errors += "- The field " + e.name + " must be a number\n";
					if(e.min != null){
						errors += " greater than " + e.min;
						errors += ".\n";
					}
					if(e.max != null){
						errors += " that is less than " + e.max;
						errors += ".\n";
					}else if (e.max != null){
						errors += ".\n";
					}
				}
			}
		}
	}
	var x = document.getElementById("email");
	if(x.value != ""){
		var r = new RegExp("[a-zA-Z0-9]+@[a-zA-Z0-9]+\.{1}[a-zA-Z0-9]+","gi");
		if (!r.exec(x.value)){
			errors += "- email address \" " + x.value + " \" is invalid \n";
		}
	}	

	if(!empty_fields && !errors){ 
		return true;
	}

	msg =  "________________________________________________________    \n\n";
	msg += "Some required fields were left blank empty.\n";
	msg += "Thank you for taking the time to complete them.\n";
	msg += "________________________________________________________    \n\n";

	if (empty_fields){
		msg += "We need the following details:" + empty_fields + "\n";
		if(errors){
			msg += "\n";
		}
	msg += errors;
	alert(msg);
	return false;
	}
}

function update_cookie(curr){

	var anticurr = "   ";

	if(curr == "USD"){anticurr = "GBP";
				currname="US Dollars";
				anticurrname="British Pounds Sterling";}
	else if(curr == "GBP"){anticurr = "USD";
				anticurrname="US Dollars";
				currname="British Pounds Sterling";}

	var warning="Please note:\n\nYou have requested to change your shopping currency from "+anticurrname+" ("+anticurr+") to "+currname+" ("+curr+").\n\nIf you have already made a purchase in "+anticurrname+" you will not be able to add further purchases to your shopping basket in "+currname+". This is because we can only accept payments in either Dollars or Pounds in one transaction.\n\nPlease click 'OK' to change currency to "+currname+" or 'Cancel' to stay in "+anticurrname+".\n\nThank You!\nRebecca and Penelope\n";

	var allcookies=document.cookie;

	//alert(allcookies);

	var pos = allcookies.indexOf(curr);

	//alert(pos);
	if(pos == -1){
		//if(confirm (warning)){
			var posx = allcookies.indexOf(anticurr);
			var start = posx;	
			var end = allcookies.indexOf(";", start);
			if(end == -1){end = allcookies.length;}
	
			valueNew = curr;
			var expdate = new Date ();
	
			expdate.setTime (expdate.getTime() + (60 * 60 * 24 * 10 * 1000)); 
	
			//alert (expdate);
			setCookieValue("RebeccaAndPenelope", valueNew, expdate);
	
			//alert("currency changed to "+valueNew+" ");

			document.location=document.location;
		//}
	}else if(pos != -1){
		var start = pos;
		var end = allcookies.indexOf(";", start);
		if(end == -1){end = allcookies.length;}

		var value = allcookies.substring(start, end);

		value = unescape(value);
		
		//alert (value);
		if(value == curr){
		//alert("unchanged");
		document.location=document.location;
		}
	}

	return true;
}

function setCookieValue(name, value, expires, path, domain, secure) {
	// Some characters - including spaces - are not allowed in cookies
	// so we escape to change the value we have entered into
	// a form acceptable to the cookie.

	var thisCookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	document.cookie = thisCookie;
}


// cookie object constructor:
function Cookie(document, name, hours, path, domain, secure){

	this.$document = document;
	this.$name = name;
	if(hours){
		this.$expiration = new Date((new Date()).getTime()+hours*3600000);
	}	
	else{
		this.$expiration = null;
	}
	if(path){ this.$path = path; }else{ this.$path = null; }
	if(domain){ this.$domain = domain; }else{ this.$domain = null; }
	if(secure){ this.$secure = secure; }else{ this.$secure = null; }
}

// build the cookie string
Cookie.prototype.store = function(){

	var cookieval = "";
	for(var prop in this){
		if((prop.charAt(0) == '$')||((typeof this[prop]) == 'function')){
			continue;
		}
		if(cookieval != ""){
			cookieval += '&';
		}
		cookieval += prop + ':' + escape(this[prop]);
	}
	
	var cookie = this.$name + '=' + cookieval;
	if(this.$expiration){ cookie += '; expires=' + this.$expiration.toGMTString(); }
	if(this.$path){ cookie += '; path=' + this.$path; }
	if(this.$domain){ cookie += '; domain=' + this.$domain; }
	if(this.$secure){ cookie += '; secure=' + this.$secure; }

	this.$document.cookie = cookie;	
}

Cookie.prototype.load = function(){
	var allcookies = this.$document.cookie;
	if(allcookies == "") return false;

	var start = allcookies.indexOf(this.$name + '=');

	if(start == -1) return false;

	start += this.$name.length +1;
	
	var end = allcookies.indexOf(';', start);

	if(end == -1) end = allcookies.length;

	var cookieval = allcookies.substring(start, end);


	var a = cookieval.split('&');
	for(var i=0;i<a.length;i++){
		a[i] = a[i].split(':');
	}

	for(var i=0; i , a.length;i++){
		this[a[i][0]] = unescape(a[i][1]);

	}

	return true;
}

Cookie.prototype.remove = function(){
	var cookie;
	cookie = this.$name + '=';

	if(this.$path){ cookie += '; path=' + this.$path; }
	if(this.$domain){ cookie += '; domain=' + this.$domain; }
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00: GMT';

	this.$document.cookie = cookie;
}

function pop_zoom(section,page,number)
{
	//newwindow=window.open(url,'rp_zoom','height=480,width=480,menubar=yes,scrollbars=yes');
	//document.write("<a href=\"javascript:window.close()\">Close</a> this popup");

	var i = zoom_count;
	var image=zoom_images[i];
	var rnd=Math.round(1000*Math.random());
	var namex='zoom'+rnd;
	var topline="<h4>Rebecca and Penelope "+section+" </h4>\n";
  	var generator=window.open('',namex,'height=800,width=700,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,status=yes');

	if (window.focus) {generator.focus();}
  
  	//generator.document.write('<html>\n<head>\n<title>Rebecca and Penelope Zoom Image</title>\n');
  	generator.document.write('<html>\n<head>\n<title> </title>\n');
	generator.document.write('<link rel="stylesheet" href="styles/rp.css">\n');
	generator.document.write('</head>\n<body>\n');
	//generator.document.write('<a href="');
	//generator.document.write( page );
	//generator.document.write('">');

	//generator.document.write('<div class=\"boxOuterZoom\"/>\n');
	//generator.document.write("<h4>Rebecca &amp; Penelope - ");
	//generator.document.write("</h4>\n");
	generator.document.write("<img src=\"images/products/logos/logo_popup.gif\">\n");
	
	//generator.document.write("<h4>");
	//generator.document.write( section );
	//generator.document.write("</h4>\n");
	//generator.document.write('</a>');
	generator.document.write('<p><a href="javascript:self.close()">CLOSE WINDOW</a></p>\n');
	//generator.document.write('<hr class=\"pink\"/>\n');
	//generator.document.write('<div class=\"boxZoom\"/>\n');
	generator.document.write('<img src=\"');
	generator.document.write( image );
	generator.document.write('"/>\n');
	//generator.document.write('\" height=\"400\" width=\"400\">\n');
	//generator.document.write('</div>\n');
	//generator.document.write(zoom_text[i]);

	//generator.document.write("<h4>");
	//generator.document.write( rnd );
	//generator.document.write("</h4>\n");

	//generator.document.write('</div>\n');

	generator.document.write('</body>\n</html>\n');
	generator.document.close();
}


function zoom_image(i){
	
	//var target=image+".php";
	var target="";
	var width="width="+width+"";
	var height="height="+height+"";
	var title="image zoom";
	
	open(target, width, height, status="no", resizable="yes");

	var args=target+" "+width+" "+title+" "+height+" ";

	alert (args);

}

function preloadImages() {
	var d=document; 

	if(d.images){ 
		if(!d.p){ 
			d.p=new Array();
		}
    
		var i,j=d.p.length,a=preloadImages.arguments; 

		for(i=0; i<a.length; i++){
			if (a[i].indexOf("#")!=0){ 
				d.p[j]=new Image; 
				d.p[j++].src=a[i];
			}
		}
	}
}
