var wndDialog = null;

function getRadioValue(formname, elname) {
    var result = '';
    for(var i = 0; i < document.forms.length; i++) {
        if(document.forms[i].id == formname) {
            var fobj = document.forms[i];
            var inputs = fobj.getElementsByTagName('input');
            for(var j = 0; j < inputs.length; j++) {
                if((inputs[j].getAttribute('type') == 'radio') && (inputs[j].id == elname)) {
                    var robj = new Array(inputs[j]);
                    for(var k = 0; k < robj.length; k++) {
                        if(robj[k].checked == true) {
                            result = robj[k].value;
                            break;
                        }
                    }
                }
            }
        }
    }
    return(result);
}    

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      	return false;
   } else {
   		return(true);
   }
}

function checkNewUser() {

    //try {
        var errmsg = '';
        var fstname = document.getElementById('fst_name').value;
        var lastname = document.getElementById('last_name').value;
        var username = document.getElementById('newuser').value;
        var pwd = document.getElementById('newpwd').value;
        var confpwd = document.getElementById('confpwd').value;
        var email = document.getElementById('email').value;
        var gender = document.getElementById('gender_flg').options[document.getElementById('gender_flg').selectedIndex].value;
        var day = document.getElementById('dob_day').options[document.getElementById('dob_day').selectedIndex].value;
        var month = document.getElementById('dob_month').options[document.getElementById('dob_month').selectedIndex].value;
        var year = document.getElementById('dob_year').options[document.getElementById('dob_year').selectedIndex].value;
        var acct = document.getElementById('user_type_cd').options[document.getElementById('user_type_cd').selectedIndex].value;
        var country = document.getElementById('country_id').options[document.getElementById('country_id').selectedIndex].value;
        var region = document.getElementById('region_id').options[document.getElementById('region_id').selectedIndex].value;
        var city = document.getElementById('city_id').options[document.getElementById('city_id').selectedIndex].value;
        var newsletter = (document.getElementById('newsletter_flg').checked == true);
        var terms = (document.getElementById('terms').checked == true);
        var usertype = document.getElementById('user_type_cd').options[document.getElementById('user_type_cd').selectedIndex].value;
        var code = document.getElementById('code').value;
        
        if(usertype != '1') {
	        var company = document.getElementById('company').value;
	        var street = document.getElementById('street').value;
	        var postal = document.getElementById('postal').value;
	        var phone = document.getElementById('phone').value;
	        var fax = document.getElementById('fax').value;
	        var tollfree_ph = document.getElementById('tollfree_ph').value;
	        var public_email = document.getElementById('public_email').value;
	        var subcategory = "";
	        var category = document.getElementById('category_cd').options[document.getElementById('category_cd').selectedIndex].value;
	        if(category != "") {
	        	subcategory = document.getElementById('sub_category_cd').options[document.getElementById('sub_category_cd').selectedIndex].value;
			} else {
				subcategory = "";
			}	        
	        if(company == '') errmsg += 'No company name was entered\n';
	        if(category == '') errmsg += 'No business category was selected\n';
	    }
        
        if(fstname == '') errmsg += 'No information was entered in the first name field\n';
        if(lastname == '') errmsg += 'No information was entered in the last name field\n';
        if(username == '') errmsg += 'No information was entered in the username field\n';
        if(pwd == '') errmsg += 'No information was entered in the password field\n';       
        if(pwd.length < 6) errmsg += 'The password must be 6 characters or more in length\n';
        if(pwd != confpwd) errmsg += 'The password and repeated password do not agree\n';
        if(email == '') {
        	errmsg += 'No information was entered in the e-mail address field\n';
		} else {
			if(!validateEmail(email)) {
				errmsg += 'Invalid e-mail address\n';
			}
		}
        
        if(usertype == '1') {
            if(day == '') errmsg += 'No birthday date was selected\n';
            if(month == '') errmsg += 'No birthday month was selected\n';
            if(year == '') errmsg += 'No birthday year was selected\n';
            if((day != '') && (month != '') && (year != '')) {
                try {
                    if(parseInt(month) == 2) {
                        if((parseInt(year) % 4) == 0) {
                            if(parseInt(day) > 29) errmsg += 'An invalid birthday was selected\n';
                        } else {
                            if(parseInt(day) > 28) errmsg += 'An invalid birthday was selected\n'; 
                        }
                    } else {
                        if((parseInt(month) == 4) || (parseInt(month) == 6) || (parseInt(month) == 9) || (parseInt(month) == 11)) {
                            if(parseInt(day) > 30) {
                                errmsg += 'An invalid birthday was selected\n';
                            }
                        }
                    }
                    var bday = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
                    if(!bday) errmsg += 'An invalid birthday was selected\n';    
                } catch(e) {
                    errmsg += 'An invalid birthday was selected\n';
                }
            }
        }
        
        if(acct == '') errmsg += 'No account type was selected\n';
        if(country == '') errmsg += 'No country was selected\n';
        if(region == '') errmsg += 'No region was selected\n';
        if(city == '') errmsg += 'No city was selected\n';
        if(!terms) errmsg += 'You must agree to the myeCarousel terms and conditions\n';
        if(code == '') {
        	errmsg += 'No Verification Code was entered\n';
		} else {
			if(code.length != 6) {
				errmsg += 'Verification Code is not six characters long\n';
			}
		}        
        
        if(errmsg == '') {
            return(true);
        } else {
            alert(errmsg);
            return(false);
        }
    //} catch(e) {
    //    alert(e.message);
    //}

}

function setMessageFilters() {
    var message_search = document.getElementById('search').value;
    var obj = document.getElementById('message_view');
    var message_view = 'Unread';
    if(obj != null) message_view = obj.options[obj.selectedIndex].value;
    var query = 'index3.php?view=user_messages&action=display&message_view=' + 
        message_view + '&search=' + escape(message_search);
    window.location = query;
}

function goHotPano(countryid, regionid, cityid, panoid) {
    var ts = new Date().getTime();
    var query = "index.php?view=ajax&action=setcountry&country=" + countryid +
                    "&region=" + regionid + "&city=" + cityid + "&ts=" + ts;
    $.get(query, function(data) {
        var items = data.split('~');
        var html = "<img src='images/flags/png/" + items[1] + "' border=0>" +
            " " + items[2] + " <font id='country-change'>(<a href='#' id='" +
            items[0] + "' onclick='changeCountry();'>Change Country</a>)" +
            "</font>";    
        $("#country-text").attr('innerHTML', html);
        var query = "index.php?view=user_panoramas&action=display&id=" + panoid;
        window.location = query;    
    }, 'text');
}

function showDialog(tag) {
    wndDialog = dhtmlwindow.open("wndDialog", "iframe", "index.php?view=popup&name=show_dialog&id=" + tag, "Help", "width=600px,height=430px,resize=1,scrolling=0,center=1", "recal");
}

function showlink(panoid, view) {
	wndDialog = dhtmlwindow.open("wndDialog", "iframe", "index.php?view=popup&name=show_link&id=" + panoid + "&v=" + view, "Panorama Link", "width=400px,height=380px,resize=1,scrolling=0,center=1", "recal");
}

