// load Flash into Lightbox
function GroupDelegate(id) {
   var objLink = document.getElementById(id);
   Lightbox.prototype.start(objLink);
}

// workaround for absence of target attributes in xhtml strict

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}



function toggle(id) {
   var item = document.getElementById(id);

   if (item.style.display == "block")
      item.style.display = "none";
   else
      item.style.display = "block";
}



function validateAdmin(type) {
	var aMsg = new Array();
	if (type == 'add')
		var aMandatory = new Array('admUsername','admPassword1','admPassword2','admEmail','admFirstName','admLastName');
	else
		var aMandatory = new Array('admUsername','admEmail','admFirstName','admLastName');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fUsername = document.getElementById('admUsername');
	var sUsername = fUsername.value;
	if (sUsername != '' && (sUsername.length < 4 || sUsername.length > 16)) {
		aMsg[aMsg.length] = 'Username should be between 4 to 16 characters';
		highlightLabel(fUsername.previousSibling);
	}

	var fPassword1 = document.getElementById('admPassword1');
	var sPassword1 = fPassword1.value;
	if (sPassword1 != '' && (sPassword1.length < 8 || sPassword1.length > 32)) {
		aMsg[aMsg.length] = 'Password should be between 8 to 32 characters';
		highlightLabel(fPassword1.previousSibling);
	}

	var fPassword2 = document.getElementById('admPassword2');
	var sPassword2 = fPassword2.value;
	if (sPassword2 != sPassword1) {
		aMsg[aMsg.length] = 'Passwords do not match';
		highlightLabel(fPassword2.previousSibling);
	}

	var fEmail = document.getElementById('admEmail');
	var sEmail = fEmail.value;
	if (sEmail != '' && sEmail.length > 255) {
		aMsg[aMsg.length] = 'Email should be less than 255 characters';
		highlightLabel(fEmail.previousSibling);
	}
	else if (sEmail != '' && !validateEmail(sEmail)) {
		aMsg[aMsg.length] = 'Email is invalid';
		highlightLabel(fEmail.previousSibling);
	}

	var fFirstName = document.getElementById('admFirstName');
	var sFirstName = fFirstName.value;
	if (sFirstName != '' && sFirstName.length > 64) {
		aMsg[aMsg.length] = 'First Name should be less than 64 characters';
		highlightLabel(fFirstName.previousSibling);
	}

	var fLastName = document.getElementById('admLastName');
	var sLastName = fLastName.value;
	if (sLastName != '' && sLastName.length > 64) {
		aMsg[aMsg.length] = 'Last Name should be less than 64 characters';
		highlightLabel(fLastName.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateClient() {
	var aMsg = new Array();
	var aMandatory = new Array('clientNo','clientName','countryID','clientDEV','clientMT','clientINF','clientDateCreated');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fClientNo = document.getElementById('clientNo');
	var sClientNo = fClientNo.value;
	if (sClientNo != '' && sClientNo.length > 8) {
		aMsg[aMsg.length] = 'Client Number should be less than 8 digits';
		highlightLabel(fClientNo.previousSibling);
	}
	else if (sClientNo != '' && !validateNumber(sClientNo)) {
		aMsg[aMsg.length] = 'Client Number should contain only numbers';
		highlightLabel(fClientNo.previousSibling);
	}

	var fName = document.getElementById('clientName');
	var sName = fName.value;
	if (sName != '' && sName.length > 255) {
		aMsg[aMsg.length] = 'Company Name should be less than 255 characters';
		highlightLabel(fName.previousSibling);
	}

	var fDEV = document.getElementById('clientDEV');
	var sDEV = fDEV.value;
	if (sDEV != '' && sDEV.length > 4) {
		aMsg[aMsg.length] = 'DEV should be less than 4 digits';
		highlightLabel(fDEV.previousSibling);
	}
	else if (sDEV != '' && !validateNumber(sDEV)) {
		aMsg[aMsg.length] = 'DEV should contain only numbers';
		highlightLabel(fDEV.previousSibling);
	}

	var fMT = document.getElementById('clientMT');
	var sMT = fMT.value;
	if (sMT != '' && sMT.length > 4) {
		aMsg[aMsg.length] = 'MT should be less than 4 digits';
		highlightLabel(fMT.previousSibling);
	}
	else if (sMT != '' && !validateNumber(sMT)) {
		aMsg[aMsg.length] = 'MT should contain only numbers';
		highlightLabel(fMT.previousSibling);
	}

	var fINF = document.getElementById('clientINF');
	var sINF = fINF.value;
	if (sINF != '' && sINF.length > 4) {
		aMsg[aMsg.length] = 'INF should be less than 4 digits';
		highlightLabel(fINF.previousSibling);
	}
	else if (sINF != '' && !validateNumber(sINF)) {
		aMsg[aMsg.length] = 'INF should contain only numbers';
		highlightLabel(fINF.previousSibling);
	}

	var fDateCreated = document.getElementById('clientDateCreated');
	var sDateCreated = fDateCreated.value;
	if (sDateCreated != '' && !validateYMD(sDateCreated)) {
		aMsg[aMsg.length] = 'Date Created should be in yyyy-mm-dd format';
		highlightLabel(fDateCreated.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateContact() {
	var aMsg = new Array();
	var aMandatory = new Array('contFirstName','contLastName','contEmail');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fFirstName = document.getElementById('contFirstName');
	var sFirstName = fFirstName.value;
	if (sFirstName != '' && sFirstName.length > 64) {
		aMsg[aMsg.length] = 'First Name should be less than 64 characters';
		highlightLabel(fFirstName.previousSibling);
	}

	var fLastName = document.getElementById('contLastName');
	var sLastName = fLastName.value;
	if (sLastName != '' && sLastName.length > 64) {
		aMsg[aMsg.length] = 'Last Name should be less than 64 characters';
		highlightLabel(fLastName.previousSibling);
	}

	var fAppointment = document.getElementById('contAppointment');
	var sAppointment = fAppointment.value;
	if (sAppointment != '' && sAppointment.length > 64) {
		aMsg[aMsg.length] = 'Appointment should be less than 64 characters';
		highlightLabel(fAppointment.previousSibling);
	}

	var fEmail = document.getElementById('contEmail');
	var sEmail = fEmail.value;
	if (sEmail != '' && sEmail.length > 255) {
		aMsg[aMsg.length] = 'Email should be less than 255 characters';
		highlightLabel(fEmail.previousSibling);
	}
	else if (sEmail != '' && !validateEmail(sEmail)) {
		aMsg[aMsg.length] = 'Email is invalid';
		highlightLabel(fEmail.previousSibling);
	}

	var fMobile = document.getElementById('contMobile');
	var sMobile = fMobile.value;
	if (sMobile != '' && sMobile.length > 16) {
		aMsg[aMsg.length] = 'Mobile should be less than 16 characters';
		highlightLabel(fMobile.previousSibling);
	}

	var fPhone = document.getElementById('contPhone');
	var sPhone = fPhone.value;
	if (sPhone != '' && sPhone.length > 16) {
		aMsg[aMsg.length] = 'Phone should be less than 16 characters';
		highlightLabel(fPhone.previousSibling);
	}

	var fFax = document.getElementById('contFax');
	var sFax = fFax.value;
	if (sFax != '' && sFax.length > 16) {
		aMsg[aMsg.length] = 'Fax should be less than 16 characters';
		highlightLabel(fFax.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validatePortfolio(type) {
	var aMsg = new Array();
	if (type == 'add')
		var aMandatory = new Array('clientID','portTitle','portImgFilename','portDesc','portLaunchDate');
	else
		var aMandatory = new Array('clientID','portTitle','portDesc','portLaunchDate');
	var bMissing = false;
	var currField = '';

	resetLabels();

	var fCsPublished = document.getElementById('portCsPublished');
	if (fCsPublished.checked == true) {
		// if cs is published, cs fields are also mandatory
		if (type == 'add')
			aMandatory = new Array('clientID','portTitle','portImgFilename','portDesc','portLaunchDate','portCsImgFilename','portCsChallenge','portCsSolution','portCsResult','piFilename');
		else if (type == 'add_cs')
			aMandatory = new Array('clientID','portTitle','portDesc','portLaunchDate','portCsImgFilename','portCsChallenge','portCsSolution','portCsResult');
		else
			aMandatory = new Array('clientID','portTitle','portDesc','portLaunchDate','portCsChallenge','portCsSolution','portCsResult');
	}

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var checkboxes = document.getElementsByTagName('input');
	var bTagChecked = false;
	for (var i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].type == 'checkbox' && checkboxes[i].name.substring(0,8) == 'portTag_' && checkboxes[i].checked)
			bTagChecked = true;
	}
	if (!bTagChecked)
		aMsg[aMsg.length] = 'Please select at least one tag';

	var fTitle = document.getElementById('portTitle');
	var sTitle = fTitle.value;
	if (sTitle != '' && sTitle.length > 255) {
		aMsg[aMsg.length] = 'Title should be less than 255 characters';
		highlightLabel(fTitle.previousSibling);
	}

	var fFile = document.getElementById('portImgFilename');
	var sFile = fFile.value;
	if (sFile != '' && !validateImgExt(sFile)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fFile.previousSibling);
	}

	var fURL = document.getElementById('portURL');
	var sURL = fURL.value;
	if (sURL != '' && sURL.length > 255) {
		aMsg[aMsg.length] = 'Live URL should be less than 255 characters';
		highlightLabel(fURL.previousSibling);
	}
	else if (sURL != '' && !validateURL(sURL)) {
		aMsg[aMsg.length] = 'Live URL is invalid';
		highlightLabel(fURL.previousSibling);
	}

	var fLaunchDate = document.getElementById('portLaunchDate');
	var sLaunchDate = fLaunchDate.value;
	if (sLaunchDate != '' && !validateYMD(sLaunchDate)) {
		aMsg[aMsg.length] = 'Date Created should be in yyyy-mm-dd format';
		highlightLabel(fLaunchDate.previousSibling);
	}

	var fCsFile = document.getElementById('portCsImgFilename');
	var sCsFile = fCsFile.value;
	if (sCsFile != '' && !validateImgExt(sCsFile)) {
		aMsg[aMsg.length] = 'Case Study Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fCsFile.previousSibling);
	}

	if (type == 'add') {
		var fCsImg = document.getElementById('piFilename');
		var sCsImg = fCsImg.value;
		if (sCsImg != '' && !validateImgExt(sCsImg)) {
			aMsg[aMsg.length] = 'Case Study Gallery Image to be uploaded should be a gif, jpg, or png file';
			highlightLabel(fCsImg.previousSibling);
		}

		var fCaption = document.getElementById('piCaption');
		var sCaption = fCaption.value;
		if (sCaption != '' && sCaption.length > 255) {
			aMsg[aMsg.length] = 'Caption should be less than 255 characters';
			highlightLabel(fCaption.previousSibling);
		}
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateImage(type) {
	var aMsg = new Array();
	if (type == 'add')
		var aMandatory = new Array('piFilename');
	else
		var aMandatory = new Array();
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	if (type == 'add') {
		var fImg = document.getElementById('piFilename');
		var sImg = fImg.value;
		if (sImg != '' && !validateImgExt(sImg)) {
			aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
			highlightLabel(fImg.previousSibling);
		}
	}

	var fCaption = document.getElementById('piCaption');
	var sCaption = fCaption.value;
	if (sCaption != '' && sCaption.length > 255) {
		aMsg[aMsg.length] = 'Caption should be less than 255 characters';
		highlightLabel(fCaption.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateTag(type) {
	var aMsg = new Array();
	if (type == 'add')
		var aMandatory = new Array('tagCategoryID','tagName','tagImgFilename');
	else
		var aMandatory = new Array('tagCategoryID','tagName');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fName = document.getElementById('tagName');
	var sName = fName.value;
	if (sName != '' && sName.length > 255) {
		aMsg[aMsg.length] = 'Name should be less than 255 characters';
		highlightLabel(fName.previousSibling);
	}

	var fFile = document.getElementById('tagImgFilename');
	var sFile = fFile.value;
	if (sFile != '' && !validateImgExt(sFile)) {
		aMsg[aMsg.length] = 'Image to be uploaded should be a gif, jpg, or png file';
		highlightLabel(fFile.previousSibling);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateFeaturedTags() {
	var aMsg = new Array();
	var aMandatory = document.getElementsByTagName('select');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = aMandatory[i];
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else
		return true;
}



function validateFeaturedPortfolios() {
	var aMsg = new Array();
	var aMandatory = new Array();
	var aMandSelMul = new Array('portID');
	var aSelMul = new Array('portID');
	var bMissing = false;
	var currField = '';

	resetLabels();

	for (var i = 0; i < aMandatory.length; i++) {
		currField = document.getElementById(aMandatory[i]);
		if (currField.value == '') {
			bMissing = true;
			highlightLabel(currField.previousSibling);
		}
	}
	for (var i = 0; i < aMandSelMul.length; i++) {
		currField = document.getElementById(aMandSelMul[i]);
		if (currField.options.length == 0) {
			bMissing = true;
			highlightLabel(currField.parentNode.firstChild);
		}
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all mandatory fields';

	var fPortID = document.getElementById('portID');
	if (fPortID.options.length != 0 && fPortID.options.length > 8) {
		aMsg[aMsg.length] = 'Please select up to 8 featured portfolios';
		highlightLabel(fPortID.parentNode.firstChild);
	}

	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else {
		//for selects with multiple options, select all
		for (var i = 0; i < aSelMul.length; i++) {
			var fSelMul = document.getElementById(aSelMul[i]);
			for (var j = 0; j < fSelMul.options.length; j++) {
				fSelMul.options[j].selected = true;
			}
		}
		return true;
	}
}



function validatePagin(iPageCnt) {
	var fP = document.getElementById('p');
	var sP = fP.value;
	if (sP != '' && validateNumber(sP) && sP > 0 && sP <= iPageCnt)
		return true;
	else
		return false;
}



function validateEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (regex.test(email))
		return true;
	else
		return false;
}

function validateURL(url) {
	var regex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/;

	if (regex.test(url))
		return true;
	else
		return false;
}

function validatePdfExt(filename) {
	var dotIndex = filename.lastIndexOf('.');
	var extension = '';

	if (dotIndex != -1) {
		extension = filename.substring(dotIndex+1);
		extension = extension.toLowerCase();
		if (extension == 'pdf')
			return true;
		else
			return false;
	}
	else
		return false;
}

function validateImgExt(filename) {
	var dotIndex = filename.lastIndexOf('.');
	var extension = '';

	if (dotIndex != -1) {
		extension = filename.substring(dotIndex+1);
		extension = extension.toLowerCase();
		if (extension == 'jpg' || extension == 'jpeg' || extension == 'gif' || extension == 'png')
			return true;
		else
			return false;
	}
	else
		return false;
}

function validateNumber(txt) {
	var regex = /^\d+$/;

	if (regex.test(txt))
		return true;
	else
		return false;
}

function validateYMD(txt) {
	var regex = /^\d\d\d\d-\d\d-\d\d$/;

	if (regex.test(txt))
		return true;
	else
		return false;
}



function resetLabels() {
	var labels = document.getElementsByTagName('label');
	var currLabel = '';

	for (var i = 0; i < labels.length; i++) {
		currLabel = labels[i];
		currLabel.style.color = '';
	}
}

function highlightLabel(label) {
	if (label.nodeName.toLowerCase() == 'label') {
		label.style.color = 'red';
	}
}



function checkTagParent(currCb, value) {
	var tagParentID = tags[value];
	if (tagParentID != 0) {
		var tagParentCb = document.getElementById('portTag_' + tagParentID);
		if (currCb.checked)
			tagParentCb.checked = true;

		var tagGrandparentID = tags[tagParentID];
		if (tagGrandparentID != 0) {
			var tagGrandparentCb = document.getElementById('portTag_' + tagGrandparentID);
			if (currCb.checked)
				tagGrandparentCb.checked = true;
		}
	}
}

function setTagParentSel(value) {
	var tagParentSel = document.getElementById('tagParent');
	tagParentSel.options.length = null;
	tagParentSel.options[tagParentSel.length] = new Option('Select one', '');

	if (value != '') {
		for (var i in tags[value]) {
			if (tags[value][i] != null) {
				tagParentSel.options[tagParentSel.length] = new Option(tags[value][i], i);
				//alert('text: ' + tags[value][i] + ' and value: ' + i);
			}
		}
	}
}

function setPortfolioSel(value) {
	var portfolioSel = document.getElementById('portSel');
	portfolioSel.options.length = null;
	portfolioSel.options[portfolioSel.length] = new Option('Select one', '');

	if (value != '') {
		for (var i = 0; i < portfolios[value].length; i++) {
			if (portfolios[value][i] != null) {
				portfolioSel.options[portfolioSel.length] = new Option(portfolios[value][i], i);
				//alert('text: ' + portfolios[value][i] + ' and value: ' + i);
			}
		}
	}
}



function addOption(fromID, toID) {
	var fromSel = document.getElementById(fromID);
	var toSel = document.getElementById(toID);
	var selIndex = fromSel.selectedIndex;
	var selText = fromSel.options[selIndex].text;
	var selValue = fromSel.options[selIndex].value;
	var bExists = false;

	for (var i = 0; i < toSel.length; i++) {
		if (toSel.options[i].value == selValue)
			bExists = true;
	}

	if (!bExists && selValue != '')
		toSel.options[toSel.length] = new Option(selText, selValue);
	else if (bExists)
		alert('This option has already been added');
	else
		alert('Please select an option to add');
}

function add2Option(fromID1, fromID2, toID) {
	var fromSel1 = document.getElementById(fromID1);
	var fromSel2 = document.getElementById(fromID2);
	var toSel = document.getElementById(toID);
	var selIndex1 = fromSel1.selectedIndex;
	var selText1 = fromSel1.options[selIndex1].text;
	var selValue1 = fromSel1.options[selIndex1].value;
	var selIndex2 = fromSel2.selectedIndex;
	var selText2 = fromSel2.options[selIndex2].text;
	var selValue2 = fromSel2.options[selIndex2].value;
	var bExists = false;

	for (var i = 0; i < toSel.length; i++) {
		if (toSel.options[i].value == selValue2)
			bExists = true;
	}

	if (!bExists && selValue1 != '' && selValue2 != '')
		toSel.options[toSel.length] = new Option(selText2+' ('+selText1+')', selValue2);
	else if (selValue1 == '' || selValue2 == '')
		alert('Please select an option to add');
	else
		alert('This option has already been added');
}

function delOption(fromID) {
	var fromSel = document.getElementById(fromID);

	for (var i = fromSel.length-1; i >= 0; i--) {
		if (fromSel.options[i].selected)
			fromSel.options[i] = null;
	}
}

function moveOptionUp(selID) {
	var sel = document.getElementById(selID);
	var selIndex = sel.selectedIndex;
	var aboveSelIndex = selIndex - 1; // the index to swap with, and will be the new selected index
	var temp = '';

	if (selIndex > 0) {
		temp = sel.options[aboveSelIndex];
		sel.options[aboveSelIndex] = new Option(sel.options[selIndex].text, sel.options[selIndex].value);
		sel.options[selIndex] = temp;
		sel.options[aboveSelIndex].selected = true;
	}
	else if (selIndex == -1)
		alert('Please select an option to move');
}

function moveOptionDown(selID) {
	var sel = document.getElementById(selID);
	var selIndex = sel.selectedIndex;
	var belowSelIndex = selIndex + 1; // the index to swap with, and will be the new selected index
	var temp = '';

	if (selIndex != -1 && selIndex < sel.options.length - 1) {
		temp = sel.options[belowSelIndex];
		sel.options[belowSelIndex] = new Option(sel.options[selIndex].text, sel.options[selIndex].value);
		sel.options[selIndex] = temp;
		sel.options[belowSelIndex].selected = true;
	}
	else if (selIndex == -1)
		alert('Please select an option to move');
}



function inputFocus(input, value) {
    if (input.value == value)
        input.value = "";
}
function inputBlur(input, value) {
    if (input.value == "")
        input.value = value;
}



// check if contact form input/textarea fields are filled in, ie they have values other than their default values
// if not, remove default values so they are not submitted
// type 0 = get in touch, type 1 = contact, type 2 = quote

var contact_fields = "";

function checkFields(type) {
   if (type == 2) {
	var aMsg = new Array();
	var aMandatory = new Array('company name','contact name','country','elaboration','project budget','project priority');
	var bMissing = false;
	var currField = '';

	for (var i = 0; i < aMandatory.length; i++) {
		currFieldName = "quote_" + aMandatory[i].replace(" ", "_");
		currField = document.getElementById(currFieldName);
//alert(currFieldName + ' ' + currField.value);
		if (aMandatory[i] != 'elaboration' && currField.value == aMandatory[i]) {
			bMissing = true;
//alert('missing');
		}
		else if (aMandatory[i] == 'elaboration' && currField.value == 'elaboration: [brief intro of your project and objectives]') {
			bMissing = true;
//alert('missing');
		}
	}
	var fEmail = document.getElementById('quote_email');
	var sEmail = fEmail.value;
	var fPhone = document.getElementById('quote_phone');
	var sPhone = fPhone.value;
	if (sEmail == 'email' && sPhone == 'phone') {
		bMissing = true;
	}
	if (bMissing)
		aMsg[aMsg.length] = 'Please fill in all required fields';

	if (sEmail != 'email' && !validateEmail(sEmail)) {
		aMsg[aMsg.length] = 'Email is invalid';
	}

//alert(aMsg.length);
	if (aMsg.length > 0) {
		alert(aMsg.join('\n'));
		return false;
	}
	else {
		return true;
	}return true;
   }
   else {
      if (type == 0)
         contact_fields = new Array("name", "company name", "email", "message");
      else
         contact_fields = new Array("name", "company name", "email", "contact number", "message");

      for (var i = 0; i < contact_fields.length; i++) {
         if (type == 0)
            currFieldName = "getintouch_" + contact_fields[i];
         else
            currFieldName = "contact_" + contact_fields[i].replace(" ", "_");

         currField = document.getElementById(currFieldName);

         if (type == 1 && contact_fields[i] == "message" && currField.value == "comments / questions")
            currField.value = "";
         else if (currField.value == contact_fields[i])
            currField.value = "";
      }
      return true;
   }
}



// equalize heights of left/right floated divs
// add 20 (px) to compensate for bottom padding

/*
Derived from a script by Alejandro Gervasio.
Modified to work in FireFox by Stefan Mischook for Killersites.com

How it works: just apply the CSS class of 'column' to your pages' main columns.
*/
matchColumns=function(){

     var divs,contDivs,maxHeight,divHeight,d;

     // get all <div> elements in the document
     divs=document.getElementsByTagName('div');
     contDivs=[];

     // initialize maximum height value
     maxHeight=0;

     // iterate over all <div> elements in the document
     for(var i=0;i<divs.length;i++){

          // make collection with <div> elements with class attribute 'container'
          if(/\bcolumn\b/.test(divs[i].className)){
                d=divs[i];
                contDivs[contDivs.length]=d;

                // determine height for <div> element
                if(d.offsetHeight){
                     divHeight=d.offsetHeight;
                }

                else if(d.style.pixelHeight){
                     divHeight=d.style.pixelHeight;
                }

                // calculate maximum height
                maxHeight=Math.max(maxHeight,divHeight);
          }
     }

     // assign maximum height value to all of container <div> elements
     for(var i=0;i<contDivs.length;i++){
          contDivs[i].style.height=(maxHeight) + "px";
     }
}



// eMail Obfuscator Script 1.31 by Tim Williams - freeware

function obfuscate(type) {

var obfuscated = '';

if (type == 'link')

obfuscated = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,
99,111,110,116,97,99,116,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
34,62,
99,111,110,116,97,99,116,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
60,47,97,62);

if (type == 'fm_recipient')

obfuscated = String.fromCharCode(60,105,110,112,117,116,32,116,121,112,101,61,34,104,105,100,100,101,110,34,32,110,97,109,101,61,34,114,101,99,105,112,105,101,110,116,34,32,118,97,108,117,101,61,34,
99,111,110,116,97,99,116,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
34,32,47,62);

if (type == 'address_m')

obfuscated = String.fromCharCode(109,97,105,108,116,111,58,
99,111,110,116,97,99,116,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103);

if (type == 'address')

obfuscated = String.fromCharCode(99,111,110,116,97,99,116,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103);

if (type == 'link_jobs')

obfuscated = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,
106,111,98,115,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
34,62,
106,111,98,115,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
60,47,97,62);

if (type == 'link_billing')

obfuscated = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,
98,105,108,108,105,110,103,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
34,62,
98,105,108,108,105,110,103,64,119,101,98,112,117,112,112,105,101,115,46,99,111,109,46,115,103,
60,47,97,62);

document.write(obfuscated);

}



// change open/closed images for toggle links (animatedcollapse.js)
var contact = new Array("general", "quote");
var careers = new Array("designer", "programmer");

function toggle_collapse(id) {
	var group = id.substring(0, id.indexOf('_'));
	var newDiv = document.getElementById(id);
	var newLink = document.getElementById(id + "_link");
	var currLink = '';

	for (var i = 0; i < window[group].length; i++) {
		currLink = document.getElementById(group + "_" + window[group][i] + "_link");
		currLink.style.backgroundImage = 'url(img/toggle_closed.gif)';
	}

	if (newDiv.style.display != 'block')
		newLink.style.backgroundImage = 'url(img/toggle_open.gif)';
	else
		newLink.style.backgroundImage = 'url(img/toggle_closed.gif)';

	animatedcollapse.toggle(id);
}



// run all necessary functions on page load

function loadAll() {
	externalLinks();
	matchColumns();
	if (document.title.search(/Case Studies/) != -1)
		enableTooltips("casestudy_tn");
	if (document.title.search(/Contact Us/) != -1) {
		animatedcollapse.addDiv('contact_general', 'speed=500,hide=1,group=contact');
		animatedcollapse.addDiv('contact_quote', 'speed=500,hide=1,group=contact');
		animatedcollapse.init();
	}
	if (document.title.search(/Careers/) != -1) {
		//animatedcollapse.addDiv('careers_designer', 'speed=500,hide=1,group=careers');
		//animatedcollapse.addDiv('careers_programmer', 'speed=500,hide=1,group=careers');
		//animatedcollapse.init();
	}
}
window.onload = loadAll;