
function popUp(URL) {

	day = new Date();
	id = day.getTime();
	window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=380,height=400');

}

function getnavndx() {
	if (typeof(window['ni']) != 'undefined') return window['ni'];
	return 0;
	var val = getqval('ni');
	if (val == '') return -1;
	else return parseInt(val);
}
function getqval(key) {
	var loc = document.location.href;
	var fnd = key + '=';
   	var sdx = loc.indexOf(fnd);
	var val = '';
   	if (sdx > 0) {
   		var edx = loc.indexOf('&', sdx);
		if (edx > 0)
			val = loc.substring(sdx + fnd.length, edx);
		else {
			edx = loc.indexOf('#');
			if (edx > 0)
				val = loc.substring(sdx + fnd.length, edx);
			else
				val = loc.substring(sdx + fnd.length);
		}
	}
	return val.split('+').join(' ');
}
function init() {
	navndx = getnavndx() - 1;
	subnavs = document.getElementsByTagName("subnav");
	for (var i = subnavs.length; --i >= 0; ) {
		//alert(i);
		if (navndx == i) {
			var img = null;
			if (document.all) {
				var img = subnavs[i].parentNode.parentNode.previousSibling.firstChild.firstChild.firstChild;
			} else {
				var tr = subnavs[i].parentNode.parentNode;
				var tr2 = tr;
				do {
					tr2 = tr2.previousSibling;
				} while (!tr2.tagName || tr2.tagName.toLowerCase() != "tr")
				var imgs = tr2.getElementsByTagName("img");
				if (imgs.length > 0) img = imgs[0];
			}
			if (img) img.src = img.src.substring(0, img.src.length - 4) + "-on.gif";
		}
		if (navndx != i)
			subnavs[i].parentNode.parentNode.style.display = "none";
	}
	//var pagetitle = getqval('pt');
	//alert(pagetitle);
	//document.title = 'Melatrol Sleeping Aid - ' + pagetitle;
	//document.getElementById('title').innerHTML = pagetitle;
}
/* End Melatrol Custom
====================================================================== */
function GetMyCookieVal(offset) {
    var endstr = document.cookie.indexOf ( ";", offset ) ;
    if ( endstr == -1 ) endstr = document.cookie.length ;
    return unescape(document.cookie.substring(offset, endstr) ) ;
}
function GetMyCookie(name) {
    var arg = name + "=" ;
    var alen = arg.length ;
    var i = 0 ;
    var clen = document.cookie.length ;

    while (i < clen) {
        var j = i + alen
        if ( document.cookie.substring(i,j) == arg ) {
            var v =  GetMyCookieVal (j) ;
            return v ;
        }
        i = document.cookie.indexOf (" ", i) + 1 ;
        if ( i == 0 ) break ;
	} return null ;
}
function read_querystring()
   {
   var a_out = new Object();
   //a_out[0] = "";
   var s_loc = String(location.href);
 	//alert(s_loc);
   if(s_loc.indexOf('?')>0)
     {
     var p;
     var s_query = s_loc.substr(s_loc.indexOf('?')+1);
     var a_query = s_query ? s_query.split('&') : new Array();

     for(var i=0; i<a_query.length; i++)
       {
       p = a_query[i].split('=');
       a_out[p[0]] = p[1].replace(/\+/g, ' ');
     }
   }
    return a_out;

}




function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_home_over = newImage("images/nav_home-over.gif");
		nav_adout_us_over = newImage("images/nav_adout_us-over.gif");
		nav_how_it_works_over = newImage("images/nav_how_it_works-over.gif");
		ingredients_over = newImage("images/ingredients-over.gif");
		nav_clinical_studies_over = newImage("images/nav_clinical_studies-over.gif");
		nav_faq_over = newImage("images/nav_faq-over.gif");
		nav_testimonials_over = newImage("images/nav_testimonials-over.gif");
		nav_contact_us_over = newImage("images/nav_contact_us-over.gif");
		nav_order_over = newImage("images/nav_order-over.gif");
		preloadFlag = true;
	}
}






function popHold() {

		newwin = window.open('pophold.php','','height=350,width=350,scrollbars=0,toolbar=0,status=0');

		newwin.focus();

	}
function openpopupphermone()
	{
	var popurl="phermone_offer.php"
	window.open(popurl,"","width=410,height=510,scrollbars,")
	}

