// cdsupport.js
var cDat;

function _Cookie_store()
{
	var cookieval = "";
    for(var prop in this) {
        // Ignore properties with names that begin with '$' and also methods.
        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.$document.cookie = cookie;
}

function _Cookie_load()
{
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;
    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;   // Cookie not defined for this page.
    start += this.$name.length + 1;  // Skip name and equals sign.
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    var a = cookieval.split('&');    // Break it into array of name/value pairs.
    for(var i=0; i < a.length; i++){a[i] = a[i].split(':');}  // Break each pair into an array.
    for(var i = 0; i < a.length; i++) { this[a[i][0]] = unescape(a[i][1]); }
    return true;
}

function _Cookie_remove()
{
    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 CDobj() {
this.$document = document;
this.$name = "cd_data";
var exp = new Date(); exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30 * 6 ));
this.$expiration = exp; // approx 6 months
this.$path = null; this.$domain = null; this.$secure = false;
}//

function cdval(tag){ ret= cDat[tag]? cDat[tag] : ""; return( ret ); }//
function cdtbxval(tag){ ret= cDat[tag]? unescape(cDat[tag]) : ""; return( ret ); }//
function cdseltxt(tag){ var val=cdval(tag); var bits=val.split("|"); return(bits[2]); }//
function cdselval(tag){ var val=cdval(tag); var bits=val.split("|"); return(bits[1]); }//
function cdchkval(tag){ var val=cdval(tag); var bits=val.split("|"); return(eval(bits[0])?bits[1]:""); }//

function store(el){
var sval;var delim="|";var escstr;
var tag=el.name;
window.status="storing : "+tag;
if(el.type=="text" || el.type=="radio"){ cDat[tag]=el.value; }
if(el.type=="textarea"){ escstr=escape(el.value); cDat[tag]=escstr; }
if(el.type=="checkbox"){ cDat[tag] = el.checked + delim + el.value;  }
if(el.type=="select-one")
	{
	sval=""+el.selectedIndex+delim;
	sval+=el.options[el.selectedIndex].value+delim;
	sval+=el.options[el.selectedIndex].text;
	cDat[tag]=sval;
	}
top.cDat.store();
return(true);
}//

function setUpcDatObject(){
new CDobj();
CDobj.prototype.store  = _Cookie_store;
CDobj.prototype.load   = _Cookie_load;
CDobj.prototype.remove = _Cookie_remove;
cDat=new CDobj();
cDat.load();
}//

function pullFromcDat(form){
var L = form.elements.length;
var el,val,bits;var delim="|";
for(var i=0; i<L; i++)
	{
    el = form.elements[i];
	if(el.name==""){continue;}
        if( cDat[el.name])
		{
		if(el.type=="text"){ form[el.name].value = cDat[el.name];  }
		if(el.type=="textarea"){ form[el.name].value = unescape(cDat[el.name]); }
		if(el.type=="checkbox") { val=cDat[el.name]; bits=val.split(delim); el.checked=eval(bits[0]); }
		if(el.type=="radio") { val=cDat[el.name]; el.checked=("___"+val)==("___"+el.value); }
		if(el.type=="select-one") { val=cDat[el.name]; bits=val.split(delim); el.selectedIndex=parseInt(""+bits[0]); }
		}
	}
}//

setUpcDatObject();

// money format for input

function moneyFormat(V){
var intPart,decPart
ret=V*100;ret=Math.round(ret);
ret=V<.1?"0"+ret:ret;
ret=V< 1?"0"+ret:""+ret;
intPart=ret.substring(0,ret.length-2);
decPart=ret.substring(ret.length-2);
ret=intPart+"."+decPart;
return(ret);
}

function dec2format(el){
el.value=moneyFormat(decimals(el.value));
}

function decimals(str){ return(clean(str,"1234567890.")) }

function clean(str,permitted){
str=""+str;
var nchar,n;var out="";
for(n=0;n<str.length;n++)
	{
	nchar=str.charAt(n);
	out+=permitted.indexOf(nchar)>-1?nchar:"";
	}
return(out);
}

// verification

function verify(form){
var ch = form.cardholder.value;
var co = form.company.value;
var tf = form.transaction_ref.value;
var pa = form.payment_amount.value;
var msg=""; var OK=true;

msg+= (ch.length<2)? "\t-\tthe name of the card holder\n"  : "";
msg+= (co.length<2)? "\t-\tthe company name\n"  : "";
msg+= (tf.length<2)? "\t-\ta reference for this order/transaction\n"  : "";
var pamsg="\t-\tthe amount payable in GBP (British Pounds Sterling)\n"
msg+= (pa.charAt(0)=="£") || (pa.indexOf(".")==-1) || pa=="0.00" ? pamsg  : "";

if(msg.length>0){OK=false;}
if(!OK){alert("Please fill in : \n\n"+msg);}

return(OK);
}
