// Project: Enyuka Green - Website
// Developed by: Chris Halgryn for Enyuka Devco (Pty) Ltd
// Copyright: © 2009-2010 Enyuka Devco (Pty) Ltd
// Contact e-mail: chris@enyukagroup.com
// Date created: 2009/06/01
// Date modified: 2010/04/27

// JavaScript Document

// Main JavaScript functions
function validateField (objValue,checkType)	{ 
	switch (checkType) {
		// Allow alpha-numeric with special characters
		case 1: {
			var charPos = objValue.value.search ("[^A-Za-z0-9\-\+_= \s!@#\$%&\*\(\):;',\.\?/\r\n]");
			break;
		}
		// Allow alpha-numeric
		case 2: {
			var charPos = objValue.value.search ("[^A-Za-z0-9]");
			break;
		}
		// Allow numeric
		case 3: {
			var charPos = objValue.value.search ("[^0-9]");
			break;
		}
		// Allow decimal numeric
		case 4: {
			var charPos = objValue.value.search ("[^0-9\.]");
			break;
		}
	}
	if (objValue.value.length > 0 &&  charPos >= 0) {
		alert ('Invalid character entered at position ' + eval (charPos + 1) + '.');
		objValue.focus ();
		objValue.select ();
		return false;
	}
}

function showHideDivLayer (divName,showHide) {
	if (showHide) {
		setVisibility = 'block';
	} else {
		setVisibility = 'none';
	}
	if (document.getElementById) {
		document.getElementById (divName).style.display = setVisibility; 
	} else { 
		if (document.layers) {
			eval ('document.' + divName + '.display = \'' + setVisibility + '\''); 
		} else {
			eval ('document.all.' + divName + '.style.display = \'' + setVisibility + '\''); 
		}
	} 
}

function showHideLayer (divName) {
	var newStyleDisplay = (document.getElementById (divName).style.display == 'block'?'none':'block');
	document.getElementById (divName).style.display = newStyleDisplay;
	return newStyleDisplay;
}

function formatQuantity (numVar,maxVal) {
	num = numVar.toString ();
	if (isNaN (num)) {
		num = '0';
	} else if (num > maxVal) {
		num = maxVal;
	}
	sign = (num == (num = Math.abs (num)));
	num = Math.floor (num);
	return (num);
}

function formatCurrency (numVar,maxVal) {
	num = numVar.toString ().replace (/\$|\,/g,'');
	if (isNaN (num)) {
		num = '0';
	} else if (maxVal > '0' && num > maxVal) {
		num = maxVal;
	}
	sign = (num == (num = Math.abs (num)));
	num = Math.floor (num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor (num / 100).toString();
	if (cents < 10) {
		cents = '0' + cents;
	}
	return (num + '.' + cents);
}

function hideStatus () {
	window.status = window.status;
	return true;
}

function openPdf (targetUrl) {
	pdfWin = window.open (targetUrl,'pdfWindow','width=760,height=500,scrollbars');
	pdfWin.focus ();
}

function viewSlideShow (prdId) {
	targetUrl = 'product.php?key=viewslideshow&prdId=' + prdId;
	slideWin = window.open (targetUrl,'slideWindow','width=800,height=600,scrollbars=no');
	slideWin.focus ();
}

function cleanQuantity (numVar) {
	quanLabel = numVar.value;
	if (quanLabel == 'quantity' || quanLabel == 'qty' || quanLabel == '0' || quanLabel * 1 == 0) {
		quanLabel = '';
	}
	return (quanLabel);
}

function cleanKeywords (numVar) {
	quanLabel = numVar.value;
	if (quanLabel == 'keywords') {
		quanLabel = '';
	}
	return (quanLabel);
}

function genNewCode () {
	var newVerificationCodeImage = document.getElementById ('verificationCodeImage');
	var now = new Date();
	newVerificationCodeImage.src = 'verificationimage.php?' + now.getTime ();
}

function goToCheckoutPage () {
	if (confirm ('Proceed to complete transaction now?')) {
		document.location.href = './checkout.php?key=complete';
	}
}