function tos()
	{
	var popurl="tos.php"
	window.open(popurl,"","width=600,height=500,scrollbars,")
	}
function openpopup()
	{
	var popurl="tos.php"
	window.open(popurl,"","width=600,height=500,scrollbars,")
	}
function openpopupphone()
	{
	var popurl="http://www.securebiller.com/phone/index.php?aid=#aid#"
	window.open(popurl,"","width=375,height=400,")
	}
function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
	offset = document.cookie.indexOf(search)
	if (offset != -1) {
	offset += search.length
	end = document.cookie.indexOf(";", offset);
	if (end == -1)
	end = document.cookie.length;
	returnvalue=unescape(document.cookie.substring(offset, end))
	}
	}
	return returnvalue;
	}

function loadornot()
	{
	if (get_cookie('poppedup')=='')
	{
	//setTimeout("popUnder()", 8000);
//	openpopup();
//document.cookie="poppedup=yes"
	}
	}
	loadornot()

function popUnder() {

	url = "../ata/index.htm";

	newwin = Window.open(url,'','');

	//newwin.blur();

	self.focus();
	setTimeout("self.focus()",300);

}
	var day="";
	var month="";
	var myweekday="";
	var year="";
	mydate = new Date();
	myday = mydate.getDay();
	mymonth = mydate.getMonth();
	myweekday= mydate.getDate();
	weekday= myweekday;
	myyear= mydate.getYear();
	year = myyear
	if(myday == 0)
	day = " Sunday, "
	else if(myday == 1)
	day = " Monday, "
	else if(myday == 2)
	day = " Tuesday, "
	else if(myday == 3)
	day = " Wednesday, "
	else if(myday == 4)
	day = " Thursday, "
	else if(myday == 5)
	day = " Friday, "
	else if(myday == 6)
	day = " Saturday, "
	if(mymonth == 0) {
	month = "January "}
	else if(mymonth ==1)
	month = "February "
	else if(mymonth ==2)
	month = "March "
	else if(mymonth ==3)
	month = "April "
	else if(mymonth ==4)
	month = "May "
	else if(mymonth ==5)
	month = "June "
	else if(mymonth ==6)
	month = "July "
	else if(mymonth ==7)
	month = "August "
	else if(mymonth ==8)
	month = "September "
	else if(mymonth ==9)
	month = "October "
	else if(mymonth ==10)
	month = "November "
	else if(mymonth ==11)
	month = "December "

function openpopuptos()
	{
	var popurl="tos.php"
	window.open(popurl,"","width=600,height=500,scrollbars,")
	}

function openpopupcopyright()
	{
	var popurl="copyright.php"
	window.open(popurl,"","width=600,height=500,scrollbars,")
	}

function fillbill()
	{
	if ( document.form.sameshipping.checked == true )
	{
	document.form.elements['formData[x_First_Name]'].value 		= document.form.elements['formData[x_Ship_to_First_Name]'].value;
	document.form.elements['formData[x_Last_Name]'].value  		= document.form.elements['formData[x_Ship_to_Last_Name]'].value;
	document.form.elements['formData[x_Email]'].value 	 	= document.form.elements['formData[x_Ship_to_Email]'].value;
	document.form.elements['formData[x_Address]'].value 		= document.form.elements['formData[x_Ship_to_Address]'].value;
	document.form.elements['formData[x_City]'].value 	 	= document.form.elements['formData[x_Ship_to_City]'].value;
	document.form.elements['formData[x_State]'].value 	 	= document.form.elements['formData[x_Ship_to_State]'].value;
	document.form.elements['formData[x_Zip]'].value 	 	= document.form.elements['formData[x_Ship_to_Zip]'].value;
	document.form.elements['formData[x_Country]'].selectedIndex = document.form.elements['formData[x_Ship_to_Country]'].selectedIndex;
	document.form.elements['formData[x_Country]'].selectedIndex = document.form.elements['formData[x_Ship_to_Country]'].selectedIndex;
	}
	}

