//Copyright 2002-2004 Sandy Point Soft Furnishings.  All Rights Reserved.
function dupe(sw1,sw2)
{
	if (parseInt(sw1,10) == parseInt(sw2, 10) && parseInt(sw1, 10) > 0) {
		return true;
	}
	else {
		return false;
	}
}
//function notspec(n)
//{
//	if (isNAN(n)==true)
//		return true;
//	else if (parseInt(n)<=0)
//		return true;
//	else
//		return false;
//}
function dupes(sw1,sw2,sw3,sw4,sw5,sw6)
{
	if (dupe(sw1, sw2) || dupe(sw1, sw3) || dupe(sw1, sw4) || dupe(sw1, sw5) || dupe(sw1, sw6) || dupe(sw2, sw3) || dupe(sw2, sw4) || dupe(sw2, sw5) || dupe(sw2, sw6) || dupe(sw3, sw4) || dupe(sw3, sw5) || dupe(sw3, sw6) || dupe(sw4, sw5) || dupe(sw4, sw6) || dupe(sw5, sw6)) {
		return true;
	}
	else {
		return false;
	}
}
function allblank(sw1,sw2,sw3,sw4,sw5,sw6)
{
//	if (notspec(sw1)&&notspec(sw2)&&notspec(sw3)&&notspec(sw4)&&notspec(sw5)&&notspec(sw6))
//		return true;
//	else
		return false;
}

