function checkForm(formName) {
	var required = new Array('category',
												 	 'subcategory',
												 	 'skills_0',
												   'duration',
												 	 'start_month',
												   'start_day',
												   'start_year',
												   'email',
												   'email2',
												   'uname',
												   'pictocode');
												 
	var required_show = new Array('Category',
												 				'Subcategory',
												 				'Skills',
												 				'Duration',
												 				'Start Month',
												 				'Start Day',
												 				'Start Year',
												 				'Email',
												 				'Repeat Email',
												 				'Name',
												 				'Number shown');												 

	for(j=0; j<required.length; j++) {
    for (i=0; i<document.forms[formName].length; i++) {
      if (document.forms[formName].elements[i].name == required[j]
          && document.forms[formName].elements[i].value == '' ) {
        alert('Field ' + required_show[j] + ' is required');
        document.forms[formName].elements[i].focus();
        return false;
      }
    }
  }
  
  var emailID = document.forms[formName].email;
  var email2ID = document.forms[formName].email2;
  var pictocodeID = document.forms[formName].pictocode;
  
  if (echeckEmail(emailID.value) == false) {
  	emailID.focus();
  	emailID.select();
  	return false;
  }
  
  if (emailID.value != email2ID.value) {
  	alert ('Email does nor match Repeat Email');
  	emailID.focus();
  	email2ID.select();
  	return false;
  }
 
  return true;
}

	function echeckEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
	}

	function enableSubcat(url, formName) {
	  if (document.forms[formName].elements.category.value) {
	  	doSearchLoad(url, 'subcat', formName);
	  	document.forms[formName].elements.subcategory.disabled = false;
	  	document.forms[formName].elements.subcategory.focus();
	  	document.getElementById("send_cat").style.display = 'none';
			document.getElementById("send_subcat").style.display = 'block';
	  }
	  else {
	  	document.forms[formName].elements.subcategory.disabled = true;
	  }
	}
	
	function enableDesc(field, formName, contactNotice, contactExpand) {
		if (field) {
			if (document.forms[formName][field].value) {
				document.getElementById(contactNotice).style.display = 'none';
				document.getElementById(contactExpand).style.display = 'block';
				
				if (field == 1) {
					document.getElementById("send_subcat").style.display = 'none';
					if (!document.forms[formName].skills_0.value ||
							document.forms[formName].skills_0.value == '--- Please select ---') {
						document.getElementById("send_skills").style.display = 'block';
						document.forms[formName].skills_0.focus();
					}
				}
				
				if (field == 3) {
					if (document.forms[formName][field].value &&
							document.forms[formName][field].value != '--- Please select ---') {
						document.getElementById("send_skills").style.display = 'none';
						if (!document.forms[formName].duration.value) { 
							document.getElementById("send_term").style.display = 'block';
						}
					}
				}
				
				if (field == 5 || field == 6 || field == 7) {
					if (document.forms[formName].start_month.value &&
							document.forms[formName].start_month.value != 'Month' &&
							document.forms[formName].start_day.value &&
							document.forms[formName].start_day.value != 'Day' &&
							document.forms[formName].start_year.value &&
							document.forms[formName].start_year.value != 'Year') {
						document.getElementById("send_start").style.display = 'none';
						document.getElementById("send_desc").style.display = 'block';
					}
				}
			}
			
			if (field == 3 && document.forms[formName][field].value) {
				doSearchLoad('index.php?mode=getskills', '', formName);
			}
		}
	}
	
function treatDuration(formName) {
	if (document.forms[formName].duration.value) {
		document.getElementById("send_term").style.display = 'none';
		if (!document.forms[formName].start_month.value ||
				document.forms[formName].start_month.value == 'Month' ||
				!document.forms[formName].start_day.value ||
				document.forms[formName].start_day.value == 'Day' ||
				!document.forms[formName].start_year.value ||
				document.forms[formName].start_year.value == 'Year') {
			document.getElementById("send_start").style.display = 'block';
		}
	}
	else {
		document.getElementById("send_term").style.display = 'block';
	}
}

function treatDesc(formName, blur) {
	if (document.forms[formName].description.value) {
		document.getElementById("send_desc").style.display = 'none';
	}
	else {
		document.getElementById("send_desc").style.display = 'block';
	}
	
	if (blur) {
		if (document.forms[formName].description.value) {
			document.getElementById("send_desc").style.display = 'none';
			document.getElementById("send_email").style.display = 'block';
		}
		else {
			document.getElementById("send_desc").style.display = 'none';
		}
	}
}

function treatEmail() {
	document.getElementById("send_email").style.display = 'block';
}

function treatRepEmail(formName) {
	if (document.forms[formName].email.value && document.forms[formName].email2.value) {
		document.getElementById("send_email").style.display = 'none';
		
		if (document.forms[formName].email.value != document.forms[formName].email2.value) {
			document.getElementById("send_email").style.display = 'none';
			document.getElementById("match_email").style.display = 'block';
		}
		else {
			document.getElementById("match_email").style.display = 'none';
		}
		
		if (!document.forms[formName].uname.value) {
			document.getElementById("send_name").style.display = 'block';
		}
	}
}