function autofillbill()
	{
	document.form.elements['formData[x_First_Name]'].value 		= document.form.elements['formData[x_Ship_to_First_Name]'].value;
	document.form.elements['formData[x_Last_Name]'].value  		= document.form.elements['formData[x_Ship_to_Last_Name]'].value;
	document.form.elements['formData[x_Email]'].value 	 	= document.form.elements['formData[x_Ship_to_Email]'].value;
	document.form.elements['formData[x_Address]'].value 		= document.form.elements['formData[x_Ship_to_Address]'].value;
	document.form.elements['formData[x_City]'].value 	 	= document.form.elements['formData[x_Ship_to_City]'].value;
	document.form.elements['formData[x_State]'].value 	 	= document.form.elements['formData[x_Ship_to_State]'].value;
	document.form.elements['formData[x_Zip]'].value 	 	= document.form.elements['formData[x_Ship_to_Zip]'].value;
	selCountry  = document.form.elements['formData[x_Ship_to_Country]']
	document.form.elements['formData[x_Country]'].value = selCountry.options[selCountry.selectedIndex].value;
	}

function js_in_array(the_needle, the_haystack){
        var the_hay = the_haystack.toString();
        if(the_hay == ''){
            return false;
        }
        var the_pattern = new RegExp(the_needle, 'g');
        var matched = the_pattern.test(the_haystack);
        return matched;
    }

function js_get_index_at(the_needle, the_haystack) {
    for(var i=0; i<the_haystack.length; i++)
    {
        if ( the_haystack[i] == the_needle ) {
        	return i+1;
        }

    }
    return 0;
}

function confRemoval(name) {

    conf = confirm("Are you sure you wish to delete this item? " + name)

    return conf;

}

function jsValidateEmailAddress(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false
	}

	return true

}

function club_discount_check() {

    row = document.getElementById('discount_row');
    chk = document.getElementById('discount_check');

	if ( row == undefined || chk == undefined ) {
		return;
	}

    if ( chk.checked ) {
        row.className 	= 'blk-element';
    }
    else {
        row.className 	= 'blk-off';
    }

}

function add_ins_check() {

    row = document.getElementById('ins_row');
    chk = document.getElementById('ins_check');

	if ( row == undefined || chk == undefined ) {
		return;
	}

    if ( chk.checked ) {
        row.className 	= 'blk-element';
    }
    else {
        row.className 	= 'blk-off';
    }

}

function js_in_array(the_needle, the_haystack){
        var the_hay = the_haystack.toString();
        if(the_hay == ''){
            return false;
        }
        var the_pattern = new RegExp(the_needle, 'g');
        var matched = the_pattern.test(the_haystack);
        return matched;
    }

function js_get_index_at(the_needle, the_haystack) {
    for(var i=0; i<the_haystack.length; i++)
    {
        if ( the_haystack[i] == the_needle ) {
        	return i+1;
        }

    }
    return 0;
}

