//this function includes all necessary js files for the application
function include(file)
{
	var script  = document.createElement('script');
	script.src  = file;
	script.type = 'text/javascript';
	script.defer = true;
	document.getElementsByTagName('head').item(0).appendChild(script);
}
include('../common/mrhjs.js');

function productNotification(prod)
{
	pop('notify.php?pid=' + prod,'', 500, 470);
	return false;
}
function pop(url,winName,width,height)
{
	var settings = "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no";
	var popup = window.open(url,winName,settings);
	if (window.focus) { popup.focus() }
	return false;
}
function subscribe() {
    var email = trim(document.getElementById('tbNewsletter'));
    if (email.value == '') {
        alert('Please enter your email address');
        email.focus();
        return false;
    }
    else
    {
        if(!validEmail(email.value)) {
            alert('A valid email address is required.');
            email.focus();
            return false;
        }
    }
    
    var emailwin=window.open("includes/mailer.php?address=" + email.value, "mailer_win", "top=100,left=100,height=10,width=100,scrollbars=0,menubar=0,locationbar=0,statusbar=0,resizable=0");
    emailwin.blur();
    window.focus();

    alert('Thank you for signing up for My Red House emails!');
    email.value = '';
}
function setProduct(prod,propertyTypes,dosubmit)
{
    var cont = true;
    if(propertyTypes!=null)
    {
        if(document.getElementById('propTypes')!='undefined') document.getElementById('propTypes').value = propertyTypes;
        if(propertyTypes!="")
        {
            var prop = propertyTypes.split("|");
            for(var i=0;i<prop.length;i++)
            {
                if(document.getElementById(prop[i] + prod).value=="") 
                {
                    alert('Please indicate the ' + prop[i] + " of your intended purchase.");
                    cont = false;
                    break;
                }
            }
        }
    }
    if(cont)
    {
        if(document.getElementById('qty' + prod).value<1) 
        {
            alert('Please indicate the quantity of your intended purchase.');
            cont = false;
        }
        else
        {
            if(document.getElementById('prod')!='undefined') document.getElementById('prod').value = prod;
            if(dosubmit) document.forms[0].submit();
        }
    }
}
function productsearch()
{
	var search = trim(document.getElementById('tbSearch').value);
	if(search.length<3)
	{
		alert('Please enter at least 3 characters of the product that you are searching for.');
		document.getElementById('tbSearch').focus();
	}
	else
		redirect('productsearch.php?search=' + document.getElementById('tbSearch').value);
}
function selectProperty(target,val)
{
    setValue(target,val);
}
function setValue(target,val)
{
	if(document.getElementById(target)!='undefined')
    {
        if(document.getElementById(target)!=null)
            document.getElementById(target).value=val;
    }
}
function setInvoice(val)
{
	setValue('invoice',val);
	document.forms[0].submit();
}
function validContactUs()
{
	if(trim(document.getElementById('firstName').value)=='')
    {
        alert('First Name cannot be left empty.');
        document.getElementById('firstName').focus();
        return false;
    }
    if(trim(document.getElementById('lastName').value)=='')
    {
        alert('Last Name cannot be left empty.');
        document.getElementById('lastName').focus();
        return false;
    }
    if(trim(document.getElementById('email').value)=='')
    {
        alert('Email cannot be left empty.');
        document.getElementById('email').focus();
        return false;
    }
    else
    {
        if (!validEmail(trim(document.getElementById('email').value))) 
        {
            alert('A valid email address is required.');
            document.getElementById('email').focus();
            return false;
        }
    }
    if(trim(document.getElementById('comments').value)=='')
    {
        alert('Please enter your comments.');
        document.getElementById('comments').focus();
        return false;
    }
}
function validShipping()
{
    if(trim(document.forms['frmShipping'].first_name.value)=='')
    {
        alert('Please enter the receipient\'s name.');
        document.forms['frmShipping'].first_name.focus();
        return false;
    }
	if(trim(document.forms['frmShipping'].address1.value)=='' && trim(document.forms['frmShipping'].address2.value)=='')
	{
		alert('Please enter the postage address.');
		document.forms['frmShipping'].address1.focus();
        return false;
	}
	else
	{
		if(trim(document.forms['frmShipping'].address1.value).length < 5)
		{
			alert('A valid postage address is required.');
			document.forms['frmShipping'].address1.focus();
			return false;
		}
	}
	if(trim(document.forms['frmShipping'].city.value)=='')
	{
		alert('Please enter the city of the postage address.');
		document.forms['frmShipping'].city.focus();
        return false;
	}
	if(trim(document.forms['frmShipping'].zip.value)=='')
	{
		alert('Please enter the zip/postal code of the postage address.');
		document.forms['frmShipping'].zip.focus();
        return false;
	}
    if(trim(document.forms['frmShipping'].email.value)=='')
    {
        alert('Please enter your email address.');
        document.forms['frmShipping'].email.focus();
        return false;
    }
    else
    {
        if (!validEmail(trim(document.forms['frmShipping'].email.value))) 
        {
            alert('A valid email address is required.');
            document.forms['frmShipping'].email.focus();
            return false;
        }
    }
	document.forms['frmShipping'].submit();
}
