// JavaScript Document

var bannerOp=-1;

function validEmail(s) {
	good=true;
	if (s.indexOf('"')!=-1 || s.indexOf("'")!=-1) good=false;
	else {
		parts=s.split('@');
		if (parts.length!=2) good=false;
		else {
			uname=parts[0].split('.');
			for (i=0;i<uname.length;i++) {
				if (uname[i].length==0) good=false;
			}
			if (good) { 
				domain=parts[1].split('.');
				if (domain.length>1) {
					for (i=0;i<domain.length;i++) {
						if (i==domain.length-1) {
							if (domain[i].length<2 || domain[i].length>3) good=false;
						} else {
							if (domain[i].length==0) good=false;
						}
					}
				} else good=false;
			}
		}
	}
	return good;
}

function topMenu(pageID, on) {
	if (on) {
		document.getElementById('topMenuImage'+pageID).src='images/topmenu/'+pageID+'_on.jpg';
		document.getElementById('topMenuCell'+pageID).className='topMenuOver';
	} else {
		document.getElementById('topMenuImage'+pageID).src='images/topmenu/'+pageID+'.jpg';
		document.getElementById('topMenuCell'+pageID).className='topMenuOut';
	}
}

function greyMenu(pageID, on) {
	if (on) {
		document.getElementById('greyMenuImage'+pageID).src='images/greymenu/'+pageID+'_on.jpg';
		document.getElementById('greyMenuCell'+pageID).className='greyMenuOver';
	} else {
		document.getElementById('greyMenuImage'+pageID).src='images/greymenu/'+pageID+'.jpg';
		document.getElementById('greyMenuCell'+pageID).className='greyMenuOut';
	}
}

function openPage(pageID) {
	document.location.href=pageID+'.php';
}

function getNextBanner(pageID) {
	ajaxRequest('includes/ajax_getbanner.php', 'pageID='+pageID, 'div|bannerBack', 'switchBanner()');
}

function fd(div,percent) {
	if (percent>99) percent=99;
	if (percent<10) percent='0'+percent;
	isIE=(navigator.appName.indexOf('Microsoft')!=-1);
	isNN=(navigator.appName=='Netscape');
	if (isIE) {
		document.getElementById(div).style.filter="alpha(opacity="+percent+")";
		document.getElementById(div).filters.alpha.apply();
	} else if (isNN) {
		document.getElementById(div).style.MozOpacity='.'+percent;
	} else {
		document.getElementById(div).style.opacity='.'+percent;
	}
}

function fadeBanner() {
	if (bannerOp>-1) {
		if (bannerOp==0) {
			// finish
			bannerOp=-1;
			content=document.getElementById('bannerBack').innerHTML;
			document.getElementById('bannerFront').innerHTML=content;
			fd('bannerFront',99);
		} else {
			// continue fading
			bannerOp-=2;
			fd('bannerFront',bannerOp);
		}
	}
}

function switchBanner() {
	content=document.getElementById('bannerBack').innerHTML;
	if (content.indexOf('Error')==-1) {
		bannerOp=100;
	}
}

function initTopBanners(pageID) {
	if (document.getElementById('bannerBack')) {
		clearInterval(init);
		setInterval("getNextBanner('"+pageID+"')",8000);
	}
}

function focusForm() {
	if (document.loginForm) {
		if (document.loginForm.userName.value=='') {
			document.loginForm.userName.focus();
		} else {
			document.loginForm.password.focus();
		}
	}
	if (document.contactForm) {
		if (document.contactForm.contactName.value=='') {
			document.contactForm.contactName.focus();
		}
	}
	if (document.passwordReminderForm) {
		if (document.passwordReminderForm.email.value=='') {
			document.passwordReminderForm.email.focus();
		}
	}
	if (document.newUserRequestForm) {
		if (document.newUserRequestForm.fullName.value=='') {
			document.newUserRequestForm.fullName.focus();
		}
	}
}

function checkContactForm(formObj) {
	ok=true;
	message='Please complete the following field(s):\n';
	if (formObj.contactName.value=='') {
		ok=false;
		message+='\n- Name';
	}
	if (formObj.contactEmail.value=='') {
		ok=false;
		message+='\n- Email';
	} else if (!validEmail(formObj.contactEmail.value)) {
		ok=false;
		message+='\n- Invalid Email address';
	}
	if (formObj.contactMessage.value=='') {
		ok=false;
		message+='\n- Message';
	}
	if (!ok) alert (message);
	return ok;
}

function checkFaultForm(formObj) {
	ok=true;
	message='Please complete the following field(s):\n';
	if (formObj.contactName.value=='') {
		ok=false;
		message+='\n- Name';
	}
	if (formObj.contactEmail.value=='') {
		ok=false;
		message+='\n- Email';
	} else if (!validEmail(formObj.contactEmail.value)) {
		ok=false;
		message+='\n- Invalid Email address';
	}
	if (formObj.faultDescription.value=='') {
		ok=false;
		message+='\n- Fault description';
	}
	if (!ok) alert (message);
	return ok;
}

function checkFaqForm(formObj) {
	ok=true;
	//message='Please complete the following field(s):\n';
	if (formObj.question.value=='') {
		ok=false;
		//message+='\n- Name';
	}
	/*if (formObj.contactEmail.value=='') {
		ok=false;
		message+='\n- Email';
	}
	if (formObj.contactMessage.value=='') {
		ok=false;
		message+='\n- Message';
	}
	if (!ok) alert (message);*/
	return ok;
}

function checkUserDetailsForm(formObj) {
	ok=true;
	message='The following error(s) occured:\n';
	if (formObj.userName.value=='') {
		ok=false;
		message+='\n- Please enter username';
	}
	if (formObj.oldPassword.value!='' || formObj.newPassword.value!='') {
		if (formObj.oldPassword.value=='') {
			ok=false;
			message+='\n- Please enter your current password';
		}
		if (formObj.newPassword.value=='') {
			ok=false;
			message+='\n- Please enter the new password';
		} else
		if (formObj.newPassword.value!=formObj.cPassword.value) {
			ok=false;
			message+='\n- Please enter the same password in the Confirm new password field';
		}
	}
	if (!ok) alert (message);
	return ok;
}

function checkIssueForm(form) {
	ok=true;
	message='The following error(s) occured:\n';
	if (form.contractID.selectedIndex==0) {
		ok=false;
		message+='\n- Please select contract No';
	} else
	if (form.contractID.value=='0' && form.dateOfPurchase.value=='') {
		ok=false;
		message+='\n- Please select date of purchase';
	}
	if (!ok) alert(message);
	return ok;
}

function getIssueOptions(contractID) {
	document.getElementById('issueOptions').innerHTML='<center>Loading...</center>';
	ajaxRequest('includes/ajax_getissueoptions.php', 'contractID='+contractID, 'div|issueOptions', null);
}

function checkNewUserRequestForm(formObj) {
	ok=true;
	message='Please complete the following field(s):\n';
	if (formObj.fullName.value=='') {
		ok=false;
		message+='\n- Name';
	}
	if (formObj.email.value=='') {
		ok=false;
		message+='\n- Email';
	}
	if (!ok) alert (message);
	return ok;
}

setTimeout('focusForm()',400);
setInterval('fadeBanner()',10);


