var zoom_count=0;

var xhr;

var persistentlimitvalue=6;
var persistentdependson=0;
var persistentexternalid='';
var alerts=false;

function updateSelection(textstring){

	var f=$('portfolio');
	var p=$('portfolio+project');
	var w=$('portfolio+project+workpackage');
	var df=$('dependsonfid');
	var dp=$('dependsonfid+dependsonpid');
	var dw=$('dependsonfid+dependsonpid+dependsonwid');
	var limit=$('limit');
	var csv=$('csv');
	var ie=$('internal_external');
	var modnum=0;
	var modnum1=0;
	var a=12;
	var b=14;
	var myFunction=function(){
		return a+b;
	}

	if(textstring=="portfolio"){
		p.value='';
		w.value='';
	}else if(textstring=="portfolio+project"){
		f.value='';
		w.value='';
	}else if(textstring=="portfolio+project+workpackage"){
		f.value='';
		p.value='';
	}else if(textstring=="dependsonfid"){
		if(ie.value=='E'){ ie.value=''; }
		dp.value='';
		dw.value='';
	}else if(textstring=="dependsonfid+dependsonpid"){
		if(ie.value=='E'){ ie.value=''; }
		df.value='';
		dw.value='';
	}else if(textstring=="dependsonfid+dependsonpid+dependsonwid"){
		if(ie.value=='E'){ ie.value=''; }
		df.value='';
		dp.value='';
	}else if(textstring=="csv"){
		if(csv.checked){
			limit.value='';
		}else{
			limit.vlue=persistentlimitvalue;
		}
	}else if(textstring=="limit"){
		persistentlimitvalue=limit.value;
		csv.checked=false;
	}else if(textstring=='internal_external'){
		if(ie.value=='E'){
			df.value='';   //dependsonfid+dependsonpid+dependsonwid
			dp.value='';   //dependsonfid+dependsonpid+dependsonwid
			dw.value='';   //dependsonfid+dependsonpid+dependsonwid
		}
	}
	
	//alert("Duncan Limit: " + textstring +" limitOn:[" + limitOn + "] value:[" + limit.value + "] modnum:[" + modnum + "]" );
	//alert("Duncan CSV: " + textstring +" csv.value:[" + csv.value + "] modnum1:[" + modnum1 + "]" );
	if(alerts){
		alert("Duncan IE: " + textstring +" ie.value:[" + ie.value + "] dw.value:[" + dw.value + "]");
	}	

	if(alerts){
		var c=myFunction();
		alert("DUNCAN TEST: c:[" + c + "]" );
		a=192;
		b=123;
		var d=myFunction();
		alert("DUNCAN TEST: d:[" + d + "]" );



	}	
}



function updateInputSelection(textstring){
	var df=$('dependson');
	var ie=$('internal_external');
	var eid=$('externalid');
	var extdep=158;

	if(persistentdependson==extdep){
		persistentdependson=0;
	}
	
	if(textstring=="dependson"){
		if(alerts){
			alert("updateInputSelection:[" + textstring + "] value:["+df.value+"] pdo:"+persistentdependson+"]" );
		}
		persistentdependson=df.value;
		if(df.value==extdep){
			ie.value='E';
		}else{
			ie.value='I';
			eid.value='';
		}
	}else if(textstring=='internal_external'){
		if(alerts){
			alert("updateInputSelection:[" + textstring + "] value:["+ie.value+"] pie:["+persistentexternalid+"]");
		}
		if(ie.value=='I'){
			eid.value='';	
			df.value=persistentdependson;
		}else if(ie.value=='E'){
			eid.value=persistentexternalid;	
			df.value=extdep;
		}else if(ie.value==''){
			eid.value='';
			df.value=persistentdependson;
		}
	}else if(textstring=='externalid'){
		persistentexternalid=eid.value;
		if(alerts){
			alert("updateInputSelection:[" + textstring + "] value:["+eid.value+"]");
		}
		if(eid.value!=''){
			ie.value='E';
			df.value=extdep;
		}else{
			ie.value='I';
			df.value=persistentdependson;
		}
	}else{
		persistentdependson=0;
	}
}