function adjustState() {

		/// COUNTRY VALUES
		var bill_country	= document.getElementById("brazil_input_bill");
		var ship_country	= document.getElementById("brazil_input");

		/// ROWS
		var bill_state_text_row	= document.getElementById("bill_state_text_row");
		var bill_state_drop_row	= document.getElementById("bill_state_drop_row");
		var bill_state_text_row_can	= document.getElementById("bill_state_text_row_can");
		var bill_state_drop_row_can	= document.getElementById("bill_state_drop_row_can");


		var ship_state_text_row	= document.getElementById("ship_state_text_row");
		var ship_state_drop_row	= document.getElementById("ship_state_drop_row");
		var ship_state_text_row_can	= document.getElementById("ship_state_text_row_can");
		var ship_state_drop_row_can	= document.getElementById("ship_state_drop_row_can");

		/// TEXT VALS
		var bill_state_text	= document.getElementById("bill_state_text");
		var ship_state_text	= document.getElementById("ship_state_text");

		/// DROP VALS
		var bill_state_drop	= document.getElementById("bill_state_drop");
		var ship_state_drop	= document.getElementById("ship_state_drop");
		var bill_state_drop_can	= document.getElementById("bill_state_drop_can");
		var ship_state_drop_can	= document.getElementById("ship_state_drop_can");

        var ship_find_state = '';
        var bill_find_state = '';

		/// SHIP HANDLE - US
		if ( ship_country.options[ship_country.selectedIndex].value == 'US' ) {

            /// ATTEMPT TO FIND THE MATCHING DROP STATE
            if ( ship_state_text.value != undefined && ship_state_text.value.length >= 2 ) {
                ship_find_state = ship_state_text.value.substring(0,2);
            }

            /// TEXT OFF
            /// DROP ON
            ship_state_text_row.className 	    = 'ordertextOFF';
            ship_state_drop_row_can.className 	= 'ordertextOFF';
            ship_state_drop_can.disabled		= true;
            //ship_state_text.disabled		= true;

            ship_state_drop_row.className	= 'ordertext';
            ship_state_drop.disabled		    = false;

            /// SET DROP
            if ( ship_state_drop.selectedIndex < 1 ) {
                ship_state_drop.selectedIndex	= js_get_index_at(ship_find_state.toUpperCase(),the_states);
            }

		}
		else if ( ship_country.options[ship_country.selectedIndex].value == 'CA' ) {

            /// ATTEMPT TO FIND THE MATCHING DROP STATE
            if ( ship_state_text.value != undefined && ship_state_text.value.length >= 2 ) {
                ship_find_state = ship_state_text.value.substring(0,2);
            }

            /// TEXT OFF
            /// DROP ON
            ship_state_text_row.className 	= 'ordertextOFF';
            ship_state_drop_row.className 	= 'ordertextOFF';
            ship_state_drop.disabled		= true;
            //ship_state_text.disabled		= true;

            ship_state_drop_row_can.className	= 'ordertext';
            ship_state_drop_can.disabled		= false;

            /// SET DROP
            if ( ship_state_drop_can.selectedIndex < 1 ) {
                ship_state_drop_can.selectedIndex	= js_get_index_at(ship_find_state.toUpperCase(),can_states);
            }

		}
		else {

			/// TEXT ON
			/// DROP OFF
			ship_state_text_row.className 	= 'ordertext';
			ship_state_text.disabled		= false;

			ship_state_drop_row.className	    = 'ordertextOFF';
			ship_state_drop.disabled		    = true;
			ship_state_drop_row_can.className	= 'ordertextOFF';
			ship_state_drop_can.disabled		= true;

		}

		/// BILL HANDLE - US
		if (  bill_country.options[bill_country.selectedIndex].value == 'US' ) {

            /// ATTEMPT TO FIND THE MATCHING DROP STATE
            if ( bill_state_text.value != undefined && bill_state_text.value.length >= 2 ) {
                bill_find_state = bill_state_text.value.substring(0,2);
            }

            /// TEXT OFF
            /// DROP ON
            bill_state_text_row.className 	    = 'ordertextOFF';
            bill_state_drop_row.className	    = 'ordertextOFF';
            bill_state_drop_can.disabled		= true;
            //bill_state_text.disabled		= true;

            bill_state_drop_row.className	= 'ordertext';
            bill_state_drop.disabled		= false;

            /// SET DROP
            if ( bill_state_drop.selectedIndex < 1 ) {
                bill_state_drop.selectedIndex	= js_get_index_at(bill_find_state.toUpperCase(),the_states);
            }

		}
		else if (  bill_country.options[bill_country.selectedIndex].value == 'CA' ) {

            /// ATTEMPT TO FIND THE MATCHING DROP STATE
            if ( bill_state_text.value != undefined && bill_state_text.value.length >= 2 ) {
                bill_find_state = bill_state_text.value.substring(0,2);
            }

            /// TEXT OFF
            /// DROP ON
            bill_state_text_row.className 	    = 'ordertextOFF';
            bill_state_drop_row.className	    = 'ordertextOFF';
            bill_state_drop.disabled		= true;
            //bill_state_text.disabled		= true;

            bill_state_drop_row_can.className	= 'ordertext';
            bill_state_drop_can.disabled		= false;

            /// SET DROP
            if ( bill_state_drop_can.selectedIndex < 1 ) {
                bill_state_drop_can.selectedIndex	= js_get_index_at(bill_find_state.toUpperCase(),can_states);
            }

		}
		else {

			/// TEXT ON
			/// DROP OFF
			bill_state_text_row.className 	= 'ordertext';
			bill_state_text.disabled		= false;

			bill_state_drop_row.className	    = 'ordertextOFF';
			bill_state_drop.disabled		    = true;
			bill_state_drop_row_can.className	= 'ordertextOFF';
			bill_state_drop_can.disabled		= true;

		}

	}

	function adjustBillShip() {

		tb = document.getElementById('bill_table');

		if ( document.forms['form'].elements['sameshipping'].checked ) {
			tb.className = 'ordertextOFF';
		}
		else {
			tb.className = 'body';
		}


	}

