// JavaScript Document
function fnSubmit() {		
	//alert('1');
	if (field_validation()) {
		frm.submit();
	}	
}

function getTimeStringID() {
	var now;
		
	now = new Date();
	return now.getTime();
}

function trim(str) {
	return str.replace(/\s/g, "");
}

function getFileSize(path)
{
	var img = new Image();
	img.dynsrc = path;
	return img.fileSize;
}

function file_err_refresh()
{
 var url = document.all.addr.value;
 location.replace(url);
}


function FileSizeValidation(path)
{
	//alert('hello');
	var str_tmp = path.split('.');
	var int_len;
	if (str_tmp.length > 0) {
		int_len = str_tmp.length - 1;
		if (str_tmp[int_len].toLowerCase() != 'jpg' && str_tmp[int_len].toLowerCase() != 'gif' ) {
			alert('JPG or GIF image Only');
			//file_err_refresh();		
			return;
		}
	}
	else {
		alert('JPG or GIF image Only');
		//file_err_refresh();	
		return;
	}
	//alert(str_tmp[str_tmp.length-1]);
	
	
	var lng_file_size = getFileSize(path);
	//alert(lng_file_size);
	if (getFileSize(path) > 200000) {
		alert('You can upload up to 2MB image');
		//file_err_refresh();
	}
}

function checkRadio(radio_btn) {
	var len = radio_btn.length;
	for( i=0; i < len; i++ ) {
		if( radio_btn[i].checked == true ) {
			return radio_btn[i].value;
		}
	}
	return '';	   	
}
	
function field_validation() {
	getTimeStringID();
	//alert(frm.txt_pic.value);
	var str_fname;
	var str_lname;
	var str_add1;
	var str_city;
	var str_state;
	var str_post;
	var str_country;
	var str_email;
	var str_pic;
	var str_date_id;
		
	var str_tmp;
	var int_cnt;
		
	str_fname = frm.txt_fname.value;
	str_lname = frm.txt_lname.value;
	str_add1 = frm.txt_add1.value;
	str_city = frm.txt_city.value;
	str_state = frm.txt_state.value;
	str_post = trim(frm.txt_post.value);
	str_country = frm.txt_country.value;
	str_email = trim(frm.txt_email.value);
		
	//********** Field Validation *************
	if (str_fname == '') 
	{
		alert('Please enter your first name.');
		frm.txt_fname.focus();
		return false;
	}
	else if (str_lname == '')
	{
		alert('Please enter your last name.');
		frm.txt_lname.focus();
		return false;
	}
	else if (str_add1 == '')
	{
		alert('Please enter your street address.');
		frm.txt_add1.focus();
		return false;
	}
	else if (str_city == '')
	{
		alert('Please enter your City.');
		frm.txt_city.focus();
		return false;
	}
	else if (str_state == '')
	{
		alert('Please enter your state.');
		frm.txt_state.focus();
		return false;
	}
	else if (str_post == '')
	{
		alert('Please enter your post code.');
		frm.txt_post.focus();
		return false;
	}
	else if (str_country == '')
	{
		alert('Please enter your country.');
		frm.txt_country.focus();
		return false;
	}
	else if (str_email.search(/(\S+)@(\S+)\.(\S+)/) == -1)
 	{
		alert("Please enter a valid email address.");
		frm.txt_email.focus();
		return false;
	}
 	else
	{
		frm.hdn_q1.value = frm.txt_q1.value;
		frm.hdn_q2.value = frm.cbo_q2.value;
		frm.hdn_q3.value = checkRadio(frm.rdo_q3);
		frm.hdn_q4.value = checkRadio(frm.rdo_q4);
		frm.hdn_q5.value = checkRadio(frm.rdo_q5);
		frm.hdn_q6.value = checkRadio(frm.rdo_q6);
		frm.hdn_q7.value = frm.txt_q7.value;
		frm.hdn_q8_rogaine.value = checkRadio(frm.rdo_q8_rogaine);
		frm.hdn_q8_saw.value = checkRadio(frm.rdo_q8_saw);
		frm.hdn_q8_propecia.value = checkRadio(frm.rdo_q8_propecia);
		frm.hdn_q8_other.value = checkRadio(frm.rdo_q8_other);
		frm.hdn_q9.value = frm.txt_q9.value;
		
		frm.hdn_fname.value = str_fname;
		frm.hdn_lname.value = str_lname;
		if (frm.chk_mailing.checked == true)
		{
			frm.hdn_is_mailing.value = 'Yes';
		}
		else
		{
			frm.hdn_is_mailing.value = 'No';
		}
		frm.hdn_add1.value = str_add1;
		frm.hdn_add2.value = frm.txt_add2.value;
		frm.hdn_city.value = str_city;
		frm.hdn_state.value = str_state;
		frm.hdn_post.value = str_post;
		frm.hdn_city.value = str_city;
		frm.hdn_country.value  = str_country;
		frm.hdn_phone.value = frm.txt_ph.value;
		frm.hdn_email.value  = str_email;
		if (frm.chk_newsletter.checked == true)
		{
			frm.hdn_is_newsletter.value = 'Yes';
		}
		else
		{
			frm.hdn_is_newsletter.value = 'No';
		}
		frm.hdn_contact_preference.value  = checkRadio(frm.rdo_contact_preference);
		frm.hdn_pic.value  = frm.txt_pic.value;
		frm.hdn_date_id.value = getTimeStringID();	
		return true;		
	}
}

