/* visual and help box control */

fields_info = new Array();
fields_info['name_first'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>First Name</b><br /><br />Please enter your first name only.';
fields_info['name_last'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Last Name</b><br /><br />Please enter your last name only.';
fields_info['company'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Company</b><br /><br />Please enter the name of the company or organization that you are currently associated with.';
fields_info['address'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Street Address</b><br /><br />Please enter your street address in the first line and use the second line to enter any additional address information such as suite or building.';
fields_info['phone'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Phone</b><br /><br />Please enter a valid ten digit phone number that we can contact you at during business hours.';
fields_info['email'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>E-Mail</b><br /><br />Please enter a valid and current e-mail address.';
fields_info['title'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Title</b><br /><br />Please enter your current title.';
fields_info['city_state_zip'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>City, State Zip</b><br /><br />Please enter all addtional address information for your city, state and zip code.';
fields_info['ptp'] = '<img src="/images/plus.gif" class="info_img" /> &nbsp;<b>Pass the Plus</b><br /><br />Select this box to receive our free monthly e-newsletter highlighting events, activities, news and communities within the Cleveland Plus region.';

var frozen = 0;

function row_highlight(element, info_name) {
if(!frozen) {
element.className = 'highlight';
document.getElementById('help_info_default').style.display = 'none';
document.getElementById('help_info').innerHTML = fields_info[info_name];
document.getElementById('help_info').style.display = 'block';
document.getElementById('info_box').style.top = (element.offsetTop + 127) + 'px';
}
}

function row_unhighlight(element) {
if(!frozen) {
element.className = '';
document.getElementById('help_info').style.display = 'none';
document.getElementById('help_info_default').style.display = 'block';
}
}

function row_freeze(element, info_name) {
row_highlight(element, info_name);
frozen = 1;
}

function row_unfreeze(element, field) {
frozen = 0;
row_unhighlight(element);

if(field) {
check_graphic(field);
}
}

function check_graphic(field) {
switch(field.name) {
case 'city':
case 'state':
case 'zip':
if(document.getElementById('city').value != '' && document.getElementById('state').value != '' && document.getElementById('zip').value != '') {
document.getElementById('img_csz').src = '/images/plus.gif';
} else {
document.getElementById('img_csz').src = '/images/blueplus.gif';
}
break;
case 'other_topic':
validate_topics(true);
break;
default:
if(field.value != '') {
document.getElementById('img_' + field.name).src = '/images/plus.gif';
} else {
document.getElementById('img_' + field.name).src = '/images/blueplus.gif';
}
break;
}
}

/* form validation */

function validate_form() {
var the_form = document.getElementById('form');
var txt = 'Please correct the following before continuing:';
var valid = true;

var bad = new Array();
bad['name'] = false;
bad['addess'] = false;
bad['proposal'] = 0;
bad['photo'] = 0;


for(var i = 0; i < the_form.elements.length; i++) {
switch(the_form.elements[i].name) {
case 'name_first':
case 'name_last':
if(!bad['name'] && the_form.elements[i].value == '') {
txt += '\n - please enter your full name';
bad['name'] = true;
valid = false;
}
break;
case 'street_one':
case 'city':
case 'state':
case 'zip':
if(!bad['address'] && the_form.elements[i].value == '') {
txt += '\n - please enter your full address';
bad['address'] = true;
valid = false;
}
break;
case 'company':
if(the_form.elements[i].value == '') {
txt += '\n - please enter your company';
valid = false;
}
break;
case 'phone':
if(the_form.elements[i].value == '' || !checkInternationalPhone(the_form.elements[i].value)) {
txt += '\n - please enter a valid phone number';
valid = false;
}
break;
/* case 'email':
if(the_form.elements[i].value == '' || !check_email(the_form.elements[i].value)) {
txt += '\n - please enter a valid email';
valid = false;
}
break; */
/* case 'website':
var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

if(the_form.elements[i].value.indexOf('http://') !== 0 && the_form.elements[i].value.indexOf('https://') !== 0) {
the_form.elements[i].value = 'http://' + temp;
}

if(!v.test(elements[i].value)) {
txt += '\n - please enter a valid website';
valid = false;
}
break; */
case 'bio':
if(the_form.elements[i].value == '') {
txt += '\n - please include a bio';
valid = false;
}
break;
/* case 'references':
if(the_form.elements[i].value == '') {
txt += '\n - please include information for at least one reference';
valid = false;
}
break; */
case 'attach1':
case 'attach2':
case 'attach3':
case 'attach4':
case 'attach5':
if(the_form.elements[i].value == '') {
bad['proposal']++;
}
break;

case 'attach6':
case 'attach7':
if(the_form.elements[i].value == '') {
bad['photo']++;
}
break; 
}
}

if(!validate_opportunities()) {
txt += '\n - please select at least one opportunity';
valid = false;
}

if(!validate_topics()) {
txt += '\n - please select at least one topic';
valid = false;
}

if(bad['proposal'] > 4) {
txt += '\n - please include at least one proposal';
valid = false;
}

if(bad['photo'] > 1) {
txt += '\n - please include at least one photo';
valid = false;
}

if(valid) {
// var msg = 'Have you reviewed all of the information you are including? Please make sure that all of the information is filled out and correct because you may only submit once.\n\nClick "Ok" to submit this form.\nClick "Cancel" to go back and review your information.';

// if(confirm(msg)) {
document.getElementById('submit_button').disabled = true;
document.getElementById('submit_button').value = 'please wait...';

return true;
// } else {
//     return false;
// }
} else {
alert(txt);

return false;
}
}

function validate_opportunities(graphic) {
for(var i = 0; i < opportunities.length; i++) {
if(document.getElementById(opportunities[i]).checked) {
if(graphic) {
document.getElementById('img_opportunities').src = '/images/plus.gif';
}

return true;
}
}

if(graphic) {
document.getElementById('img_opportunities').src = '/images/blueplus.gif';
}

return false;
}

function validate_topics(graphic) {
for(var i = 0; i < topics.length; i++) {
if(document.getElementById(topics[i]).checked) {
if(graphic) {
document.getElementById('img_topics').src = '/images/plus.gif';
}

return true;
}
}

if(document.getElementById('other_topic').value != '') {
if(graphic) {
document.getElementById('img_topics').src = '/images/plus.gif';
}

return true;
}

if(graphic) {
document.getElementById('img_topics').src = '/images/blueplus.gif';
}

return false;
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
// Maximum no of digits in an international phone no.
var maxDigitsInIPhoneNumber = 10;

function isInteger(s) {
var c = null;

for(var i = 0; i < s.length; i++) {
// Check that current character is number.
c = s.charAt(i);

if(((c < "0") || (c > "9"))) {
return false;
}
}

// All characters are numbers.
return true;
}

function stripCharsInBag(s, bag) {
var c = null;
var returnString = '';

// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for(var i = 0; i < s.length; i++) {
// Check that current character isn't whitespace.
c = s.charAt(i);

if (bag.indexOf(c) == -1) {
returnString += c;
}
}

return returnString;
}

function checkInternationalPhone(strPhone) {
var s = stripCharsInBag(strPhone,validWorldPhoneChars);

return (isInteger(s) && s.length >= minDigitsInIPhoneNumber && s.length <= maxDigitsInIPhoneNumber);
}

function check_email(str) {
var at = '@';
var dot = '.';
var lat = str.indexOf(at);
var lstr = str.length;
var ldot = str.indexOf(dot);

if(str.indexOf(at) == -1) {
return false;
}

if(str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
return false;
}

if(str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
return false;
}

if(str.indexOf(at, (lat+1)) != -1) {
return false;
}

if(str.substring(lat-1, lat) == dot || str.substring(lat+1, lat+2) == dot) {
return false;
}

if(str.indexOf(dot, (lat+2)) == -1) {
return false;
}

if(str.indexOf(' ') != -1) {
return false;
}

return true;
}

function checkwords(cur) {
var maxwords = 150;
var tmp = cur.value.split(' ');

if(tmp.length > maxwords) {
return true;
} else {
return false;
}
}

/* utilites */

function in_array(needle, haystack, strict) {
var i = null;

if(strict) {
for(i in haystack) {
if(haystack[i] === needle) {
return true;
}
}
} else {
for(i in haystack) {
if(haystack[i] == needle) {
return true;
}
}
}

return false;
}