function getData(key){
	xhr=new XMLHttpRequest();
	xhr.open("GET","/getDataFromPortfolio.request?" + key);
	xhr.send(null);
}


function sameimg(n){
	//n--;
       	var one="'javascript:pop_zoom(";
	var two=n;
	var three="); return false;'";
	var string=one+two+three;

	zoom_count=n;
	//document.image2.onClick = one+two+three;
	
	//alert (string);	
	document.swapImage.src = swap[n];

	//alert (" "+swap[n]+" "zoom[n]+" ");
}

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;
	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 string=itemtext[i];

	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.name;
				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("useremail");
	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 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+" ");

			window.location.reload(true);
		}
	}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")
			;
		}
	}
	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_2(section,size,index)
{
	//alert(zoom[index]);

	var image=zoom[index];
	var rnd=Math.round(1000*Math.random());
	var height=size+=80;
	var width=size+=80;
	var namex='zoom'+rnd;
	var topline="<h4>Sharon Hall "+section+" </h4>\n";
	var left=rnd/10;
	var top=rnd/10;
  	var generator=window.open('',namex,'height='+height+',width='+width+',left='+left+',top='+top+',resizable=yes,scrollbars=no,toolbar=no,status=yes');
	if (window.focus) {generator.focus();}
  
  	generator.document.write('<html>\n<head>\n<title> </title>\n');
	generator.document.write('<link rel="stylesheet" href="css/sh.css">\n');
	generator.document.write('</head>\n<body>\n');
	//generator.document.write( topline );
	
	generator.document.write('<p>');
	//generator.document.write(namex);
	generator.document.write('</p>');
	generator.document.write('<img src=\"');
	generator.document.write( image );
	generator.document.write('"/>\n');
	generator.document.write('<p><a href="javascript:self.close()">&nbsp;<img src="images/close_window.gif" border="0"></a></p>\n');
	generator.document.write('</body>\n</html>\n');
	generator.document.close();


}

function pop_zoom(section,size)
{
	var i=zoom_count;
	var image=zoom[i];
	var rnd=Math.round(1000*Math.random());
	var height=size+=80;
	var width=size+=80;
	var namex='zoom'+rnd;
	var topline="<h4>Sharon Hall "+section+" </h4>\n";
	var left=rnd/10;
	var top=rnd/10;
  	var generator=window.open('',namex,'height='+height+',width='+width+',left='+left+',top='+top+',resizable=yes,scrollbars=no,toolbar=no,status=yes');
	if (window.focus) {generator.focus();}
  
  	generator.document.write('<html>\n<head>\n<title> </title>\n');
	generator.document.write('<link rel="stylesheet" href="css/sh.css">\n');
	generator.document.write('</head>\n<body>\n');
	//generator.document.write( topline );
	
	generator.document.write('<p>');
	//generator.document.write(namex);
	generator.document.write('</p>');
	generator.document.write('<img src=\"');
	generator.document.write( image );
	generator.document.write('"/>\n');
	generator.document.write('<p><a href="javascript:self.close()">&nbsp;<img src="images/close_window.gif" border="0"></a></p>\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];
			}
		}
	}
}

function logout_reload_page(){
	
	alert("HELLO");

	window.location.reload(true);
}



/*
Text Link/Image Map Tooltip Script- 
© Dynamic Drive (www.dynamicdrive.com)
For full source code, and 100's more DHTML scripts
Visit http://www.dynamicdrive.com
*/

if (!document.layers&&!document.all&&!document.getElementById)
event="test"
function showtip(current,e,text){

if (document.all||document.getElementById){
thetitle=text.split('<br>')
if (thetitle.length>1){
thetitles=''
for (i=0;i<thetitle.length;i++)
thetitles+=thetitle[i]
current.title=thetitles
}
else
current.title=text
}

else if (document.layers){
document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>')
document.tooltip.document.close()
document.tooltip.left=e.pageX+5
document.tooltip.top=e.pageY+5
document.tooltip.visibility="show"
}
}
function hidetip(){
if (document.layers)
document.tooltip.visibility="hidden"
}



