



commentValid = false;

function updateComment(){
    commentCnt = document.getElementById('commentCnt');
    commentRmn = document.getElementById('commentRmn');
    comment = document.getElementById('comment');
    max = commentRmn.defaultValue;
    cnt = comment.value.length;
    commentCnt.value = cnt;
    commentRmn.value = max - cnt;
    if (cnt >= 50) {
        commentValid = true
    }
    
}

function rateValidate(){
    compId = document.getElementsByName('compId');
    //alert(compId[0]);    
	 branchId = document.getElementsByName('branchId');
	//alert(branchId[0])
    valid = true;
    if (compId[0] == null) {
        if (tbValidate(new Array('companyName', 'companyAddress1')) == false) {
            valid = false;
				//alert(valid)
        }
//        if (selValidate('companyState') == false) {
//            valid = false;
//        }
		  //alert(valid)
		  if (locValidate('companyLoc')==false){
		  	valid = false;
		  }
		  
        if (selValidate('companyCats') == false) {
            valid = false;
				//alert('it got here too')
        }
		  //alert(valid)
        if (webValidate('companyWebSite') == false) {
            valid = false;
        }
		  //alert(valid)
    }
    branches = new Array('branchAddress1', 'branchLoc')
    branch = true
    for (i = 0; i < branches.length; i = i + 1) {
		tb = document.getElementsByName(branches[i])[0];
		//alert(tb.value)
		if (tb != null) {
			if (tb.value != tb.defaultValue && tb.value != '') {
				branch = false
			}
		}
    }
    if (branchId[0] == null && !branch) {
        if (tbValidate(new Array('branchAddress1')) == false) {
            valid = false;
        }
		  if (locValidate('branchLoc')==false){
		  	valid = false
		  }
//        if (selValidate('branchState') == false) {
//            valid = false;
//        }
    }
    if (radioValidate('Pay') == false) {
        valid = false
    }
	 //alert(valid)
    if (radioValidate('Respect') == false) {
        valid = false
    }
    if (radioValidate('Career Advancement') == false) {
        valid = false
    }
    if (radioValidate('Benefits') == false) {
        valid = false
    }
    if (radioValidate('Vacation') == false) {
        valid = false
    }
    if (radioValidate('Ideas') == false) {
        valid = false
    }
    if (radioValidate('Professionalism') == false) {
        valid = false
    }
    if (radioValidate('Morale') == false) {
        valid = false
    }
    if (radioValidate('Work/Life Balance') == false) {
        valid = false
    }
    if (radioValidate('Employee Turnover') == false) {
        valid = false
    }
    cmt = document.getElementById('comment');
    if (cmt.value.length < 50) {
        cmt.style.background = 'red';
        valid = false
    }
    else {
        cmt.style.background = 'white';
    }
    if (valid == false) {
        span1 = document.getElementById('validationWarning1');
        span2 = document.getElementById('validationWarning2');
        text = 'Some of the necessary information is incomplete.  Please complete all of the required items which are highlighted in red.<br /><br />';
        span1.innerHTML = text;
        span2.innerHTML = text;
        window.scrollTo(0, 0);
    }
	 //alert(valid);
    return valid;
}



function tbValidate(tbNames){
    tbValid = true;
    //	alert(tbNames)
    for (x=0; x<tbNames.length; x=x+1) {
        //alert(tbNames[x]);
        tb = document.getElementsByName(tbNames[x]);
        //		alert(tbNames[x]);
        //		alert(tb);
        tb = tb[0];
        //		alert(tb.name)
        if (tb.value == tb.defaultValue || tb.value == '') {
            hilightBack(tb);
            tbValid = false;
        }
        else {
            resetBack(tb);
        }
    }
	 //alert(tbValid);
    return tbValid;
}

function locValidate(tbName){
    tb = document.getElementsByName(tbName);
    //		alert(tbNames[x]);
    //		alert(tb);
    tb = tb[0];
    //		alert(tb.name)
	 val = tb.value.split(',')
	 
    if (tb.value == tb.defaultValue || tb.value == '') {
        hilightBack(tb);
        tbValid = false;
    }
	 else if (val.length<2){
	 	alert('There is an issue with one of your locations.\nThe location must at least be city, country.\nThe city, state/region, country is preferred.');
		hilightBack(tb)
		tbValid = false;
	 }
    else {
        resetBack(tb);
    }
}

function selValidate(selName){
    select = document.getElementsByName(selName)
    select = select[0]
	 //alert(select.selectedIndex)
    if (select.selectedIndex == 0) {
        hilightBack(select)
		  //alert('it got here')
        return false;
    }
    else {
        resetBack(select);
        return true;
    }
}

function webValidate(tbName){
    tb = document.getElementsByName(tbName);
    tb = tb[0];
    url = tb.value;
	 //alert(url);
    webValid = true;
    if (url.length > 0 && url != tb.defaultValue) {
        // alert(value);
        if (url.indexOf('http://') != 0) {
            url = 'http://' + url;
        }
        xmlHttp = getXMLHttp();
        
        
        //		alert(url)
        xmlHttp.open("GET", 'ajax/checkUrl.py?url=' + url, false);
        xmlHttp.send(null);
        if (xmlHttp.responseText == '\nTrue\n') {
            webValid = true;
            
        }
        else 
            if (xmlHttp.responseText == '\nFalse\n') {
                alert('The web site address given is not valid.  Please check to make sure the address is accurate.\n\n  You may want to open the web site in a separate window and then copy and paste the address.')
                webValid = false;
            }
            else {
                alert(xmlHttp.responseText)
            }
    }
    if (webValid) {
        resetBack(tb);
    }
    else {
        hilightBack(tb);
    }
    return webValid;
    
}

function hilightBack(elem){
    elem.style.background = 'red';
    elem.style.color = 'black'
}

function resetBack(elem){
    elem.style.background = 'white';
    elem.style.color = 'black'
}

function radioValidate(name){
    radios = document.getElementsByName(name)
    radioValid = false;
    for (x=0; x<radios.length; x=x+1) {

        if (radios[x].checked) {
            radioValid = true;
        }
    }
    radio = radios[0];
    if (radioValid == false) {
        radio.parentNode.parentNode.parentNode.style.background = 'red';
    }
    else {
        radio.parentNode.parentNode.parentNode.style.background = 'white';
    }
    return radioValid;
}
