// JavaScript Document
// Example: obj = findObj("image1");
function findObj(theObj, theDoc) {

	var p, i, foundObj;
	
	if(!theDoc) theDoc = document;
	if((p = theObj.indexOf("?")) > 0 && parent.frames.length) {
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	for (i=0; !foundObj && i < theDoc.forms.length; i++) 
	foundObj = theDoc.forms[i][theObj];
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
	foundObj = findObj(theObj,theDoc.layers[i].document);
	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
	
	return foundObj;

}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;

function preloadImages() {
	if (document.images) {
		preloadFlag = true;
	}
}

function setMainPic(productref, thumbindex) {

	main = findObj("mainpic");
	tmb = findObj("thumbpic" + thumbindex);
	
	msrc = String(main.src);
	tsrc = String(tmb.src);
	
	msrc = msrc.replace(productref + "/large", productref + "/thumb");
	tsrc = tsrc.replace(productref + "/thumb", productref + "/large");
	
	main.src = tsrc;
	tmb.src = msrc;

}

function submitForm(formName) {
	
	fm = findObj(formName);
	fm.submit();
	
}

function showEnc(category) {
	
	nl = "more-info.php?category=" + category + "#" + category;
	enc = window.open(nl, "Enc", "height=400, width=500, toolbar=no, resizable=yes, status=no, titlebar=no, scrollbars=yes");
	enc.focus();
	
}

function validateEnquiryForm() {
	var dm = document.forms["contactform"];
	var errorMsg = "";
	
	if (!dm.name.value) {
		errorMsg = errorMsg + "Name is required\n";
	}
	if (!((dm.tel.value)||(dm.email.value))) {
		errorMsg = errorMsg + "Telephone Number or E-mail address are required\n";
	}
	if (!isEmail(dm.email.value)&&dm.email.value.length>0) {
		errorMsg = errorMsg + "Invalid E-mail Address\n";
	}
	if (!((dm.subject.value)||(dm.comments.value))) {
		errorMsg = errorMsg + "Subject or Comments are required";
	}
	if (errorMsg) {
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
}

function isEmail(str) {
	var ret = true;
	if (str.indexOf("@")<=0) {
		ret = false;
	}
	if (str.lastIndexOf(".")<str.indexOf("@")) {
		ret = false;
	}
	return ret;
}

function cardType(ref, title, externalref, issuedate, issuenum, luhnverify, available) {
	this.ref = ref;
	this.title = title;
	this.externalref = externalref;
	this.issuedate = issuedate;
	this.issuenum = issuenum;
	this.luhnverify = luhnverify;
	this.available = available;
}

function luhnVerify(cardnumber) {

	var cnum = new String(cardnumber);
	cnum = cnum.replace(" ","");
	strlen = cnum.length;
	
	i = strlen-1;
	sum = 0;
	
	while (i>=0) {
		cchar = cnum.charAt(i);
		num = Number(cchar);
		//alert(cchar + "=>" + num)
		sum += num;
		i--;
		
		if (i>=0) {
			cchar = cnum.charAt(i);
			num = Number(cchar);
			num = (num * 2);
			if (num>9) {
				num -= 9;
			}
			//alert(cchar + "=>" + num)
			sum += num;
			i--;
		}
	}
	
	//alert("abc");
	//alert("sum = " + String(sum));

	cv = sum/10;
	if (Math.floor(cv)==cv) {
		return true;
	} else {
		return false;
	}

}

function openpopup(url,features) {
  popupWin = window.open(url, 'remote', features)
  }