//function doneRepEmail(formName) {
//	if (document.forms[formName].email.value == document.forms[formName].email2.value) {
//		document.getElementById("send_email").style.display = 'none';
//		document.getElementById("send_name").style.display = 'block';
//	}
//}

function treatName(formName) {
	if (document.forms[formName].uname.value) {
		document.getElementById("send_name").style.display = 'none';
		document.getElementById("send_pictocode").style.display = 'block';
	}
	else {
		document.getElementById("send_name").style.display = 'block';
	}
}

function treatPictocode(formName) {
	if (!document.forms[formName].pictocode.value) {
		document.getElementById("send_pictocode").style.display = 'block';
	}
}

function countPictocode(formName) {
	if (document.forms[formName].pictocode.value.length == 5) {
		document.getElementById("send_pictocode").style.display = 'none';
	}
	else {
		document.getElementById("send_pictocode").style.display = 'block';
	}
}
	
function addRemove(field, sel_id, formName) {
	if (document.forms[formName][field].value) {
		doSearchLoad('index.php?mode=getskills', '', formName);
	}
	else {
	  remove(sel_id);
	}
} 

function remove(sel_id) {
	var brs = whichBrs();

	if (brs == 'IE') {
  		document.getElementById(sel_id).removeNode(true);
  	}
  	else {
  		var Node1 = document.getElementById(sel_id); 
    	var len = Node1.childNodes.length;
       
    	for(var i = 0; i < len; i++) {
    		if (Node1.childNodes[i]) {
				Node1.removeChild(Node1.childNodes[i]);
			}      
    	} 
  	}
}

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'IE';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

function openHidden(name, titleOn, titleOff, isVal) {
	
  var objHidden = document.getElementById(name);
  var onTitle = document.getElementById(titleOn);
  var offTitle = document.getElementById(titleOff);
  if (isVal) var objVal = document.getElementById(name + '_completed');

  if (objHidden.style.display != 'block') {
    objHidden.style.display = 'block';
    onTitle.style.display = 'block';
    offTitle.style.display = 'none';
    if (objVal) objVal.style.display = 'none';
  }
  else {
    objHidden.style.display = 'none';
    onTitle.style.display = 'block';
    offTitle.style.display = 'none';
    if (isVal)
      objVal.style.display = 'block';
  }

  return;
}

var request;

function doSearchLoad(url, select_id, formName) {

  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    if (request.overrideMimeType)
      request.overrideMimeType('text/html');
  }
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if (request) {
    request.open('GET', url, true);
    request.send(null);
  }

  request.onreadystatechange = function() {
    abortRequest = window.setTimeout('request.abort();', 10000);
		
    if (request.readyState == 4) {
      clearTimeout(abortRequest);
      
      if (request.status == 200) {
        //alert(request.responseText);
        document.getElementById('loading').style.display = 'none';
        if (select_id) {
        	fill(select_id, request.responseText, '--- Please select ---');
        }
        else {
          createSelect('addSkill', request.responseText, formName);
        }
      }
      else {
        document.getElementById('loading').style.display = 'block';
        doSearchLoad(url, select_id, formName);
        //alert('Failed loading data. Please try again.');
      }
    }
    else {
      document.getElementById('loading').style.display = 'block';
    }
  }
}

function fill (select_id, data, title) {

  var select = addOptions(select_id, title);
  //alert (data);

  if (data.length == 0) return;

  var arr = data.split('*');

  for (var i in arr) {
    val = arr[i].split('~');
    select.options[select.options.length] = new Option(val[1], val[0], false, false);
  }

  //select.disabled = false;
  //select.focus();
}

function addOptions(select_id, title) {
	
  var select = document.getElementById(select_id);
  select.options.length = 0;
  select.options[select.options.length] = new Option(title, '', false, false);
  select.options[select.options.length] = new Option('', '', false, false);
  if (title == '--- Add skill or ignore ---') {
  	select.options[select.options.length] = new Option('--- Remove ---', '', false, false);
  	select.options[select.options.length] = new Option('', '', false, false);
  }
  return select;
}

function createSelect(newselect_id, data, formName) {
	var numFields = document.forms[formName].length;
	//var nextVal = numFields - 13;
	var nextVal = Math.floor(Math.random()*100001);
	var elName = 'skills_'+nextVal;
  	var se = document.createElement('select');
  	var brs = whichBrs();
  	
 	se.setAttribute("style", "width:170px;");
	se.style.cssText = "width:170px;";
	se.name = elName;
	se.setAttribute('id', elName);
	se.id = elName;
	//se.setAttribute('onChange', 'return enableDesc(3);');
	se.onchange=function(){return addRemove(elName, 'div' + elName, formName)};

	var elDiv = document.createElement('<div>');
	elDiv.id = 'div' + elName;
	var lbreak = document.createElement('<br>');
	if (brs == 'IE') {
		var title = document.createTextNode('\u00a0\u00a0Skills:\u00a0\u00a0');
	}
	else {
		var title = document.createTextNode('\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0');
	}

	document.getElementById(newselect_id).appendChild(elDiv);
	elDiv.appendChild(lbreak);
	elDiv.appendChild(title);
	elDiv.appendChild(se);
	
	fill (elName, data, '--- Add skill or ignore ---');
	
	document.getElementById(newselect_id).style.display = 'block';
}