

function setCookie(name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie(c_name) {
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

var url = document.location.href;
var get = url.split('?');

var c_val = getCookie( 'referrer' );



if (get[1]) {
    var reqs = get[1].split('&');
    var arr = new Array();
    for (var i=0; i<reqs.length;i++) {
        var this_req = reqs[i].split( '=' );
        arr[this_req[0]] = this_req[1];
    }
    if (arr['ref']) {
        var ref = arr['ref'];
        if (c_val == '' || c_val == null || c_val != ref) {
            setCookie( 'referrer', ref, 30, '/' );
            c_val = ref;
        }
    }
}


if (document.getElementById( 'referrer')) {
    document.getElementById( 'referrer' ).value = c_val;
}

Dialogs = {

    errorBox: function( errors ) {
      var text = '';
        for (var i=0; i<errors.length; i++) {
            text += '<li>'+errors[i]+'</li>';
        }

        $('#error_box_content').html('<ul>'+text+'</ul>');
        var div = '#error_box';
        $(div).addClass( 'ui-error' );
        $(div).dialog({
            bgiframe: true,
            autoOpen: false,
            width: 450,
            minHeight: 80,
            modal: true,
            title: 'Errors',
            open: function(){
                //$('#error_box_content').html('<ul>'+text+'</ul>');
            },
            close: function(){
                $(div).dialog( 'destroy' );
            }
        });

        $(div).dialog( 'open' );
    },

    confirmBox: function( title, text, callback ) {

        var div = '#confirm_box';
        $(div).addClass( 'ui-confirmation' );
        $(div).dialog({
            bgiframe: true,
            autoOpen: false,
            width: 450,
            minHeight: 100,
            autoHeight: true,
            modal: true,
            title: title,
            open: function(){
                $('#confirm_box_content').html( text );
                $('#confirm_box_yes_button').unbind().click( callback );
            },
            close: function(){
                $(div).dialog( 'destroy' );
            }
        });

        $(div).dialog( 'open' );
    },

    alertBox: function(alert,title) {
        var the_title = title || 'Alert!';
        $('#alert_box').dialog({
            bgiframe: true,
            autoOpen: false,
            width: 300,
            minHeight: 120,
            modal: true,
            title: the_title,
            open: function(){
                $('#alert_box_content').html( alert );
            },
            close: function() {
                $('#alert_box').dialog('destroy');
            }
        });

        $('#alert_box').dialog( 'open' );
    }
}



$(document).ready(function() {
    $('#date1').datepicker({
        showOn: 'both',
        buttonImage: '/images/calendar-icon.gif'
    });

    $('#signup_form').form({
		confirm: {
			title: 'Confirm Information',
			desc: 'Are you sure your information is correct?'
		},
		callback: function( req ) {
			if (req && req.success) {
				$('#confirm_box').dialog('close');
    			$('#signup_form_wrapper').html(req.tpl).show();
			}
		}
    });

    $('#doctor_form').form({
		confirm: {
			title: 'Confirm Information',
			desc: 'Are you sure your information is correct?'
		},
		callback: function( req ) {
			if (req && req.success) {
				$('#confirm_box').dialog('close');
                $('#doctor_form_wrapper').html(
                    '<div class="form-success">Thank you! Your request has been received. A representative will contact you soon.</div>'
                );
			}
		}
    });

    $('#registration_form').form({
		confirm: {
			title: 'Confirm Information',
			desc: 'Are you sure your information is correct?'
		},
		callback: function( req ) {
			if (req && req.success) {
				$('#confirm_box').dialog('close');
                $('#doctor_form_wrapper').html(
                    '<div class="form-success">Thank you! Your request has been received. A representative will contact you soon.</div>'
                );
			}
		}
    });

});
