/* language specific strings */
var str = new Object;
str['firstname'] = "Incomplete profile: Please enter your first name.";
str['lastname'] = "Incomplete profile: Please enter your last name.";
str['dob'] = "Incomplete profile: Please enter your date of birth.";
str['dob_format'] =  "Please enter your date of birth in mm/dd/yyyy format.";
str['gender'] = "Incomplete profile: Please enter your gender.";
str['email'] = "Incomplete profile: Please enter your e-mail address.";
str['email2'] = "Incomplete profile: Please confirm your e-mail address.";
str['email_format'] = "Invalid e-mail address: Please enter a valid e-mail address."
str['email_match'] = "Your e-mail addresses do not match.";
str['address'] = "Incomplete profile: Please enter your street address.";
str['city'] = "Incomplete profile: Please enter your city.";
str['state'] = "Incomplete profile: Please enter your state.";
str['zipcode'] = "Incomplete profile: Please enter your zipcode.";
str['terms'] = "You must accept the terms and conditions of service.";
str['incomplete'] = "You didn't complete the following question(s): ";
str['answers'] = ".\nPlease provide answers to these questions.";

/* all functions below here are language independent*/
function toggle(id){
	//alert(document.getElementById(id).className);
	if (document.getElementById(id).className=='faqa'){
		document.getElementById(id).className='faqa_display';
	} else {
		document.getElementById(id).className='faqa';
	}
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function validateSurvey(form, questionstring, labelstring) {
	var choices, first = 0;
	var qcount, sub, n = 0;
	var flag = 0;
	var last = 0;
	var msg = 0;
	var questions = questionstring.split(":");
	var labels = labelstring.split(":");
	for (var i=0; i < questions.length; i++){ //questions.length
		choices = eval("document.getElementsByName('" + questions[i] + "')");
		if (choices[0] == undefined) {
			flag = 1;
		} else {
			if (choices[0].type == 'radio') flag = validateChoices(choices);
			if (choices[0].type == 'checkbox') flag = validateChoices(choices);
			if (choices[0].type == 'select-one') flag = choices[0].selectedIndex;
			if (choices[0].type == 'text') flag = choices[0].value.trim();
            if (choices[0].type == 'textarea') flag = choices[0].value.trim();
		}
		if (!flag) {
			if (msg) {
				if (last == labels[i]) {
					//remove the sequence ", xx (2 items)" and replace with ", xx (3 items)"
					msg = msg.replace(/\,[^\,]*$/, ", " + labels[i]);
					//msg = msg + ", " + labels[i];
				} else {
					msg = msg + ", " + labels[i];
				}
			} else {
				msg = labels[i];
				if (!first) {
                    first = choices[0];
                    var coords = findPos(document.getElementById(questions[i] + "_target"));
                }
			}
			last = labels[i];
		}
	}
	if (msg) {
		alert(str['incomplete'] + msg + str['answers']);
		first.focus();
        window.scrollTo(0,coords[1]);
		return false;
	} else {
		return true;
	} 
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function validateChoices(choices) {
	for (var j=0; j < choices.length; j++){
		if (choices[j].checked) return true;
	}
	return false;
}

function validateEmail(str) {
	if (str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) { 
            return true; 
	} 
	return false; 
} 

function validatePhone(str) {
	if (str.match(/^\(*\d\d\d\)*-*\d\d\d-*\d\d\d\d$/) ) {
		return true; 
	} 
	return false; 
} 

function validateDOB(str) {
	if (str.match(/^\d\d\/\d\d\/\d\d\d\d$/) ) {
        var mydob = str.split("/");
		if (Number(mydob[0]) > 0 && Number(mydob[0]) < 13 && Number(mydob[1]) > 0 && Number(mydob[1]) < 32 && Number(mydob[2]) > 1908) {
            return true; 
        }
	} 
	return false; 
} 

function validateZipcode(str) {
	if (str.match(/^\d\d\d\d\d-*\d\d\d\d$/) ) {
		return true; 
	} 
	return false; 
} 

function validateProfile(form, questionstring, labelstring) {
	var choices;
	vmsg = 0;
	if (form.firstname.value == '') {
		alert(str['firstname']);
		form.firstname.focus();
		return false;
	}
	if (form.lastname.value == '') {
		alert(str['lastname']);
		form.lastname.focus();
		return false;
	}
	if (form.dob.value == '') {
		alert(str['dob']);
		form.dob.focus();
		return false;
	}
	if (validateDOB(form.dob.value) == false) {
		alert(str['dob_format']);
		form.dob.focus();
		return false;
	}
	choices = document.getElementsByName('gender');
	if (validateChoices(choices) == false){
		alert(str['gender']);
		form.gender[0].focus();
		return false;
	}	
	if (form.email.value == '') {
		alert(str['email']);
		form.email.focus();
		return false;
	}
	if (form.email2.value == '') {
		alert(str['email2']);
		form.email2.focus();
		return false;
	}
	if (validateEmail(form.email.value) == false) {
		alert(str['email_format']);
		form.email.focus();
		return false;
	}
	if (form.email.value != form.email2.value) {
		alert(str['email_match']);
		form.email.focus();
		return false;
	}	
	if (form.address.value == '') {
		alert(str['address']);
		form.address.focus();
		return false;
	}
	if (form.city.value == '') {
		alert(str['city']);
		form.city.focus();
		return false;
	}
	if (form.state.value == '') {
		alert(str['state']);
		form.state.focus();
		return false;
	}
	if (form.zipcode.value == '') {
		alert(str['zipcode']);
		form.zipcode.focus();
		return false;
	}
	var temp = validateSurvey(form, questionstring, labelstring);
    if (temp == false) return false;
    choices = document.getElementsByName('terms');
    if (validateChoices(choices) == false){
        alert(str['terms']);
        form.terms.focus();
        return false;
    }
    return true;
}
