function checkForm2(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;
  
  
  if (echeckEmail(emailID.value) == false) {
  	emailID.focus();
  	emailID.select();
  	return false;
  }
  
  if (emailID.value != email2ID.value) {
  	alert ('Email does not match Repeat Email');
  	email2ID.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 enableSubcat2(url, formName) {
	  if (document.forms[formName].elements.category.value) {
	  	doSearchLoad2(url, 'subcat2', formName);
	  	document.forms[formName].elements.subcategory.disabled = false;
	  	document.forms[formName].elements.subcategory.focus();
	  }
	  else {
	  	document.forms[formName].elements.subcategory.disabled = true;
	  }
	}
	
	function enableDesc2(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 == 3 && document.forms[formName][field].value) {
				doSearchLoad2('index.php?mode=getskills', '', formName);
			}
		}
	}
	
function addRemove2(field, sel_id, formName) {
	if (document.forms[formName][field].value) {
		doSearchLoad2('index.php?mode=getskills', '', formName);
	}
	else {
	  remove2(sel_id);
	}
} 

function remove2(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 whichBrs2() {
	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 openHidden2(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 doSearchLoad2(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) {
        	fill2(select_id, request.responseText, '--- Please select ---');
        }
        else {
          createSelect2('addSkill2', request.responseText, formName);
        }
      }
      else {
        document.getElementById('loading').style.display = 'block';
        doSearchLoad2(url, select_id, formName);
        //alert('Failed loading data. Please try again.');
      }
    }
    else {
      document.getElementById('loading').style.display = 'block';
    }
  }
}

function fill2 (select_id, data, title) {

  var select = addOptions2(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 addOptions2(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 createSelect2(newselect_id, data, formName) {
	var numFields = document.forms[formName].length;
	//var nextVal = numFields - 14;
	var nextVal = Math.floor(Math.random()*100001);
	var elName = 'skillsX_'+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.onchange=function(){return addRemove2(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';
}