function swatchsubmit(form)
{
	if (allblank(form.M_swatch1.value,form.M_swatch2.value,form.M_swatch3.value,form.M_swatch4.value,form.M_swatch5.value,form.M_swatch6.value)===true)
	{
		alert("All your swatches are blank!  You must select a fabric.");
	}
	else if (form.M_deliverypostcode.value.length<3)
	{
		alert("You must enter a postal code");
	}
	else if (dupes(form.M_swatch1.value,form.M_swatch2.value,form.M_swatch3.value,form.M_swatch4.value,form.M_swatch5.value,form.M_swatch6.value))
	{
		alert("You have entered a fabric more than once");
	}
	else if (form.M_deliverycountry.value=='Select Country')
	{
		alert("You must select a delivery country");
	}
	else if (form.M_deliverycountry.value=='GB'&&parseInt(form.M_deliverypostcode.value,10)>0)
	{
		alert("Illegal post code (are you aware we cannot deliver to the USA?");
	}
	else if (form.email.value.length<3)
	{
		alert("You must enter an email address");
	}
	else
	{
		if (form.amount.value=="Free"||form.amount.value===0||form.email.value=="workroom@romansdirect.co.uk")
		{
			form.action="Swatches_on_their_way.php" ;
		}
		else
		{
			form.action="transferswatch.php" ;
		}
		form.submit();
    }
}
function popswatch(form,sw)
{
	switch(sw)
	{
		case 1:	popfab(parseInt(form.M_swatch1.value,10));break;
		case 2: popfab(parseInt(form.M_swatch2.value,10));break;
		case 3: popfab(parseInt(form.M_swatch3.value,10));break;
		case 4: popfab(parseInt(form.M_swatch4.value,10));break;
		case 5: popfab(parseInt(form.M_swatch5.value,10));break;
		case 6: popfab(parseInt(form.M_swatch6.value,10));break;
	}
}
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 = true;
	}
	else {
		this.$secure = false;
	}
    this.M_swatch1=0;
    this.M_swatch2=0;
    this.M_swatch3=0;
    this.M_swatch4=0;
    this.M_swatch5=0;
    this.M_swatch6=0;
    this.amount=0;
    this.M_deliverycountry="GB";
}
Cookie.prototype.clear = function (form) {
	form.swatchrange1.value=-1;
	form.swatchrange2.value=-1;
	form.swatchrange3.value=-1;
	form.swatchrange4.value=-1;
	form.swatchrange5.value=-1;
	form.swatchrange6.value=-1;
	form.M_swatch1.value=0;
	form.M_swatch2.value=0;
	form.M_swatch3.value=0;
	form.M_swatch4.value=0;
	form.M_swatch5.value=0;
	form.M_swatch6.value=0;
	//this.swatch1=0;
    //this.swatch2=0;
    //this.swatch3=0;
    //this.swatch4=0;
    //this.swatch5=0;
    //this.swatch6=0;
    //this.amount=0;
    this.M_deliverycountry="GB";
	//
	//shackswatch.updateform(document.swatchform);
	shackswatch.updatecookieform(document.swatchform);
	alert('Form swatch selections cleared');
	//this.remove();
};
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.$document.cookie = cookie;
};
Cookie.prototype.load = function()
{
	var i;
    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 (i = 0; i < a.length; i++) {
		a[i] = a[i].split(':');
	}
    for(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;
};
Cookie.prototype.updatecookieform=function(form)
{
	this.updatecookie(form);
	this.updateform(form);
};
function checkhighlight(swatchrangeobj,swatchobj)
{
	if (swatchobj.value<=0)
	{
		swatchobj.style.color='#0000FF';
	}
	else
	{
		swatchobj.style.color='black';
	}
	if (swatchrangeobj.value<=0)
	{
		swatchrangeobj.style.color='#0000FF';
	}
	else
	{
		swatchrangeobj.style.color='black';
	}
}
Cookie.prototype.updateform=function(form)
{
	//alert('dbg: updating form');
	form.M_swatch1.value=this.M_swatch1;
	form.M_swatch2.value=this.M_swatch2;
	form.M_swatch3.value=this.M_swatch3;
	form.M_swatch4.value=this.M_swatch4;
	form.M_swatch5.value=this.M_swatch5;
	form.M_swatch6.value=this.M_swatch6;
	form.M_deliverycountry.value=this.M_deliverycountry;
	stem='http://www.romansdirect.co.uk/fabrics/fab';
	//alert('dbg: checkhighlight');
	checkhighlight(form.swatchrange1,form.M_swatch1);
	checkhighlight(form.swatchrange2,form.M_swatch2);
	checkhighlight(form.swatchrange3,form.M_swatch3);
	checkhighlight(form.swatchrange4,form.M_swatch4);
	checkhighlight(form.swatchrange5,form.M_swatch5);
	checkhighlight(form.swatchrange6,form.M_swatch6);
	//alert('dbg: updating individual swatches');
	if (form.M_swatch1.value>0)
	{
		form.M_swt1.src=stem+form.M_swatch1.value.toString()+'t.jpg';
		form.M_swt1.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch1.value,10));
		reveal(form.M_swt1);
	}
	else
	{
		hide(form.M_swt1);
	}
	if (form.M_swatch2.value>0)
	{
		form.M_swt2.src=stem+form.M_swatch2.value.toString()+'t.jpg';
		form.M_swt2.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch2.value,10));
		reveal(form.M_swt2);
	}
	else
	{
		hide(form.M_swt2);
	}
	if (form.M_swatch3.value>0)
	{
		form.M_swt3.src=stem+form.M_swatch3.value.toString()+'t.jpg';
		form.M_swt3.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch3.value,10));
		reveal(form.M_swt3);
	}
	else
	{
		hide(form.M_swt3);
	}
	if (form.M_swatch4.value>0)
	{
		form.M_swt4.src=stem+form.M_swatch4.value.toString()+'t.jpg';
		form.M_swt4.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch4.value,10));
		reveal(form.M_swt4);
	}
	else
	{
		hide(form.M_swt4);
	}
	if (form.M_swatch5.value>0)
	{
		form.M_swt5.src=stem+form.M_swatch5.value.toString()+'t.jpg';
		form.M_swt5.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch5.value,10));
		reveal(form.M_swt5);
	}
	else
	{
		hide(form.M_swt5);
	}
	if (form.M_swatch6.value>0)
	{
		form.M_swt6.src=stem+form.M_swatch6.value.toString()+'t.jpg';
		form.M_swt6.title='Click here for a larger view of '+fabname(parseInt(form.M_swatch6.value,10));
		reveal(form.M_swt6);
	}
	else
	{
		hide(form.M_swt6);
	}
	//alert('dbg: updating delivery country');
	if (form.M_deliverycountry.value=='GB')
	{
		form.M_sendmethod.value=0;
		form.amount.value='Free';
	}
	else
	{
		form.M_sendmethod.value=1;
		form.amount.value=numstr(1.00,true);
	}
	//alert('dbg: update form completed');
};
Cookie.prototype.updatecookie=function(form)
{
	//alert('dbg: updating cookie');
	this.M_swatch1=form.M_swatch1.value;
	this.M_swatch2=form.M_swatch2.value;
	this.M_swatch3=form.M_swatch3.value;
	this.M_swatch4=form.M_swatch4.value;
	this.M_swatch5=form.M_swatch5.value;
	this.M_swatch6=form.M_swatch6.value;
	this.amount=form.amount.value;
	this.M_deliverycountry=form.M_deliverycountry.value;
	//this.store();
	//alert('dbg: updating cookie completed');
};

